Appendix B: Answers To Exercises
Chapter 1
Multiple Choice
- 5. All of the above
- 5. All of the above
- 3. Running the installation binary.
- 5. All of the above.
- 4. 1 and 2.
Programming Exercises
-
class HelloWorld { public static void main(String[] args) { System.out.println("Trevor Miller"); } } -
public static void main(String[] args) { for (int i = 0; i < 20; i++) { System.out.println(Math.pow(2, i+1)); } } -
public static void main(String[] args) { for (int i = 0; i < 100; i++) { System.out.println(i + "C is equal to " + ((1.8*i)+32) + "F"); } } -
public static void main(String[] args) { int x = 1; int y = 1; for (int i = 0; i < 20; i++) { System.out.print(x + " " + y + " "); x = x+y; y = x+y; } } -
public static void main(String[] args) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String in = reader.readLine(); double d = Double.parseDouble(in); System.out.println(Math.sqrt(d)); } catch (Exception e) { e.printStackTrace(); } }
Chapter 2
Multiple Choice
- 4. Java
- 3. Open Source Software
- 5. Eclipse has JUnit integrated into it.
- 5. All of the above.
- 4. 1 and 2.
Programming Exercises
-
public static void main(String[] args) { try { // Initial loan amount double A = Double.parseDouble(args[0]); // interest rate per annum double r = Double.parseDouble(args[1]); // Payment per period double P = Double.parseDouble(args[2]); // Current period in months int n = Integer.parseInt(args[3]); double i = (r / 12)/100; System.out.println("i = " + i); double amount_left = A * Math.pow((1+i),n) - (P / i) * (Math.pow((1+i),n) - 1); System.out.println(amount_left); } catch (Exception e) { e.printStackTrace(); } } -
public static void main(String[] args) { try { double total = 0.0; int num = 0; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String in = reader.readLine(); double d = Double.parseDouble(in); while (d != 0) { total += d; in = reader.readLine(); d = Double.parseDouble(in); num++; } System.out.println(total/num); } catch (Exception e) { e.printStackTrace(); } } - Similar to above
Chapter 3
Multiple Choice
- 2. Resolve naming conflicts among programmers.
- 1. Finding the classes you want is difficult.
- 3. package za.co.nephila.maculata
- 3. import za.co.nephila.maculata.ChatServer
- 3. /Maculata/src/za/co/nephila/maculata
Programming Exercises
Download here.
Chapter 4
Multiple Choice
- 2. False
- 5. All the above
- 1. True
- 5. All the above
- 2. False
- 4. @returns
- 2. Protected
- 1. -d
- 2. javadoc -sourcepath ./src *.java
- 3. javadoc -d ./docs/api -sourcepath g:\packages\Maculata\src;g:\packages\Cruentata\src nephila.maculata nephila.cruentata
Chapter 5
Multiple Choice
- 1. True
- 2. False
- 5. All the Above
- 3. jar cf jar-filename input-files
- 2. jar tf jar-filename
- 5. jar cmf seal.txt myJar.jar MyCompany
- 1. java -jar project.jar
- 4. jar xf jar-filename archived-files
- 3. Main-Class: net.sourceforge.jinit.Main
- 1. JarEntry
Programming Exercises
-
public static void main(String[] args) { try { File dir= new File("output"); JarFile jarFile = new JarFile("test.jar"); Enumeration entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = (JarEntry) entries.nextElement(); File outFile = new File(dir, entry.getName()); if (entry.isDirectory()) { outFile.mkdir(); } else { InputStream in = jarFile.getInputStream(entry); OutputStream out = new FileOutputStream(outFile); int c; while ((c = in.read()) != -1) out.write(c); in.close(); out.close(); } } jarFile.close(); } catch (Exception e) { e.printStackTrace(); } } -
public static void main(String[] args) { try { File outFile = new File(args[0]); JarOutputStream zout = new JarOutputStream(new FileOutputStream(outFile)); byte[] buf = new byte[1024]; for (int i=1; i < args.length; i++) { FileInputStream in = new FileInputStream(args[i]); zout.putNextEntry(new ZipEntry(args[i])); int len; while ((len = in.read(buf)) > 0) { zout.write(buf, 0, len); } zout.closeEntry(); in.close(); } zout.close(); } catch (Exception e) { e.printStackTrace(); } }
Chapter 6
Multiple Choice
- 4. bin
- 3. A java build tool.
- 2. Is an extensible markup language.
- 3. <?xml version="1.0" standalone="yes" encoding="UTF-8" ?>
- 1. True
- 1. True
- 5. All the above
- 1. mkdir
- 5. All the above
- 1. True
Chapter 7
Multiple Choice
- 4. All the above
- 1. True
- 1. Simplicity
- 4. All the above
- 1. True
- 3. Test - Code - Design
- 1. True
- 3. junit.framework.TestSuite
- 2. setUp
- 1. True
Chapter 8
Multiple Choice
- 1. True
- 1. True
- 5. All the above
- 2. Its destination
- 4. EXCEPTION
- 4. WriterAppender
- 4. All the above
- 2. False
- 4. All the above
- 2. log4j.debug=false
Chapter 9
Multiple Choice
- 1. True
- 4. All the above
- 6. 3 and 4
- 3. Get a new instance of a builder factory and get a new document builder from it.
- 1. True
- 2. Use the createElement method on a document object.
- 1. True
- 5. 2 and 3
- 1. SAX
- 2. <xsl:value-of select="."/>
Programming Exercises
-
public static void main(String[] args) { try { PrintWriter out = new PrintWriter(new FileOutputStream("memo.xml")); out.println("<?xml version=\"1.0\" ?>"); out.println(""); out.println("<!DOCTYPE memo SYSTEM \"memo.dtd\">"); out.println(""); out.println("<memo>"); out.println(" <to>John</to>"); out.println(" <subject>Re: Progress Meeting</subject>"); out.println(" <message>Don't forget that we must book the boardroom"); out.println(" well in advance or we'll have to have the meeting in"); out.println(" one of our offices which will turn out to be"); out.println(" <emphasis>very cramped</emphasis>."); out.println(" See you later!"); out.println(" </message>"); out.println(" <from>Trevor</from>"); out.println("</memo>"); out.close(); } catch (Exception e) { e.printStackTrace(); } } -
public class Main { public static void printNode(Node node) { if (node == null) return; int type = node.getNodeType(); if (type == Node.TEXT_NODE) { Text text = (Text)node; System.out.println(text.getWholeText()); } else if (type == Node.ELEMENT_NODE) { Element element = (Element) node; System.out.print(element.getTagName() + ": "); NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) printNode(children.item(i)); } } public static void main(String[] args) { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document document = builder.parse("memo.xml"); printNode(document.getDocumentElement()); } catch (Exception e) { e.printStackTrace(); } } }


