Tuesday, November 23, 2010

Test 3 Solutions

  1. (20%) Implement a method called isFileThere(String fileName) which returns true if either fileName exists (is the name of a file in the filesystem) or filename.png exists. For example, if the file is "apple" then it should return true if either "apple" or "apple.png" refer to a file in the filesystem. Return false otherwise.
    Answer:
     public static boolean isFileThere(String fileName) {
      File f = new File(fileName);
      if (f.exists()) {
       return true;
      }
      f = new File(fileName + ".png");
      if (f.exists()) {
       return true;
      }
      return false;
     }
     
  2. (20%) What does the following program print out?
    public class Exceptional {
    
     public void happyDays(int x, int y) throws Exception {
      int a[] = {4}; //array with one item.
      try {
       int d = a[x];
       if (y == 0) {
        throw new Exception("Heeeeyyyy");
       }
       System.out.println("Hello Fonzie");
      }
      catch (IndexOutOfBoundsException e) {
       System.out.println("Hello Richie");
      }
     }
    
     public static void main(String[] args) {
      Exceptional e = new Exceptional();
      try {
       e.happyDays(0,1);
       e.happyDays(1,1);
       e.happyDays(0,0);
      } catch (Exception e1) {
       System.out.println("Hello Potsie");
      }
     }
    
    }
    
    Answer: There where three different tests, each with a different answer:
    public static void main(String[] args) {
     Exceptional e = new Exceptional();
     try {
      e.happyDays(0,1);
      e.happyDays(1,1);
      e.happyDays(0,0);
     } catch (Exception e1) {
      System.out.println("Hellow Potsie");
     }
     System.out.println("-------");
     try {
      e.happyDays(1,1);
      e.happyDays(0,1);
      e.happyDays(0,0);
     } catch (Exception e1) {
      System.out.println("Hellow Potsie");
     }
     System.out.println("--------");
     try {
      e.happyDays(1,1);
      e.happyDays(0,0);
      e.happyDays(1,0);
     } catch (Exception e1) {
      System.out.println("Hellow Potsie");
     }
    }
    Hello Fonzie
    Hello Richie
    Hellow Potsie
    -------
    Hello Richie
    Hello Fonzie
    Hellow Potsie
    --------
    Hello Richie
    Hellow Potsie
      
      
  3. (30%) Implement a recursive method that returns the mutant power of a Person. The mutant power of a Person depends on whether or not that person has the X-Gene, and how many of her ancestors have the X-Gene, with maternal lineage being doubly important.
    Specifically, you are given the class
    public class Person {
     public Person mother;
     public Person father;
     /** True if person had the X-Gene */
     public boolean xgene;
    } 
    
    where either mother or father could be null, signifying that no one up that lineage has the X-Gene (ignore them). The mutantPower of a Person Y is given by the formula:
    • mutant-power(Y) = (1 if Y has X-Gene, 0 otherwise) + 2 * mutant-power(Y's mom) + mutant-power(Y's dad).
    Implement int Person.calculateMutantPower(). For example, the code below prints out xavier's mutant power which is 3.
    public static void main(String[] args) {
     Person xavier = new Person();
     xavier.xgene = true;
     Person mom = new Person();
     mom.xgene = true;
     xavier.mother = mom;
     System.out.println(xavier.calculateMutantPower()); // 3
    }
    
    Answer:
    public int calculateMutantPower () {
     int power = (xgene) ? 1 : 0;
     int motherPower = 0;
     int fatherPower = 0;
     //The base case is when both mother and father are null, in which case we just return 1.
     if (mother != null) {
      motherPower = mother.calculateMutantPower();
     }
     if (father != null) {
      fatherPower = father.calculateMutantPower();
     }
     return power + 2*motherPower + fatherPower;
    }
      
  4. (30%) Implement a class called Set which implements a mathematical set, which we define as a bunch of elements of the same type where no element appears more than once (no repeats allowed). You will use an ArrayList to implement your Set.
    The Set will implement 3 methods:
    1. a no-argument constructor (Tip: see Listing 12.10 in your textbook)
    2. an add(...) method which adds a new element to the set, if its not already there,
    3. a toString() method which turns the whole set into a string, with a dash '-' between each element.
    The following is an example usage of the Set class you will write:
    public static void main(String[] args) {
     Set<String> s = new Set<String>();
     s.add("Hi");
     s.add("Hello");
     s.add("Hi");
     s.add("Hi");
     s.add("how are you?");
     System.out.println(s); //prints Hi-Hello-how are you?-
    }
         
    Answer:
    import java.util.ArrayList;
    
    public class Set<T> {
    
     private ArrayList<T> list;
     
     public Set() {
      list = new ArrayList<T>(10); 
     }
     
     public void add(T element) {
      if (!list.contains(element)) {
       list.add(element);
      }
     }
     
     public String toString() {
      String result = "";
      for (int i=0; i < list.size();i++) {
       result += list.get(i) + "-";
      }
      return result;
     }
    }
     

No comments: