Comp 170 Lab 7 - DoME (Database of Multimedia Entertainment)
The DoME program is the central example of chapters 8 and 9; it is a simple
database for a combined library of DVDs and CDs. You can create entries
of either type, and print them out (CDs first, then DVDs). I added a field star to the DVD class; you can also use it to hold several starring actors.
The version you are to work with is here. It essentially represents version 2, but with some use of inheritance for print(), and also for the keywords() method. You are to do the following:
- Create two new classes Book and Game (as in "computer game"),
like CD and DVD. I've given you an outline for Book.
- Make sure print() works for your new classes (see below).
- Make sure each of your classes has a keywords()
method that returns the most important names (author, publisher, etc)
for that kind of object. See DVD for an example of how to join
multiple strings here.
- Write searchAll(String srchkey) in Database.java that searches the title and
keywords() (theItem.getTitle() + " " + theItem.keywords()) for matches.
The base Item class has fields title, playingTime, gotIt,
and comment. Every derived class will thus inherit these.
In the Book class constructor, set the playingTime to 0.
Do something similar for Game. Note that this is not entirely logical.
Class Book should have an additional author field of
its own. Games should have fields for publisher and numPlayers
(an int). Create appropriate accessors for these additional fields and a print method.
There are two ways to approach the print() method:
- Have your class print all fields, using accessors from class Item for the Item-based fields. Class CD works this way.
- Have your class call "super.print()" (which is, in effect, Item.print()) at the appropriate point. Class DVD works this way.
Note that, either way, although the main program appears to be calling Item.print() for each item, in fact due to the most-specific-method
rule, the appropriate specific-class print() method is called (that is,
CD.print() for CDs, DVD.print() for DVDs, etc). Item.print() is only
called if you do so explicitly, with super.print(), or if you fail to
provide a specific-class version of print(). If you take the first
apprach above, Item.print() wouldn't have to do anything at all (though
it must still be there, as a placeholder of sorts).
For searchAll(), copy what you need from Database.searchTitle(), making appropriate changes so that we search each item's keywords() instead of getTitle(). Note that we are again using inheritance. Each class has a method keywords()
(check that it does! fix any problems!) that returns a string of
keywords appropriate for that class; for example, DVD.keywords() returns the
string director + " " + star.
Item.keywords() returns the empty string; it is in effect a
"placeholder" for the specific-class versions. Each time searchAll()
calls keywords(), while it again appears
as if Item.keywords() is called, in fact the appropriate specific-class
version (eg DVD.keywords()) is invoked. This version then returns the
appropriate keyword string for that class of object.
To create the Game class, click on the New Class... button,
cut out the contents of the template BlueJ suggests for the contents,
paste in the contents of the Book class (or whatever), and then edit appropriately.
Note that the list() method is just a special case of searchTitle()
where you're searching for the empty string (so everything matches).
Email me your completed project.