CSE 17 Test 2 Tuesday 15 November 2005 >>>>>>>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 1. Design a class called Dictionary that is a subclass of the Book class. Create the methods in Dictionary so that word.main() produces the following output. Number of pages: 1500 Number of definitions: 52500 Defintions per page: 35.0 Note: 52500/1500 = 35.0 public class Words { public static void main(String[] args) { Dictionary webster = new Dictionary(1500,52500); // 1500 is number of pages & 52500 is number of definitions System.out.println(webster); System.out.println("Note: " + webster.getDefinitions() + "/" + webster.getPages() + " = " + webster.computeRatio()); } } class Book {protected int pages = 1500; public Book(int p) {pages = p;} public int getPages(){return pages;} public String toString(){return "Number of pages: "+ pages;} } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< class Dictionary extends Book {private int definitions; public Dictionary (int p, int numDef){ super(p); definitions = numDef;} public double computeRatio(){return definitions/pages;} public int getDefinitions(){return definitions;} public String toString() {return (super.toString() + "\nNumber of definitions: " + definitions + "\nDefintions per page: " + computeRatio());} } Name_______________________________________________________________ 2. This question has two distinct parts. Answer each part Part a) Given the following classes, state the output if in the interaction pane I typed the following: >x = new C() >x.e() public class A{ protected int j; public A(A b){j=b.j;} public A(){j=22;} public static void stat(){System.out.println("I am in A.stat"); } public void a(){j = 40; } protected void b(){j = j + 10;} void c(){j = 80;} private void d(){j = j*j;} public void e(){ stat(); System.out.println(j); a(); System.out.println(j); b(); System.out.println(j); c(); System.out.println(j); d(); System.out.println(j);} } class C extends A {public static void stat(){System.out.println("I am in C.stat");} public void a(){j = 5;} protected void b(){j = 10;} void c(){j = 30;} private void d(){j = 50;} >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> I am in A.stat 22 5 10 30 900 ---------------------------------------------- Part b) Explain the behavior of class T2 when T2.main() executes. import java.util.*; import java.io.*; public class T2 { public static void main(String[] args) { Scanner f; try {f=new Scanner(new FileInputStream("junk")); System.out.println(f.nextInt()); } catch(FileNotFoundException e) {System.out.println("Jerko"); System.exit(1); } catch(Exception e) {System.out.println("Dumbo"); System.exit(2); } } } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< If the file 'junk' does not exist, the program displays "Jerko" If the file 'junk' exists, but if there is something other than a leading digit, it displays "Dumbo" If the file 'junk' exists, and there is a leading digit, it reads in an int and displays it If any other kind of error ocurrs, the program displays "Dumbo" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 3. Write a recursive method, called max, that finds the largest number in an array of ints, using the following recursive algorithm, WHICH YOU MUST NOT CHANGE: to find the maximum of the entries from location a to location b in the array, divide the array in half, find the largest number in each half, and return the larger of these two numbers. If we have int [] x={0, 5, 6, 8, 2, 9, 20, 31, 12}; the call to find the largest of the 9 entries in the array x would be max(x,0,8); >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> public int max(int [] x,int bot, int top) {if(bot>=top) return x[bot]; return max( max(x,bot,(bot+top)/2), max(x,(bot+top)/2+1,top)); } public int max(int a,int b) {if(a>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> public boolean positive(FileInputStream f) {Scanner scan=new Scanner(f); while(scan.hasNextInt()) if(scan.nextInt()<=0) return false; return true; } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<