Java Workshop
 

Appendix B: Answers To Exercises

Chapter 1

Multiple Choice

  1. 5. All of the above
  2. 5. All of the above
  3. 3. Running the installation binary.
  4. 5. All of the above.
  5. 4. 1 and 2.

Programming Exercises

  1. class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Trevor Miller");
        }
    }
                            
  2. public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
    	    System.out.println(Math.pow(2, i+1));
        }
    }
                            
  3. 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");
        }
    }
                            
  4. 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;
        }
    }
                            
  5. 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

  1. 4. Java
  2. 3. Open Source Software
  3. 5. Eclipse has JUnit integrated into it.
  4. 5. All of the above.
  5. 4. 1 and 2.

Programming Exercises

  1. 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();
        }
    }
                        
  2. 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();
        }
    }
                            
  3. Similar to above

Chapter 3

Multiple Choice

  1. 2. Resolve naming conflicts among programmers.
  2. 1. Finding the classes you want is difficult.
  3. 3. package za.co.nephila.maculata
  4. 3. import za.co.nephila.maculata.ChatServer
  5. 3. /Maculata/src/za/co/nephila/maculata

Programming Exercises

Download here.

Chapter 4

Multiple Choice

  1. 2. False
  2. 5. All the above
  3. 1. True
  4. 5. All the above
  5. 2. False
  6. 4. @returns
  7. 2. Protected
  8. 1. -d
  9. 2. javadoc -sourcepath ./src *.java
  10. 3. javadoc -d ./docs/api -sourcepath g:\packages\Maculata\src;g:\packages\Cruentata\src nephila.maculata nephila.cruentata

Chapter 5

Multiple Choice

  1. 1. True
  2. 2. False
  3. 5. All the Above
  4. 3. jar cf jar-filename input-files
  5. 2. jar tf jar-filename
  6. 5. jar cmf seal.txt myJar.jar MyCompany
  7. 1. java -jar project.jar
  8. 4. jar xf jar-filename archived-files
  9. 3. Main-Class: net.sourceforge.jinit.Main
  10. 1. JarEntry

Programming Exercises

  1. 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();
        }
    }
                        
  2. 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

  1. 4. bin
  2. 3. A java build tool.
  3. 2. Is an extensible markup language.
  4. 3. <?xml version="1.0" standalone="yes" encoding="UTF-8" ?>
  5. 1. True
  6. 1. True
  7. 5. All the above
  8. 1. mkdir
  9. 5. All the above
  10. 1. True

Chapter 7

Multiple Choice

  1. 4. All the above
  2. 1. True
  3. 1. Simplicity
  4. 4. All the above
  5. 1. True
  6. 3. Test - Code - Design
  7. 1. True
  8. 3. junit.framework.TestSuite
  9. 2. setUp
  10. 1. True

Chapter 8

Multiple Choice

  1. 1. True
  2. 1. True
  3. 5. All the above
  4. 2. Its destination
  5. 4. EXCEPTION
  6. 4. WriterAppender
  7. 4. All the above
  8. 2. False
  9. 4. All the above
  10. 2. log4j.debug=false

Chapter 9

Multiple Choice

  1. 1. True
  2. 4. All the above
  3. 6. 3 and 4
  4. 3. Get a new instance of a builder factory and get a new document builder from it.
  5. 1. True
  6. 2. Use the createElement method on a document object.
  7. 1. True
  8. 5. 2 and 3
  9. 1. SAX
  10. 2. <xsl:value-of select="."/>

Programming Exercises

  1. 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();
        }
    }
                        
  2. 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();
            }
        }
    
    }