Ryerson University
School of Computer Science
CCPS209 Term Test #2 (two hours)
Fall 2005

Name:____________________________________ Student #: ______________________

There is no penalty (i.e., deduction) for incorrect answers, so you are encouraged to make a guess if you do not know the answer.

A (5 marks) For each of the following pairs (a, b) indicate whether the relationship is typically considered (a) a = superclass and b = subclass (b) a = subclass and b = superclass (c) neither, a and b are not related in this way.

  1. book, page
  2. building, house
  3. animal, horse
  4. dinosaur, Jurassic Park
  5. opium, drug
  6. finger, hand
  7. shirt, sock
B. (5 marks) Assume that
A is an abstract class.
B is an abstract subclass of A, and B implements interfaces I and J.
C is a concrete subclass of A, and C implements interface J.
B1 is a concrete subclass of B.
All of the above classes have default constructors.
A a ; B b ; C c ; I i ; J j ; Object x ; // assume these declarations
For each of the following blocks (considered in isolation) indicate if the block
(a) will not compile
(b) will compile but WILL cause an exception (run time error)
(c) will compile but MIGHT cause an exception at run time.
(d) is OK
  1. {a = new Object() ; }
  2. {a = new B() ; }
  3. {b = new I() ; }
  4. {a = new B1() ; }
  5. {x = new B1() ; }
  6. {x = new J() ; }
  7. {i = new B1() ; }
  8. {j = new B() ; }
  9. {j = new C() ; }
  10. {j = new B1() ; }
  11. {j = new Object() ; }
  12. {a = new B1() ; b = a ; c = (C)a ; }
  13. {j = new C() ; a = (A)j ;}



C.
  1. (5 marks) Write a class MinAccount which is a subclass of BankAccount. MinAccount has an interest rate and a minimum balance (along with the usual balance inherited from BankAccount). The deposit, getBalance and withdraw are the same as in BankAccount, but it has an additional method called addInterest which computes the interest on the minimum balance since the last call to addInterest. It also has an boolean equals(Object other) method which overrides the one in BankAccount, to check both the balance and the minimum balance.
  2. (5 marks) Rewrite the deposit method for the above class here so that it throws an exception if the amount of the deposit is less than 1.00.
  3. (5 marks) Write a Driver class which creates an object of type MinAccount. Then make a loop where a series of doubles is read from a file called Deposits, reading until end of file. Deposit the values read, but catch exceptions where the amount is too low. Print an error message but continue with the loop. Also catch an exception if the file Deposits does not exist, and exit gracefully with a message.
  4. (5 marks) Write a Driver class that creates a Timer object and uses it to print a random number every second. The random value should be from 0 to 399. If the value 100 is printed then the timer should stop and the program ends.
  5. (5 marks) Write a recursive method static boolean find(String s, String t) that tests whether a string t is contained in a sentence s:
          Sentence s = new Sentence("Mississippi!") ;
          boolean b = find(s, "sip") ; // returns true
    
    Hint If the text starts with the string you want to match, then you are done. If not, consider the sentence that you obtain by removing the first character.