Practice Questions: Week 09


Inheritance

1. Write a program that generates a random GlobalCredit using GlobalCredit.getRandom(). Print each RewardCard (if any) in the collection (but not the CreditCards). (This is basically a search for all of the RewardCards).

2. Study the Fraction, MixedFraction, and Money classes in type.lib. Write a program that outputs the result of invoking Money.getRandom() 100 times.

3. Problem 9.18 of the textbook (you can easily modify your solution to Question 2).

4. After doing Questions 2 and 3, attempt eCheck 09C; the textbook says there should be about 31 terminated statements, but you can do it in around 20. Inside your loop you should only need to use instanceof in one if-statement.

5. Write a program that generates a random Student reference called stu using Student.getRandom() inside a loop that runs 10 times. Output the result of invoking getClass on stu each time through the loop; also output the result of invoking getClass().getName() on stu.

6. See Question 5. Modify your solution to Question 5 so that it outputs just the class name (ITStudent instead of type.lib.ITStudent, for example). Consider using split or, lastIndexOf and substring in String.

7. After doing Questions 5 and 6, attempt eCheck 09D.

8. Run the following program and explain its output:

public class P09Q8
{
   public static void main(String[] args)
   {
      String s = null;
      System.out.println(s instanceof String);
   }
}

9. Compile the following program and explain the result:

public class P09Q9
{
   public static void main(String[] args)
   {
      String s = null;
      System.out.println(new P09Q9() instanceof String);
   }
}