├── .gitignore ├── Chapter 11 ├── 11.1 │ ├── LoadingAnXMLFile_End.java │ ├── LoadingAnXMLFile_Start.java │ └── cars.xml ├── 11.2 │ ├── ReadingXML_End.java │ ├── ReadingXML_Start.java │ └── cars.xml └── 11.3 │ ├── WritingXML_End.java │ ├── WritingXML_Start.java │ └── cars.xml ├── Chapter 2 ├── Characters.java ├── FloatingPointNumbers.java ├── StringsInJava.java ├── TheMathLib.java └── Variables.java ├── Chapter 3 ├── ComplexConditionals.java ├── ConditionalStatements2.java ├── ForLoop.java ├── IntroToLoops.java ├── Switcher.java └── WhileLoop.java ├── Chapter 4 ├── Alphabet.java ├── ChessBoard.java ├── Echo.java ├── Maps_End.java └── Maps_Start.java ├── Chapter 5 ├── AdvancedMethods_Begin.java ├── AdvancedMethods_End.java ├── TemperatureConverter_Begin.java └── TemperatureConverter_End.java ├── Chapter 6 ├── End │ ├── GettingObjectOriented_Begin.java │ ├── GettingObjectOriented_End.java │ ├── Person_Begin.java │ └── Person_End.java └── Start │ ├── GettingObjectOriented_Begin.java │ ├── GettingObjectOriented_End.java │ └── Person.java ├── Chapter 7 ├── 7.1 End │ ├── Book.java │ ├── Inheritance.java │ ├── Literature.java │ └── Poem.java ├── 7.1 Start │ ├── Book.java │ ├── Inheritance.java │ └── Poem.java ├── 7.2 End │ ├── Book.java │ ├── Inheritance.java │ ├── Literature.java │ └── Poem.java └── 7.2 Start │ ├── Book.java │ ├── Inheritance.java │ ├── Literature.java │ └── Poem.java ├── Chapter 8 ├── CustomPrinter.java ├── DatesAndTimes.java ├── Exceptions_End.java ├── Exceptions_Start.java ├── Strings_End.java └── Strings_Start.java ├── Chapter 9 ├── 9.1 │ ├── WritingToFiles_End.java │ └── WritingToFiles_Start.java ├── 9.2 │ ├── InputAndOutput_End.java │ └── InputAndOutput_Start.java ├── 9.3 End │ ├── Car.java │ ├── DeSerialize.java │ └── Serialize.java └── 9.3 Start │ └── Car.java └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # OS generated files 2 | .DS_Store 3 | .DS_Store? 4 | ._* 5 | .Spotlight-V100 6 | .Trashes 7 | Icon? 8 | ehthumbs.db 9 | Thumbs.db 10 | -------------------------------------------------------------------------------- /Chapter 11/11.1/LoadingAnXMLFile_End.java: -------------------------------------------------------------------------------- 1 | package loadinganxmlfile; 2 | 3 | import java.io.*; 4 | import javax.xml.parsers.*; 5 | import javax.xml.transform.*; 6 | import javax.xml.transform.dom.*; 7 | import javax.xml.transform.stream.*; 8 | import org.w3c.dom.*; 9 | import org.xml.sax.*; 10 | 11 | public class LoadingAnXMLFile { 12 | public static void main(String[] args) { 13 | Document dom; 14 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 15 | 16 | try { 17 | // Write code that can throw errors here... 18 | DocumentBuilder builder = factory.newDocumentBuilder(); 19 | dom = builder.parse("cars.xml"); 20 | 21 | PrintXmlDocument(dom); 22 | } 23 | catch (ParserConfigurationException pce) { 24 | System.out.println(pce.getMessage()); 25 | } 26 | catch (SAXException se) { 27 | System.out.println(se.getMessage()); 28 | } 29 | catch (IOException ioe) { 30 | System.err.println(ioe.getMessage()); 31 | } 32 | } 33 | 34 | private static void PrintXmlDocument(Document xml) 35 | { 36 | try{ 37 | Transformer transformer = TransformerFactory.newInstance().newTransformer(); 38 | StreamResult result = new StreamResult(new StringWriter()); 39 | DOMSource source = new DOMSource(xml); 40 | transformer.transform(source, result); 41 | System.out.println(result.getWriter().toString()); 42 | } 43 | catch(TransformerConfigurationException e) 44 | { 45 | System.err.println("XML Printing Failed"); 46 | } 47 | catch(TransformerException e) 48 | { 49 | System.err.println("XML Printing Failed"); 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /Chapter 11/11.1/LoadingAnXMLFile_Start.java: -------------------------------------------------------------------------------- 1 | package loadinganxmlfile; 2 | 3 | import java.io.*; 4 | import javax.xml.parsers.*; 5 | import javax.xml.transform.*; 6 | import javax.xml.transform.dom.*; 7 | import javax.xml.transform.stream.*; 8 | import org.w3c.dom.*; 9 | import org.xml.sax.*; 10 | 11 | public class LoadingAnXMLFile { 12 | public static void main(String[] args) { 13 | 14 | try { 15 | // Write code that can throw errors here... 16 | } 17 | catch (ParserConfigurationException pce) { 18 | System.out.println(pce.getMessage()); 19 | } 20 | catch (SAXException se) { 21 | System.out.println(se.getMessage()); 22 | } 23 | catch (IOException ioe) { 24 | System.err.println(ioe.getMessage()); 25 | } 26 | } 27 | 28 | private static void PrintXmlDocument(Document xml) 29 | { 30 | try{ 31 | Transformer transformer = TransformerFactory.newInstance().newTransformer(); 32 | StreamResult result = new StreamResult(new StringWriter()); 33 | DOMSource source = new DOMSource(xml); 34 | transformer.transform(source, result); 35 | System.out.println(result.getWriter().toString()); 36 | } 37 | catch(TransformerConfigurationException e) 38 | { 39 | System.err.println("XML Printing Failed"); 40 | } 41 | catch(TransformerException e) 42 | { 43 | System.err.println("XML Printing Failed"); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Chapter 11/11.1/cars.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Ford 6 | Fusion 7 | 2014 8 | Blue 9 | 10 | 11 | Toyota 12 | Tacoma 13 | 2013 14 | Green 15 | 16 | 17 | Dodge 18 | Charger 19 | 2013 20 | Red 21 | 22 | 23 | 24 | 25 | Nissan 26 | Altima 27 | 2000 28 | Green 29 | 30 | 31 | Dodge 32 | Challenger 33 | 2013 34 | Red 35 | 36 | 37 | -------------------------------------------------------------------------------- /Chapter 11/11.2/ReadingXML_End.java: -------------------------------------------------------------------------------- 1 | package readingxml; 2 | 3 | import java.io.IOException; 4 | import javax.xml.parsers.*; 5 | import org.w3c.dom.Document; 6 | import org.w3c.dom.Element; 7 | import org.w3c.dom.NodeList; 8 | import org.xml.sax.SAXException; 9 | 10 | public class ReadingXML { 11 | public static void main(String[] args) { 12 | Document dom; 13 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 14 | try { 15 | DocumentBuilder docBuilder = factory.newDocumentBuilder(); 16 | dom = docBuilder.parse("cars.xml"); 17 | 18 | // Now, print out all of Jane's cars... 19 | Element doc = dom.getDocumentElement(); 20 | NodeList ownersList = doc.getElementsByTagName("owner"); 21 | 22 | for(int i = 0; i < ownersList.getLength(); i++) 23 | { 24 | Element owner = (Element)ownersList.item(i); 25 | if(owner.getAttribute("name").equals("Jane")) 26 | { 27 | NodeList carsList = owner.getElementsByTagName("car"); 28 | PrintCars(carsList); 29 | } 30 | } 31 | } 32 | catch (ParserConfigurationException pce) { 33 | System.out.println(pce.getMessage()); 34 | } 35 | catch (SAXException se) { 36 | System.out.println(se.getMessage()); 37 | } 38 | catch (IOException ioe) { 39 | System.err.println(ioe.getMessage()); 40 | } 41 | } 42 | 43 | public static void PrintCars(NodeList cars) 44 | { 45 | for(int i = 0; i < cars.getLength(); i++) 46 | { 47 | Element carNode = (Element)cars.item(i); 48 | Car carObj = new Car(); 49 | carObj.color = carNode.getElementsByTagName("color").item(0).getTextContent(); 50 | carObj.make = carNode.getElementsByTagName("make").item(0).getTextContent(); 51 | carObj.model = carNode.getElementsByTagName("model").item(0).getTextContent(); 52 | carObj.year = Integer.parseInt(carNode.getElementsByTagName("year").item(0).getTextContent()); 53 | carObj.vin = carNode.getAttribute("vin"); 54 | System.out.println(carObj.toString()); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Chapter 11/11.2/ReadingXML_Start.java: -------------------------------------------------------------------------------- 1 | package readingxml; 2 | 3 | import java.io.IOException; 4 | import javax.xml.parsers.*; 5 | import org.w3c.dom.Document; 6 | import org.w3c.dom.Element; 7 | import org.w3c.dom.NodeList; 8 | import org.xml.sax.SAXException; 9 | 10 | public class ReadingXML { 11 | public static void main(String[] args) { 12 | Document dom; 13 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 14 | try { 15 | DocumentBuilder docBuilder = factory.newDocumentBuilder(); 16 | dom = docBuilder.parse("cars.xml"); 17 | 18 | // Now, print out all of Jane's cars... 19 | 20 | } 21 | catch (ParserConfigurationException pce) { 22 | System.out.println(pce.getMessage()); 23 | } 24 | catch (SAXException se) { 25 | System.out.println(se.getMessage()); 26 | } 27 | catch (IOException ioe) { 28 | System.err.println(ioe.getMessage()); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Chapter 11/11.2/cars.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Ford 6 | Fusion 7 | 2014 8 | Blue 9 | 10 | 11 | Toyota 12 | Tacoma 13 | 2013 14 | Green 15 | 16 | 17 | Dodge 18 | Charger 19 | 2013 20 | Red 21 | 22 | 23 | 24 | 25 | Nissan 26 | Altima 27 | 2000 28 | Green 29 | 30 | 31 | Dodge 32 | Challenger 33 | 2013 34 | Red 35 | 36 | 37 | -------------------------------------------------------------------------------- /Chapter 11/11.3/WritingXML_End.java: -------------------------------------------------------------------------------- 1 | package writingxml; 2 | 3 | import java.io.*; 4 | import javax.xml.parsers.*; 5 | import javax.xml.transform.*; 6 | import javax.xml.transform.dom.*; 7 | import javax.xml.transform.stream.*; 8 | import org.w3c.dom.*; 9 | import org.xml.sax.*; 10 | 11 | public class WritingXML { 12 | public static void main(String[] args) { 13 | File xmlFile = new File("cars.xml"); 14 | Document dom = LoadXMLDocument(xmlFile); 15 | 16 | NodeList owners = dom.getElementsByTagName("owner"); 17 | for(int i = 0; i < owners.getLength(); i++) 18 | { 19 | Element owner = (Element)owners.item(i); 20 | owner.setAttribute("name", "Mike"); 21 | } 22 | 23 | WriteXMLDocument(dom, xmlFile); 24 | } 25 | 26 | private static void WriteXMLDocument(Document doc, File destination) 27 | { 28 | try{ 29 | // Write doc to destination file here... 30 | TransformerFactory tf = TransformerFactory.newInstance(); 31 | Transformer transformer = tf.newTransformer(); 32 | StreamResult result = new StreamResult(destination); 33 | DOMSource source = new DOMSource(doc); 34 | 35 | transformer.transform(source, result); 36 | } 37 | catch(TransformerConfigurationException e) 38 | { 39 | System.err.println("XML writing failed."); 40 | } 41 | catch(TransformerException e) 42 | { 43 | System.err.println("XML writing failed."); 44 | } 45 | } 46 | 47 | private static Document LoadXMLDocument(File source) 48 | { 49 | Document dom = null; 50 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 51 | 52 | try { 53 | DocumentBuilder builder = factory.newDocumentBuilder(); 54 | dom = builder.parse(source); 55 | } 56 | catch (ParserConfigurationException e) { 57 | System.err.println("XML loading failed."); 58 | } 59 | catch (SAXException e) { 60 | System.err.println("XML loading failed."); 61 | } 62 | catch (IOException e) { 63 | System.err.println("XML loading failed."); 64 | } 65 | 66 | return dom; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Chapter 11/11.3/WritingXML_Start.java: -------------------------------------------------------------------------------- 1 | package writingxml; 2 | 3 | import java.io.*; 4 | import javax.xml.parsers.*; 5 | import javax.xml.transform.*; 6 | import javax.xml.transform.dom.*; 7 | import javax.xml.transform.stream.*; 8 | import org.w3c.dom.*; 9 | import org.xml.sax.*; 10 | 11 | public class WritingXML { 12 | public static void main(String[] args) { 13 | File xmlFile = new File("cars.xml"); 14 | Document dom = LoadXMLDocument(xmlFile); 15 | WriteXMLDocument(dom, xmlFile); 16 | } 17 | 18 | private static void WriteXMLDocument(Document doc, File destination) 19 | { 20 | try{ 21 | // Write doc to destination file here... 22 | } 23 | catch(TransformerConfigurationException e) 24 | { 25 | System.err.println("XML writing failed."); 26 | } 27 | catch(TransformerException e) 28 | { 29 | System.err.println("XML writing failed."); 30 | } 31 | } 32 | 33 | private static Document LoadXMLDocument(File source) 34 | { 35 | Document dom = null; 36 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 37 | 38 | try { 39 | DocumentBuilder builder = factory.newDocumentBuilder(); 40 | dom = builder.parse(source); 41 | } 42 | catch (ParserConfigurationException e) { 43 | System.err.println("XML loading failed."); 44 | } 45 | catch (SAXException e) { 46 | System.err.println("XML loading failed."); 47 | } 48 | catch (IOException e) { 49 | System.err.println("XML loading failed."); 50 | } 51 | 52 | return dom; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Chapter 11/11.3/cars.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Ford 6 | Fusion 7 | 2014 8 | Blue 9 | 10 | 11 | Toyota 12 | Tacoma 13 | 2013 14 | Green 15 | 16 | 17 | Dodge 18 | Charger 19 | 2013 20 | Red 21 | 22 | 23 | 24 | 25 | Nissan 26 | Altima 27 | 2000 28 | Green 29 | 30 | 31 | Dodge 32 | Challenger 33 | 2013 34 | Red 35 | 36 | 37 | -------------------------------------------------------------------------------- /Chapter 2/Characters.java: -------------------------------------------------------------------------------- 1 | package characters; 2 | 3 | public class Characters { 4 | public static void main(String[] args) { 5 | char character1 = 'H'; 6 | char character2 = 9; 7 | char character3 = 9; 8 | char character4 = 9; 9 | char character5 = 'o'; 10 | 11 | System.out.print(character1); 12 | System.out.print(character2); 13 | System.out.print(character3); 14 | System.out.print(character4); 15 | System.out.println(character5); 16 | } 17 | } -------------------------------------------------------------------------------- /Chapter 2/FloatingPointNumbers.java: -------------------------------------------------------------------------------- 1 | 2 | package floatingpointnumbers; 3 | 4 | public class FloatingPointNumbers { 5 | public static void main(String[] args) { 6 | 7 | int iNumber1 = 5; 8 | int iNumber2 = 6; 9 | float fNumber = (float)iNumber1/iNumber2; 10 | 11 | System.out.println(fNumber); 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter 2/StringsInJava.java: -------------------------------------------------------------------------------- 1 | package stringsinjava; 2 | 3 | public class StringsInJava { 4 | public static void main(String[] args) { 5 | /*char c = 'c'; 6 | String s1 = "stringone"; 7 | String s2 = "stringtwo"; 8 | String s3 = s1 + s2 + "LIT"; 9 | 10 | s3 = s3.toUpperCase(); 11 | 12 | System.out.println( s3.replace('G','o') ); 13 | System.out.println( s3 );*/ 14 | 15 | String s = "The program says: \\ \"Hello World\""; 16 | System.out.println(s); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter 2/TheMathLib.java: -------------------------------------------------------------------------------- 1 | package themathlib; 2 | 3 | import java.lang.Math; 4 | 5 | public class TheMathLib { 6 | public static void main(String[] args) { 7 | double number = 4.321; 8 | number = Math.pow(number, 4.0); 9 | System.out.println(number); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter 2/Variables.java: -------------------------------------------------------------------------------- 1 | package variables; 2 | 3 | public class Variables { 4 | 5 | public static void main(String[] args) { 6 | 7 | int x; 8 | int y; 9 | 10 | x = 10; 11 | y = 5; 12 | 13 | //2147483647 14 | //-2147483648 15 | 16 | System.out.println(x+y); 17 | System.out.println(x-y); 18 | System.out.println(x*y); 19 | System.out.println(x/y); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter 3/ComplexConditionals.java: -------------------------------------------------------------------------------- 1 | 2 | package complexconditionals; 3 | 4 | import java.util.*; 5 | 6 | public class ComplexConditionals { 7 | public static void main(String[] args) { 8 | Scanner reader = new Scanner(System.in); 9 | String input = reader.next(); 10 | String sOne = "abc"; 11 | String sTwo = "z"; 12 | 13 | boolean bool1 = input.contains(sOne); 14 | boolean bool2 = input.contains(sTwo); 15 | 16 | if((bool1 || bool2) && false) 17 | { 18 | System.out.println("TRUE"); 19 | } 20 | else 21 | { 22 | System.out.println("FALSE"); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Chapter 3/ConditionalStatements2.java: -------------------------------------------------------------------------------- 1 | 2 | package conditionalstatements2; 3 | 4 | import java.util.*; 5 | 6 | public class ConditionalStatements2 { 7 | 8 | public static void main(String[] args) { 9 | Scanner reader = new Scanner(System.in); 10 | System.out.println("Input now: "); 11 | int input = reader.nextInt(); 12 | 13 | if(input > 10) 14 | { 15 | System.out.println("MORE!"); 16 | } 17 | else 18 | { 19 | System.out.println("LESS!"); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Chapter 3/ForLoop.java: -------------------------------------------------------------------------------- 1 | package forloops; 2 | 3 | public class ForLoops { 4 | public static void main(String[] args) { 5 | for(int i = 1; i <= 100; i++) 6 | { 7 | System.out.println(i); 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /Chapter 3/IntroToLoops.java: -------------------------------------------------------------------------------- 1 | package introtoloops; 2 | 3 | import java.util.*; 4 | 5 | public class IntroToLoops { 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | 9 | String input; 10 | String all = ""; 11 | 12 | do 13 | { 14 | input = reader.nextLine(); 15 | all += input; 16 | } while(!input.equals("STOP")); 17 | 18 | System.out.println(all); 19 | } 20 | } -------------------------------------------------------------------------------- /Chapter 3/Switcher.java: -------------------------------------------------------------------------------- 1 | package switcher; 2 | 3 | public class Switcher { 4 | public static void main(String[] args) { 5 | int x = 7; 6 | 7 | switch(x) 8 | { 9 | case 1: case 5: case 7: 10 | System.out.println("RED"); 11 | break; 12 | case 2: 13 | System.out.println("BLUE"); 14 | break; 15 | case 3: 16 | System.out.println("GREEN"); 17 | break; 18 | default: 19 | System.out.println("NONE"); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /Chapter 3/WhileLoop.java: -------------------------------------------------------------------------------- 1 | package forloops; 2 | 3 | public class ForLoops { 4 | public static void main(String[] args) { 5 | int i = 1; 6 | while(i <= 100) 7 | { 8 | System.out.println(i); 9 | i++; 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter 4/Alphabet.java: -------------------------------------------------------------------------------- 1 | package alphabet; 2 | 3 | import java.util.*; 4 | 5 | public class Alphabet { 6 | public static void main(String[] args) { 7 | //97 8 | char[] alpha = new char[26]; 9 | 10 | for(int i = 0; i < 26; i++) 11 | { 12 | alpha[i] = (char)(97 + i); 13 | } 14 | 15 | System.out.println(Arrays.toString(alpha)); 16 | } 17 | } -------------------------------------------------------------------------------- /Chapter 4/ChessBoard.java: -------------------------------------------------------------------------------- 1 | package chessboard; 2 | 3 | import java.util.*; 4 | 5 | public class ChessBoard { 6 | public static void main(String[] args) { 7 | int boardDim = 8; 8 | char[][] board = new char[boardDim][boardDim]; 9 | boolean isWhite = false; 10 | 11 | for(int y = 0; y < board.length; y++) 12 | { 13 | isWhite = !isWhite; 14 | for(int x = 0; x < board[y].length; x++) 15 | { 16 | if(isWhite) board[y][x] = 'W'; 17 | if(!isWhite) board[y][x] = 'B'; 18 | isWhite = !isWhite; 19 | } 20 | } 21 | 22 | for(int i = 0; i < board.length; i++) 23 | { 24 | System.out.println(Arrays.toString(board[i])); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Chapter 4/Echo.java: -------------------------------------------------------------------------------- 1 | package echo; 2 | 3 | import java.util.*; 4 | 5 | public class Echo { 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | ArrayList memory = new ArrayList(); 9 | 10 | while(true) 11 | { 12 | memory.add(reader.nextLine()); 13 | 14 | if((memory.get(memory.size()-1)).equals("CLEAR")) { 15 | memory.clear(); 16 | } 17 | else { 18 | if((memory.get(memory.size()-1)).equals("END")) 19 | break; 20 | } 21 | 22 | System.out.println(memory.toString()); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /Chapter 4/Maps_End.java: -------------------------------------------------------------------------------- 1 | package maps; 2 | 3 | import java.util.*; 4 | 5 | public class Maps { 6 | public static void main(String[] args) { 7 | String[] allNames = 8 | // 9 | {"Jane", "Addams", 10 | "Muhammad", "Ali", 11 | "Stephen", "Ambrose", 12 | "Louis", "Armstrong", 13 | "Joan", "Baez", 14 | "Josephine", "Baker", 15 | "Clyde", "Barrow", 16 | "Elizabeth", "Blackwell", 17 | "Molly", "Brown", 18 | "Rachel", "Carson", 19 | "Fidel", "Castro", 20 | "Coco", "Chanel", 21 | "Charlie", "Chaplin", 22 | "Agatha", "Christie", 23 | "Amelia", "Earhart", 24 | "Thomas", "Edison", 25 | "Albert", "Einstein", 26 | "Enrico", "Fermi", 27 | "Henry", "Ford", 28 | "Dian", "Fossey", 29 | "Anne", "Frank", 30 | "Sigmund", "Freud", 31 | "Emma", "Goldman", 32 | "Jane", "Goodall", 33 | "Mikhail", "Gorbachev", 34 | "Cary", "Grant", 35 | "Che", "Guevara", 36 | "Ernest", "Hemingway", 37 | "Audrey", "Hepburn", 38 | "Alfred", "Hitchcock", 39 | "Adolf", "Hitler", 40 | "Harry", "Houdini", 41 | "Howard", "Hughes", 42 | "Helen", "Keller", 43 | "Grace", "Kelly", 44 | "Jackie", "Kennedy", 45 | "Charles", "Manson", 46 | "Iqbal", "Masih", 47 | "Mata", "Hari", 48 | "Henri", "Matisse", 49 | "Golda", "Meir", 50 | "Marilyn", "Monroe", 51 | "Benito", "Mussolini", 52 | "Emmeline", "Pankhurst", 53 | "Bonnie", "Parker", 54 | "Pol", "Pot", 55 | "Elvis", "Presley", 56 | "Princess", "Diana", 57 | "Ronald", "Reagan", 58 | "Sally", "Ride", 59 | "Leni", "Riefenstahl", 60 | "Jackie", "Robinson", 61 | "The", "Rolling Stones", 62 | "Nicholas", "Romanov", 63 | "Eleanor", "Roosevelt", 64 | "Frank", "Sinatra", 65 | "Joseph", "Stalin", 66 | "Edith", "Stein", 67 | "Jimmy", "Stewart", 68 | "Nikola", "Tesla", 69 | "Margaret", "Thatcher", 70 | "Jim", "Thorpe", 71 | "Leon", "Trotsky", 72 | "Alice", "Walker", 73 | "Andy", "Warhol"}; 74 | // 75 | String[] firstNames = new String[allNames.length/2]; 76 | String[] lastNames = new String[allNames.length/2]; 77 | for(int i = 0; i < allNames.length; i++) 78 | { 79 | /*This if statement checks if we are in an EVEN NUMBERED iteration 80 | % is the "mod" or "modulus" operator... 81 | it returns the remainder after we divide number1 by number2)*/ 82 | if(i % 2 == 0) 83 | { 84 | //We are in an even number iteration - looking at a first name 85 | firstNames[i/2] = allNames[i]; 86 | } 87 | else 88 | { 89 | //We are in an odd number iteration - looking at a last name 90 | lastNames[i/2] = allNames[i]; 91 | } 92 | } 93 | 94 | Map famousPeople = new HashMap<>(); 95 | for(int i = 0; i < lastNames.length; i++) 96 | { 97 | famousPeople.put(lastNames[i],firstNames[i]); 98 | } 99 | 100 | System.out.println(famousPeople.get("Sinatra")); 101 | } 102 | } -------------------------------------------------------------------------------- /Chapter 4/Maps_Start.java: -------------------------------------------------------------------------------- 1 | package maps; 2 | 3 | import java.util.*; 4 | 5 | public class Maps { 6 | public static void main(String[] args) { 7 | String[] allNames = 8 | // 9 | {"Jane", "Addams", 10 | "Muhammad", "Ali", 11 | "Stephen", "Ambrose", 12 | "Louis", "Armstrong", 13 | "Joan", "Baez", 14 | "Josephine", "Baker", 15 | "Clyde", "Barrow", 16 | "Elizabeth", "Blackwell", 17 | "Molly", "Brown", 18 | "Rachel", "Carson", 19 | "Fidel", "Castro", 20 | "Coco", "Chanel", 21 | "Charlie", "Chaplin", 22 | "Agatha", "Christie", 23 | "Amelia", "Earhart", 24 | "Thomas", "Edison", 25 | "Albert", "Einstein", 26 | "Enrico", "Fermi", 27 | "Henry", "Ford", 28 | "Dian", "Fossey", 29 | "Anne", "Frank", 30 | "Sigmund", "Freud", 31 | "Emma", "Goldman", 32 | "Jane", "Goodall", 33 | "Mikhail", "Gorbachev", 34 | "Cary", "Grant", 35 | "Che", "Guevara", 36 | "Ernest", "Hemingway", 37 | "Audrey", "Hepburn", 38 | "Alfred", "Hitchcock", 39 | "Adolf", "Hitler", 40 | "Harry", "Houdini", 41 | "Howard", "Hughes", 42 | "Helen", "Keller", 43 | "Grace", "Kelly", 44 | "Jackie", "Kennedy", 45 | "Charles", "Manson", 46 | "Iqbal", "Masih", 47 | "Mata", "Hari", 48 | "Henri", "Matisse", 49 | "Golda", "Meir", 50 | "Marilyn", "Monroe", 51 | "Benito", "Mussolini", 52 | "Emmeline", "Pankhurst", 53 | "Bonnie", "Parker", 54 | "Pol", "Pot", 55 | "Elvis", "Presley", 56 | "Princess", "Diana", 57 | "Ronald", "Reagan", 58 | "Sally", "Ride", 59 | "Leni", "Riefenstahl", 60 | "Jackie", "Robinson", 61 | "The", "Rolling Stones", 62 | "Nicholas", "Romanov", 63 | "Eleanor", "Roosevelt", 64 | "Frank", "Sinatra", 65 | "Joseph", "Stalin", 66 | "Edith", "Stein", 67 | "Jimmy", "Stewart", 68 | "Nikola", "Tesla", 69 | "Margaret", "Thatcher", 70 | "Jim", "Thorpe", 71 | "Leon", "Trotsky", 72 | "Alice", "Walker", 73 | "Andy", "Warhol"}; 74 | // 75 | String[] firstNames = new String[allNames.length/2]; 76 | String[] lastNames = new String[allNames.length/2]; 77 | for(int i = 0; i < allNames.length; i++) 78 | { 79 | /*This if statement checks if we are in an EVEN NUMBERED iteration 80 | % is the "mod" or "modulus" operator... 81 | it returns the remainder after we divide number1 by number2)*/ 82 | if(i % 2 == 0) 83 | { 84 | //We are in an even number iteration - looking at a first name 85 | firstNames[i/2] = allNames[i]; 86 | } 87 | else 88 | { 89 | //We are in an odd number iteration - looking at a last name 90 | lastNames[i/2] = allNames[i]; 91 | } 92 | } 93 | 94 | System.out.println(Arrays.toString(firstNames)); 95 | System.out.println(Arrays.toString(lastNames)); 96 | } 97 | } -------------------------------------------------------------------------------- /Chapter 5/AdvancedMethods_Begin.java: -------------------------------------------------------------------------------- 1 | package advancedmethods; 2 | 3 | public class AdvancedMethods { 4 | public static void main(String[] args) { 5 | int[] x = 5; 6 | 7 | magic(x); 8 | 9 | System.out.println("main: " + x); 10 | } 11 | 12 | public static void magic(int input) 13 | { 14 | input += 10; 15 | } 16 | } -------------------------------------------------------------------------------- /Chapter 5/AdvancedMethods_End.java: -------------------------------------------------------------------------------- 1 | package advancedmethods; 2 | 3 | import java.util.*; 4 | 5 | public class AdvancedMethods { 6 | public static void main(String[] args) { 7 | int[] x = {5,4,3,2,1}; 8 | 9 | magic(x); 10 | 11 | System.out.println("main: " + Arrays.toString(x)); 12 | } 13 | 14 | public static void magic(int input) 15 | { 16 | input += 10; 17 | } 18 | public static void magic(int[] input) 19 | { 20 | for(int i = 0; i < input.length; i++) 21 | input[i] += 10; 22 | } 23 | } -------------------------------------------------------------------------------- /Chapter 5/TemperatureConverter_Begin.java: -------------------------------------------------------------------------------- 1 | package temperatureconverter; 2 | 3 | import java.util.*; 4 | 5 | // F to C: ((t-32.0f)*5.0f)/9.0f 6 | // C to K: t+273.15f 7 | // K to F: (((t-273.15f)*9.0f)/5.0f)+32.0f 8 | 9 | public class TemperatureConverter { 10 | public static void main(String[] args) { 11 | Scanner reader = new Scanner(System.in); 12 | char inputType; 13 | char outputType; 14 | float inputValue; 15 | float returnValue; 16 | 17 | System.out.print("Input type (F/C/K): "); 18 | inputType = reader.next().charAt(0); 19 | System.out.print("Output type (F/C/K): "); 20 | outputType = reader.next().charAt(0); 21 | System.out.print("Temperature: "); 22 | inputValue = reader.nextFloat(); 23 | 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /Chapter 5/TemperatureConverter_End.java: -------------------------------------------------------------------------------- 1 | package temperatureconverter; 2 | 3 | import java.util.*; 4 | 5 | // F to C: ((t-32.0f)*5.0f)/9.0f 6 | // C to K: t+273.15f 7 | // K to F: (((t-273.15f)*9.0f)/5.0f)+32.0f 8 | 9 | public class TemperatureConverter { 10 | public static void main(String[] args) { 11 | Scanner reader = new Scanner(System.in); 12 | char inputType; 13 | char outputType; 14 | float inputValue; 15 | float returnValue; 16 | 17 | System.out.print("Input type (F/C/K): "); 18 | inputType = reader.next().charAt(0); 19 | System.out.print("Output type (F/C/K): "); 20 | outputType = reader.next().charAt(0); 21 | System.out.print("Temperature: "); 22 | inputValue = reader.nextFloat(); 23 | 24 | switch(inputType) 25 | { 26 | case 'F': 27 | inputValue = fToC(inputValue); 28 | break; 29 | case 'C': 30 | break; 31 | case 'K': 32 | inputValue = fToC(kToF(inputValue)); 33 | break; 34 | default: 35 | System.exit(1); 36 | } 37 | 38 | switch(outputType) 39 | { 40 | case 'F': 41 | inputValue = kToF(cToK(inputValue)); 42 | break; 43 | case 'C': 44 | break; 45 | case 'K': 46 | inputValue = cToK(inputValue); 47 | break; 48 | default: 49 | System.exit(1); 50 | } 51 | 52 | System.out.println(inputValue); 53 | } 54 | 55 | public static float fToC(float fVal) 56 | { 57 | return ((fVal-32.0f)*5.0f)/9.0f; 58 | } 59 | public static float kToF(float kVal) 60 | { 61 | return (((kVal-273.15f)*9.0f)/5.0f)+32.0f; 62 | } 63 | public static float cToK(float cVal) 64 | { 65 | return cVal+273.15f; 66 | } 67 | } -------------------------------------------------------------------------------- /Chapter 6/End/GettingObjectOriented_Begin.java: -------------------------------------------------------------------------------- 1 | package gettingobjectoriented; 2 | 3 | import java.util.*; 4 | 5 | public class GettingObjectOriented { 6 | public static void main(String[] args) { 7 | Person john = new Person(); 8 | john.firstName = "John"; 9 | john.lastName = "Doe"; 10 | john.birthday = new GregorianCalendar(1988,1,5); 11 | 12 | System.out.println( 13 | "Hello my name is " + 14 | john.fullName() + 15 | ". I am " + 16 | john.age(new GregorianCalendar()) + 17 | " years old."); 18 | } 19 | } -------------------------------------------------------------------------------- /Chapter 6/End/GettingObjectOriented_End.java: -------------------------------------------------------------------------------- 1 | package gettingobjectoriented; 2 | 3 | import java.util.*; 4 | 5 | public class GettingObjectOriented { 6 | public static void main(String[] args) { 7 | Person john = new Person("John", "Doe", new GregorianCalendar(1988,1,5)); 8 | //Person john = new Person("John", "Doe"); 9 | //Person john = new Person(); 10 | 11 | System.out.println( 12 | "Hello my name is " + 13 | john.fullName() + 14 | ". I am " + 15 | john.age(new GregorianCalendar()) + 16 | " years old."); 17 | } 18 | } -------------------------------------------------------------------------------- /Chapter 6/End/Person_Begin.java: -------------------------------------------------------------------------------- 1 | package gettingobjectoriented; 2 | 3 | import java.util.*; 4 | 5 | public class Person { 6 | public String firstName; 7 | public String lastName; 8 | public Calendar birthday; 9 | 10 | public String fullName() 11 | { 12 | return firstName + " " + lastName; 13 | } 14 | 15 | public int age(Calendar today) 16 | { 17 | return today.get(Calendar.YEAR) - birthday.get(Calendar.YEAR); 18 | } 19 | } -------------------------------------------------------------------------------- /Chapter 6/End/Person_End.java: -------------------------------------------------------------------------------- 1 | package gettingobjectoriented; 2 | 3 | import java.util.*; 4 | 5 | public class Person { 6 | private String firstName; 7 | private String lastName; 8 | private Calendar birthday; 9 | 10 | public Person() 11 | { 12 | firstName = ""; 13 | lastName = ""; 14 | birthday = new GregorianCalendar(); 15 | } 16 | public Person(String firstName, String lastName) 17 | { 18 | this.firstName = firstName; 19 | this.lastName = lastName; 20 | this.birthday = new GregorianCalendar(); 21 | } 22 | public Person(String firstName, String lastName, Calendar birthday) 23 | { 24 | this.firstName = firstName; 25 | this.lastName = lastName; 26 | this.birthday = birthday; 27 | } 28 | 29 | public String fullName() 30 | { 31 | return firstName + " " + lastName; 32 | } 33 | 34 | public int age(Calendar today) 35 | { 36 | return today.get(Calendar.YEAR) - birthday.get(Calendar.YEAR); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Chapter 6/Start/GettingObjectOriented_Begin.java: -------------------------------------------------------------------------------- 1 | package gettingobjectoriented; 2 | 3 | import java.util.*; 4 | 5 | public class GettingObjectOriented { 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | 9 | System.out.println(reader.next()); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter 6/Start/GettingObjectOriented_End.java: -------------------------------------------------------------------------------- 1 | package gettingobjectoriented; 2 | 3 | import java.util.*; 4 | 5 | public class GettingObjectOriented { 6 | public static void main(String[] args) { 7 | Person john = new Person(); 8 | john.firstName = "John"; 9 | john.lastName = "Doe"; 10 | john.birthday = new GregorianCalendar(1988,1,5); 11 | 12 | System.out.println(john.age(new GregorianCalendar())); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter 6/Start/Person.java: -------------------------------------------------------------------------------- 1 | package gettingobjectoriented; 2 | 3 | import java.util.*; 4 | 5 | public class Person { 6 | public String firstName; 7 | public String lastName; 8 | public Calendar birthday; 9 | 10 | public String fullName() 11 | { 12 | return firstName + " " + lastName; 13 | } 14 | 15 | public int age(Calendar today) 16 | { 17 | return today.get(Calendar.YEAR) - birthday.get(Calendar.YEAR); 18 | } 19 | } -------------------------------------------------------------------------------- /Chapter 7/7.1 End/Book.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | public class Book extends Literature { 3 | private String publisher; 4 | private String genre; 5 | 6 | public Book(String title, String author, String publisher, String genre) 7 | { 8 | super(title, author); 9 | this.publisher = publisher; 10 | this.genre = genre; 11 | } 12 | 13 | @Override public void Print() 14 | { 15 | super.Print(); 16 | System.out.println("\tPublished By: " + publisher); 17 | System.out.println("\tIs A: " + genre); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Chapter 7/7.1 End/Inheritance.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | 3 | public class Inheritance { 4 | public static void main(String[] args) { 5 | Book a = new Book( 6 | "The Lord Of The Rings", 7 | "J.R.R. Tolkein", 8 | "George Allen and Unwin", 9 | "Fantasy"); 10 | Poem b = new Poem( 11 | "The Iliad", 12 | "Homer", 13 | "Dactylic Hexameter"); 14 | 15 | Literature[] lits = new Literature[5]; 16 | lits[0] = a; 17 | lits[1] = b; 18 | lits[2] = a; 19 | lits[3] = b; 20 | lits[4] = a; 21 | 22 | for(int i = 0; i < lits.length; i++) 23 | { 24 | lits[i].Print(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter 7/7.1 End/Literature.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | public class Literature { 3 | protected String title; 4 | protected String author; 5 | 6 | public Literature(String title, String author) 7 | { 8 | this.title = title; 9 | this.author = author; 10 | } 11 | 12 | public void Print() 13 | { 14 | System.out.println(title); 15 | System.out.println("\tWritten By: " + author); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter 7/7.1 End/Poem.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | public class Poem extends Literature { 3 | private String style; 4 | 5 | public Poem(String title, String author, String style) 6 | { 7 | super(title, author); 8 | this.style = style; 9 | } 10 | 11 | @Override public void Print() 12 | { 13 | System.out.println("POEM: " + title); 14 | System.out.println("\tWritten By: " + author); 15 | System.out.println("\tIn The Style Of: " + style); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter 7/7.1 Start/Book.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | public class Book { 3 | private String title; 4 | private String author; 5 | private String publisher; 6 | private String genre; 7 | 8 | public Book(String title, String author, String publisher, String genre) 9 | { 10 | this.title = title; 11 | this.author = author; 12 | this.publisher = publisher; 13 | this.genre = genre; 14 | } 15 | 16 | public void Print() 17 | { 18 | System.out.println(title); 19 | System.out.println("\tWritten By: " + author); 20 | System.out.println("\tPublished By: " + publisher); 21 | System.out.println("\tIs A: " + genre); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Chapter 7/7.1 Start/Inheritance.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | 3 | public class Inheritance { 4 | public static void main(String[] args) { 5 | Book a = new Book( 6 | "The Lord Of The Rings", 7 | "J.R.R. Tolkein", 8 | "George Allen and Unwin", 9 | "Fantasy"); 10 | Poem b = new Poem( 11 | "The Iliad", 12 | "Homer", 13 | "Dactylic Hexameter"); 14 | 15 | a.Print(); 16 | b.Print(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter 7/7.1 Start/Poem.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | public class Poem { 3 | private String title; 4 | private String author; 5 | private String style; 6 | 7 | public Poem(String title, String author, String style) 8 | { 9 | this.title = title; 10 | this.author = author; 11 | this.style = style; 12 | } 13 | 14 | public void Print() 15 | { 16 | System.out.println(title); 17 | System.out.println("\tWritten By: " + author); 18 | System.out.println("\tIn The Style Of: " + style); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter 7/7.2 End/Book.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | public class Book extends Literature { 3 | private String publisher; 4 | private String genre; 5 | 6 | public Book(String title, String author, String publisher, String genre) 7 | { 8 | this.title = title; 9 | this.author = author; 10 | this.publisher = publisher; 11 | this.genre = genre; 12 | } 13 | 14 | @Override public void Print() 15 | { 16 | super.Print(); 17 | System.out.println("\tPublished By: " + publisher); 18 | System.out.println("\tIs A: " + genre); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter 7/7.2 End/Inheritance.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | 3 | public class Inheritance { 4 | public static void main(String[] args) { 5 | Book a = new Book( 6 | "The Lord Of The Rings", 7 | "J.R.R. Tolkein", 8 | "George Allen and Unwin", 9 | "Fantasy"); 10 | Poem b = new Poem( 11 | "The Iliad", 12 | "Homer", 13 | "Dactylic Hexameter"); 14 | 15 | Literature[] lits = new Literature[5]; 16 | lits[0] = a; 17 | lits[1] = b; 18 | lits[2] = a; 19 | lits[3] = b; 20 | lits[4] = a; 21 | 22 | for(int i = 0; i < lits.length; i++) 23 | { 24 | lits[i].Print(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter 7/7.2 End/Literature.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | public abstract class Literature { 3 | protected String title; 4 | protected String author; 5 | 6 | public void Print() 7 | { 8 | System.out.println(title); 9 | System.out.println("\tWritten By: " + author); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter 7/7.2 End/Poem.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | public class Poem extends Literature { 3 | private String style; 4 | 5 | public Poem(String title, String author, String style) 6 | { 7 | this.title = title; 8 | this.author = author; 9 | this.style = style; 10 | } 11 | 12 | @Override public void Print() 13 | { 14 | System.out.println("POEM: " + title); 15 | System.out.println("\tWritten By: " + author); 16 | System.out.println("\tIn The Style Of: " + style); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter 7/7.2 Start/Book.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | public class Book extends Literature { 3 | private String publisher; 4 | private String genre; 5 | 6 | public Book(String title, String author, String publisher, String genre) 7 | { 8 | super(title, author); 9 | this.publisher = publisher; 10 | this.genre = genre; 11 | } 12 | 13 | @Override public void Print() 14 | { 15 | super.Print(); 16 | System.out.println("\tPublished By: " + publisher); 17 | System.out.println("\tIs A: " + genre); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Chapter 7/7.2 Start/Inheritance.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | 3 | public class Inheritance { 4 | public static void main(String[] args) { 5 | Book a = new Book( 6 | "The Lord Of The Rings", 7 | "J.R.R. Tolkein", 8 | "George Allen and Unwin", 9 | "Fantasy"); 10 | Poem b = new Poem( 11 | "The Iliad", 12 | "Homer", 13 | "Dactylic Hexameter"); 14 | 15 | Literature[] lits = new Literature[5]; 16 | lits[0] = a; 17 | lits[1] = b; 18 | lits[2] = a; 19 | lits[3] = b; 20 | lits[4] = a; 21 | 22 | for(int i = 0; i < lits.length; i++) 23 | { 24 | lits[i].Print(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter 7/7.2 Start/Literature.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | public class Literature { 3 | protected String title; 4 | protected String author; 5 | 6 | public Literature(String title, String author) 7 | { 8 | this.title = title; 9 | this.author = author; 10 | } 11 | 12 | public void Print() 13 | { 14 | System.out.println(title); 15 | System.out.println("\tWritten By: " + author); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter 7/7.2 Start/Poem.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | public class Poem extends Literature { 3 | private String style; 4 | 5 | public Poem(String title, String author, String style) 6 | { 7 | super(title, author); 8 | this.style = style; 9 | } 10 | 11 | @Override public void Print() 12 | { 13 | System.out.println("POEM: " + title); 14 | System.out.println("\tWritten By: " + author); 15 | System.out.println("\tIn The Style Of: " + style); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter 8/CustomPrinter.java: -------------------------------------------------------------------------------- 1 | package strings; 2 | 3 | public class CustomPrinter { 4 | private String formatString; 5 | 6 | public CustomPrinter(String format) 7 | { 8 | formatString = format; 9 | } 10 | 11 | public void println(String input) 12 | { 13 | String formatted = String.format(formatString, input); 14 | System.out.println(formatted); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter 8/DatesAndTimes.java: -------------------------------------------------------------------------------- 1 | package DatesAndTimes; 2 | 3 | import java.util.*; 4 | 5 | public class DatesAndTimes { 6 | Calendar now = Calendar.getInstance(); 7 | 8 | now.setTimeInMillis(0); 9 | 10 | System.out.println(now.get(Calendar.MONTH) + 1); 11 | System.out.println(now.get(Calendar.DAY_OF_MONTH)); 12 | System.out.println(now.get(Calendar.YEAR)); 13 | } 14 | -------------------------------------------------------------------------------- /Chapter 8/Exceptions_End.java: -------------------------------------------------------------------------------- 1 | package exceptions; 2 | 3 | import java.util.*; 4 | 5 | public class Exceptions { 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | 9 | while(true) { 10 | try{ 11 | System.out.print("Input a number: "); 12 | float input = reader.nextFloat(); 13 | System.out.println("You input the number: " + input); 14 | System.out.println("\r\n"); 15 | } 16 | catch(InputMismatchException e) 17 | { 18 | System.out.println("You passed invalid input. Not a float!"); 19 | e.printStackTrace(System.out); 20 | System.out.println("\r\n"); 21 | } 22 | finally 23 | { 24 | reader.nextLine(); 25 | } 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Chapter 8/Exceptions_Start.java: -------------------------------------------------------------------------------- 1 | package exceptions; 2 | 3 | import java.util.*; 4 | 5 | public class Exceptions { 6 | public static void main(String[] args) { 7 | Scanner reader = new Scanner(System.in); 8 | 9 | while(true) { 10 | System.out.print("Input a number: "); 11 | float input = reader.nextFloat(); 12 | System.out.println("You input the number: " + input); 13 | System.out.println("\r\n"); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Chapter 8/Strings_End.java: -------------------------------------------------------------------------------- 1 | package strings; 2 | 3 | public class Strings { 4 | 5 | public static void main(String[] args) { 6 | CustomPrinter printer = new CustomPrinter("> > %s < <"); 7 | 8 | String s1 = new String("Strings are arrays of characters"); 9 | String s2 = new String("Strings are arrays of characters"); 10 | 11 | printer.println("string1: " + s1.replace("characters", "char")); 12 | printer.println("string2: " + s2); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter 8/Strings_Start.java: -------------------------------------------------------------------------------- 1 | package strings; 2 | 3 | public class Strings { 4 | 5 | public static void main(String[] args) { 6 | String s1 = new String("Strings are arrays of characters"); 7 | String s2 = new String("Strings are arrays of characters"); 8 | 9 | System.out.println("string1: " + s1.replace("characters", "char")); 10 | System.out.println("string2: " + s2); 11 | System.out.println(s1 == s2); 12 | 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter 9/9.1/WritingToFiles_End.java: -------------------------------------------------------------------------------- 1 | package writingtofiles; 2 | 3 | import java.io.*; 4 | 5 | public class WritingToFiles { 6 | public static void main(String[] args) throws IOException { 7 | BufferedWriter out = null; 8 | 9 | try { 10 | out = new BufferedWriter(new FileWriter("out.txt", true)); 11 | 12 | for(long number : FibonacciNumbers()) 13 | { 14 | out.write(String.valueOf(number) + "\r\n"); 15 | //System.out.println(number); 16 | } 17 | } 18 | catch(IOException e) { 19 | System.err.println("File IO Failed."); 20 | } 21 | finally{ 22 | out.close(); 23 | } 24 | } 25 | 26 | private static long[] FibonacciNumbers() 27 | { 28 | long[] fibNumbers = new long[50]; 29 | fibNumbers[0] = 0; 30 | fibNumbers[1] = 1; 31 | for(int i = 2; i < 50; i++) 32 | { 33 | fibNumbers[i] = fibNumbers[i - 1] + fibNumbers[i - 2]; 34 | } 35 | return fibNumbers; 36 | } 37 | } -------------------------------------------------------------------------------- /Chapter 9/9.1/WritingToFiles_Start.java: -------------------------------------------------------------------------------- 1 | package writingtofiles; 2 | 3 | public class WritingToFiles { 4 | public static void main(String[] args) throws IOException { 5 | for(long number : FibonacciNumbers()) 6 | { 7 | System.out.println(number); 8 | } 9 | } 10 | 11 | private static long[] FibonacciNumbers() 12 | { 13 | long[] fibNumbers = new long[50]; 14 | fibNumbers[0] = 0; 15 | fibNumbers[1] = 1; 16 | for(int i = 2; i < 50; i++) 17 | { 18 | fibNumbers[i] = fibNumbers[i - 1] + fibNumbers[i - 2]; 19 | } 20 | return fibNumbers; 21 | } 22 | } -------------------------------------------------------------------------------- /Chapter 9/9.2/InputAndOutput_End.java: -------------------------------------------------------------------------------- 1 | package inputandoutput; 2 | 3 | import java.io.*; 4 | 5 | public class InputAndOutput { 6 | public static void main(String[] args) throws IOException { 7 | File outFile = new File("OutputFile.txt"); 8 | File inFile = new File("InputFile.txt"); 9 | 10 | FileWriter out = new FileWriter(outFile); 11 | BufferedReader in = new BufferedReader(new FileReader(inFile)); 12 | 13 | //Code Here... 14 | String input = ""; 15 | String newInput; 16 | while((newInput = in.readLine()) != null) 17 | { 18 | input += (newInput + "\r\n"); 19 | } 20 | out.write(input); 21 | 22 | out.close(); 23 | in.close(); 24 | } 25 | } -------------------------------------------------------------------------------- /Chapter 9/9.2/InputAndOutput_Start.java: -------------------------------------------------------------------------------- 1 | package inputandoutput; 2 | 3 | import java.io.*; 4 | 5 | public class InputAndOutput { 6 | public static void main(String[] args) throws IOException { 7 | File outFile = new File("OutputFile.txt"); 8 | File inFile = new File("InputFile.txt"); 9 | 10 | FileWriter out = new FileWriter(outFile); 11 | FileReader in = new FileReader(inFile); 12 | 13 | //Code Here... 14 | 15 | out.close(); 16 | in.close(); 17 | } 18 | } -------------------------------------------------------------------------------- /Chapter 9/9.3 End/Car.java: -------------------------------------------------------------------------------- 1 | package serialization; 2 | 3 | import java.io.*; 4 | 5 | public class Car implements Serializable { 6 | public String vin; 7 | public String make; 8 | public String model; 9 | public String color; 10 | public int year; 11 | 12 | public Car(String vin, String make, String model, String color, int year) 13 | { 14 | this.vin = vin; 15 | this.make = make; 16 | this.model = model; 17 | this.color = color; 18 | this.year = year; 19 | } 20 | 21 | @Override 22 | public String toString() 23 | { 24 | return String.format("%d %s %s %s, vin:%s", 25 | year, color, make, model, vin); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter 9/9.3 End/DeSerialize.java: -------------------------------------------------------------------------------- 1 | package serialization; 2 | 3 | import java.io.*; 4 | 5 | public class DeSerialize { 6 | public static void main(String argv[]) { 7 | Car c = null; 8 | 9 | try { 10 | FileInputStream inFile = new FileInputStream("serialized.dat"); 11 | ObjectInputStream in = new ObjectInputStream(inFile); 12 | c = (Car)in.readObject(); 13 | in.close(); 14 | inFile.close(); 15 | } 16 | catch(IOException e) 17 | { 18 | System.err.println("ERROR"); 19 | } 20 | catch(ClassNotFoundException e) 21 | { 22 | System.err.println("ERROR"); 23 | } 24 | 25 | System.out.println(c.toString()); 26 | } 27 | } -------------------------------------------------------------------------------- /Chapter 9/9.3 End/Serialize.java: -------------------------------------------------------------------------------- 1 | package serialization; 2 | 3 | import java.io.*; 4 | 5 | public class Serialize { 6 | public static void main(String argv[]) { 7 | Car c = new Car("FDAJFD54254", "Nisan", "Altima", "Green", 2000); 8 | 9 | try { 10 | FileOutputStream outFile = new FileOutputStream("serialized.dat"); 11 | ObjectOutputStream out = new ObjectOutputStream(outFile); 12 | out.writeObject(c); 13 | out.close(); 14 | outFile.close(); 15 | } 16 | catch(IOException e) 17 | { 18 | System.err.println("ERROR"); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /Chapter 9/9.3 Start/Car.java: -------------------------------------------------------------------------------- 1 | package serialization; 2 | 3 | public class Car { 4 | public String vin; 5 | public String make; 6 | public String model; 7 | public String color; 8 | public int year; 9 | 10 | public Car(String vin, String make, String model, String color, int year) 11 | { 12 | this.vin = vin; 13 | this.make = make; 14 | this.model = model; 15 | this.color = color; 16 | this.year = year; 17 | } 18 | 19 | @Override 20 | public String toString() 21 | { 22 | return String.format("%d %s %s %s, vin:%s", 23 | year, color, make, model, vin); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fundamentals of Java 2 | > Code snippets for the "Fundamentals of Java" course, available at Pirple.com 3 | 4 | 5 | ## This course is now available 6 | This course, like all others, is included with your pirple.com monthly membership. To join Pirple, visit our membership page here: 7 | 8 | [https://www.pirple.com/pages/membership](https://www.pirple.com/pages/membership) 9 | --------------------------------------------------------------------------------