Tuesday, December 1, 2009

Test 3

145 Test 3: Fall 2009

  1. (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
    

  2. (10%) In the program above, what would happen with we removed the whole catch block? 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 a try without a catch.

  3. (10%) Implement a class called Employee, with data members String name and int salary. Make sure that instances of your Employee can be written out to a binary file. But, do not write the code to write instances to a file, just make sure that instances of Employee can be written out to a binary file.
    Answer:
    public class Employee implements Serializable {
    
      private String name;
      private int salary;
    
      /** Add other methods, as needed. */
    
    }
    

  4. (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");
        }
      }
    
    }
    

  5. (20%) A fast way of finding the greatest common divisor (gcd) of two numbers x and y is by using the following recursive definition of gcd(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));
      }
    
    
    }
    

  6. (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:
    1. The line
      list = new ArrayList<E>();
      is wrong because the type E should be Type as per the declaration of list.
    2. The line
      another.add("Alice");
      because we cannot assume that the type of another will be ArrayList<String>.

2 comments:

Anonymous said...

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?

jmvidal said...

Yes, you are right, of course. I have now fixed the answer.