Test 4 Review Questions ------------------------------------- References, Parameters, and the like ------------------------------------- 1) Given the following classes: public class IntHolder { private int intHeld; public int getIntHeld() { return intHeld; } public void setIntHeld(int intHeld) { this.intHeld = intHeld; } public IntHolder(int intHeld) { setIntHeld(intHeld); } public String toString() { Integer temp = new Integer(getIntHeld()); return temp.toString(); } } public class Test { public static void squareInt (int in) { in = in * in; } public static void squareIntHolder (IntHolder in) { in.setIntHeld(in.getIntHeld() * in.getIntHeld()); } public static void main(String args[]) { int x = 5; IntHolder y = new IntHolder(5); System.out.println("x is holding: " + x); System.out.println("y is holding: " + y); squareInt(x); squareIntHolder(y); System.out.println("x is now holding: " + x); System.out.println("y is now holding: " + y); } } What output is produced when the class Test is run using 'java Test'? Explain? ---------------------- Exceptions ---------------------- 2) Compare and contrast checked and unchecked exceptions: 3) Given the following code: public class ExeptionExample { public static void main(String args[]) { BufferedReader br; FileReader fr; PrintWriter pr; FileWriter fw; try { fr = new FileReader("input2.txt"); br = new BufferedReader(fr); } catch (FileNotFoundException e) { System.out.println("Caught one!"); } catch (IOException e) { /* do nothing */ } try { fw = new FileWriter("output2.txt"); pr = new PrintWriter(fw); } catch (FileNotFoundException fnfe) { System.out.println("Caught two!"); } catch (IOException e) { /* do nothing */ } finally { System.out.println("I'm late!"); } } } and the following directory listing: sanjay@griff:/home/sanjay/test$ ls input.txt output.txt ExceptionExample.class What is the output when ExceptionExample is run? ------------------ Useful Tools ------------------ 4) What methods are defined by the Enumeration interface? Describe what each of these methods does. e.hasMoreElements(); nextElement(); 5) Implement the method tokenize which will take in a string of input and a string of delimiters and output an Enumeration of tokens contained in the input. You may find such classes as StringTokenizer, Enumeration, and Vector helpful. public Enumeration tokenize(String input, String delim) { StringTokenizer st = new StringTokenizer(input); Vector v = new Vector(); while(st.hasMoreTokens()){ v.add(st.nextToken(delim)); } return v.elements(); } -------------------- Binary Search Trees -------------------- Given the following BST: 17 / \ / \ / \ 9 21 / \ / \ 7 12 18 30 / \ 3 15 / 13 6) What would be the output of a post-order traversal? 3,7,13,15,12,9,18,30,21,17 7) An in-order traversal? 3,7,9,12,13,15,17,18,21,30 8) A pre-order traversal? 17,9,7,3,12,15,13,21,18,30 9) a. Draw the resulting tree if the number 10 were added b. Draw the resulting tree if the number 22 were added to the original tree c. Draw the resulting tree if the number 32 were added to the original tree d. Describe the algorithm you used to arrive at your conclusion. --------------------------- Hashtables --------------------------- 10) What is a hashtable? 11) What are the advantages of a hashtable over other data structures? (i.e.Why would you ever use this?) 12) A hashtable resembles a combination of two data structures. What are those data structures? For the following questions, assume you have all the methods from p3: Write the following methods ... 13) public boolean contains(Object key){ /* Returns true if the data with the given key is found in the hashtable */ /* returns false otherwise. */ int iKey = Math.abs(key.hashCode())%hTable.length(); if(hTable[iKey] != null){ return true; } else {return false;} } 14) public int numberOfElements(){ /* Returns the a count of the number of nodes in the hashtable */ int count = 0; for(int i=0; i