- (20%) Implement a method called
isFileThere(String fileName)
which returns true if eitherfileName
exists (is the name of a file in the filesystem) orfilename.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; }
- (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
- (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 classpublic class Person { public Person mother; public Person father; /** True if person had the X-Gene */ public boolean xgene; }
where eithermother
orfather
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).
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; }
- (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 anArrayList
to implement yourSet
.
TheSet
will implement 3 methods:- a no-argument constructor (Tip: see Listing 12.10 in your textbook)
- an
add(...)
method which adds a new element to the set, if its not already there, - a
toString()
method which turns the whole set into a string, with a dash '-' between each element.
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; } }
Tuesday, November 23, 2010
Test 3 Solutions
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment