Practical Exercise 9 using BlueJ

Note that this exercise is based on Barnes and Koelling, Objects First, Chapters 8 and 9.

Before starting these exercises copy (right click then save as) projects.zip to your home directory (e.g.Z:\My Documents) and unzip it.

Dome2

  1. Open dome-v2. Note the class diagram. Remove the "extends Item" phrase from CD. Note the changes in the class diagram.
  2. Create a Database
  3. Create a CD object. Test the methods
  4. Create a DVD object
  5. Try to list the database. What happens?
  6. Fix the problem and carefully note the contents of the listing.
  7. In what way are the contents incomplete?

Static and Dynamic Types

  1. Try moving the print methods from Item to the subclasses.
  2. Compile and note that there are two kinds of error (a) Item has no print method and (b) errors in the subclasses which cannot access the superclass fields.
  3. The first problem can be fixed giving Item a print method that just prints the information common to both CDs and DVDs. The second problem can be fixed by eliminating references to superclass fields from the print methods in the subclasses.
  4. Try to arrange the print methods in both superclass and the 2 subclasses so that the project compiles, and observe the results.
  5. Invoke a "super" call in the print method for the subclass in order to print the information successfully.