CSE 17 Test 1 Friday 17 November 2006 <<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>> 1. Assume the following class: public class Node{ public int j; public Node next; } Write a method list() for listing the contents of a linked list of Nodes in the file "test2.out". Given Node hd;, where hd points to a linked list that terminates in a null pointer, the call list(hd,"test2.out") should list the contents of the linked list in the file "test2.out". <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public static void list(Node hd,String file){ try{ PrintWriter out=new PrintWriter(new FileOutputStream(file)); while(hd!=null){ out.println(hd.j); hd=hd.next; } out.close(); }catch(FileNotFoundException e){ System.err.println("The file 'afile.out' could not be opened"); System.exit(1); } } >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 2. Write an abstract class Bound, that requires its subclasses to have functions max() and min() that are meant to return the largest (max()) and smallest (min()) ints among the data stored by the given subclass. The class Bound should also have a function, inRange(), that returns true if and only if a given int is between the smallest and largest int (inclusive) among the ints stored by the subclass. A sample call to inRange() would be inRange(6). <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public abstract class Bound{ abstract int max(); abstract int min(); boolean inRange(int k){ return k>=min() && k<=max(); } } >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 3. Write a recursive function, equal(), that returns true if and only if two int arrays have exactly the same elements. It MUST USE the following RECURSIVE algorithm. a[] and b[] are equal, starting at the jth element, if the jth elements are the same, and the rest of a[] and b[] have identical elements. Given int a[], b[];, the initial call would be equal(a,b,0). <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public static boolean equal(int a[],int b[],int loc){ if(a==null && b==null) return true; if(a==null || b==null) return false; if(a.length!=b.length) return false; if(loc>=a.length) return true; return a[loc]==b[loc] && equal(a,b,loc+1); //Instead, in one swell foop: //return a==null && b==null || // a!=null && b!=null && a.length==b.length && // (loc>=a.length || a[loc]=b[loc] && equal(a,b,loc+1)); // Gee, is that right? It is better to avoid this approach.... } >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 4. Write a sublcass, FNode, of class Node from question 1 that adds a private variable, "data", for storing a double, that has methods setData() and getData(), and that has methods that enables the following code System.out.println( new Node(5,2.2)); to display "[j=5, d=2.2]". <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< class FNode extends Node{ private double data; public FNode(int z,double d){ j=z; data=d; next=null; } public FNode setData(double d){ data=d; return this; } public double getData(){ return data; } public String toString(){ return "["+j+", "+data+"]"; } } >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>