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
- Open dome-v2. Note the class diagram. Remove the "extends Item" phrase
from CD. Note the changes in the class diagram.
- Create a Database
- Create a CD object. Test the methods
- Create a DVD object
- Try to list
the database. What happens?
- Fix the problem and carefully note the contents
of the listing.
- In what way are the contents incomplete?
Static and Dynamic Types
- Try moving the print methods from Item to the subclasses.
- 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.
- 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.
- Try
to arrange the print methods in both superclass and the 2 subclasses so that the
project compiles, and observe the results.
- Invoke a "super" call in the print
method for the subclass in order to print the information successfully.