145 Test 3: Fall 2009
- (20%) What does the following program print out?
    
public class MyExceptions { public int trySomething(int x) throws Exception{ if (x < 0) { throw new Exception("X is less than 0"); } return 5; } public static void main(String[] args){ MyExceptions m = new MyExceptions(); try { System.out.println("Starting"); int result = m.trySomething(10); System.out.println("Got " + result); result = m.trySomething(- 10); System.out.println("Got " + result); System.out.println("No more tries"); } catch (Exception e){ System.out.println(e.getMessage()); } System.out.println("Done"); } }Answer:
Starting Got 5 X is less than 0 Done
 - (10%) In the program above, what would happen with we
      removed the whole 
catchblock? Would the program compile or not? If you think it would not compile then explain what the compiler complains about. If you think is would compile then show what the program would print out when run.
Answer: It does not compile. You cannot have atrywithout acatch.
 - (10%) Implement a class called 
Employee, with data membersString nameandint salary. Make sure that instances of yourEmployeecan be written out to a binary file. But, do not write the code to write instances to a file, just make sure that instances ofEmployeecan be written out to a binary file.
Answer:
public class Employee implements Serializable { private String name; private int salary; /** Add other methods, as needed. */ }
 - (20%) Given the following text file, called data.txt:
123432 3.14151 Ringo
write a program that reads those three values from the text file into an integer, double, and string variables, respectively.
Answer:
import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class ReadFile { public static void main (String[] args){ String fileName = "data.txt"; try { Scanner inputStream = new Scanner(new File(fileName)); int i = inputStream.nextInt(); inputStream.nextLine(); double d = inputStream.nextDouble(); inputStream.nextLine(); String s = inputStream.next(); System.out.println("i=" + i + "\nd=" + d + "\ns=" + s); } catch (FileNotFoundException e){ System.out.println("File Not Found"); } } }
 - (20%) A fast way of finding the greatest common divisor (
gcd) of two numbers x and y is by using the following recursive definition ofgcd(x,y):
the gcd(x,y) is equal to the gcd of y and x%y, the gcd(x,0) is equal to x.
Implement a recursive function that calculates the greatest common division of any two integers.
Answer:
public class GCD { /** Calculate the greatest common division, using the recursive function: gcd(x,y) = gcd(y,x%y) gcd(x,0) = x */ public static int gcd (int x, int y){ if (y == 0) return x; return GCD.gcd(y, x%y); } public static void main (String[] args){ System.out.println(GCD.gcd(10,5)); System.out.println(GCD.gcd(10,1)); System.out.println(GCD.gcd(8,6)); System.out.println(GCD.gcd(121,11)); } }
 - (20%) The following program contains two compile-time errors. What are they?
    
import java.util.ArrayList; public class Test<Type, E>{ private ArrayList<Type> list; private ArrayList<E> another; public Test(){ list = new ArrayList<E>(); another = new ArrayList<E>(); } public Type badFunction(E x, Type t){ E y = x; Type tt = t; list.add(t); another.add("Alice"); Test<String,String> justMe = new Test<String,String>(); justMe.add("Bob", "Charlie"); return tt; } }Answer:
- The line  
list = new ArrayList<E>();
is wrong because the typeEshould beTypeas per the declaration oflist.
 - The line 
another.add("Alice");because we cannot assume that the type ofanotherwill beArrayList<String>.
 
 - The line  
 
2 comments:
Couldn't you also say for number 6 that the line:
list = new ArrayListE(); (the site won't let me put the angled brackets around the E)
was wrong as it didn't match up with the previous declaration?
Yes, you are right, of course. I have now fixed the answer.
Post a Comment