Lab11 by wanghonghx

VIEWS: 19 PAGES: 2

									                            CPS 209 Spring 2009
                                  Lab 4
P1. Inheritance

Consider the following Card class. You can test it with the following driver: CardDriver
class.
Use this class as a superclass to implement a hierarchy of related classes:
Class                                       Data
IDcard                                      ID number
Calling Card                                Card number, PIN
DriverLicense                               Expiration year

Write definitions for each of the subclasses. For each subclass, supply private instance
variables. Leave the bodies of the constructors and the format methods blank for now.
For example, CallingCard. Note that CallingCard does not rewrite the isExpired method,
since it is inherited from Card, and it won't be changed. However, DriverLicense is a
different matter.



P2. Calling the Superclass Constructor
Implement constructors for each of the three subclasses. Each constructor should call the
superclass constructor to set the name. Here is one example: CallingCard and the driver
to try it: CardDriver



P3. Overriding Methods

Supply the implementation of the format method for the three subclasses. The methods
should produce a formatted description of the card details. The subclass methods should
call the superclass format method to get the formatted name of the cardholder. For
example, CallingCard and same driver as P2.


P4. Polymorphism

Devise another class, Billfold, which contains slots for two cards, card1 and card2, a
method void addCard(Card) and a method String formatCards().

The addCard method checks if card1 is null. If so, it sets card1 to the new card. If not,
it checks card2. If both cards are set already, the method has no effect. (In Chapter 13
you will learn how to collect an arbitrary number of items.)
Of course, formatCards invokes the format method on each non-null card and
concatenates the resulting strings. This exemplifies polymorphism, since when Billfold
calls card.format(), it won't know which format method will run, the one in IDCard or the
one in DriverLicense or the one in CallingCard.

Finish off this class: Billfold and try it out with CardDriver

Explain why the output of your program demonstrates polymorphism.



P5. Overriding methods

The Card superclass defines a method isExpired, which always returns false. This
method is not appropriate for the driver license. Supply a method
DriverLicense.isExpired() in DriverLicense that checks if the driver license is already
expired (i.e., the expiration year is less than the current year).
Note, you can get the current year as follows:

import java.util.Calendar ;    // at the top of your
program Calendar calendar = Calendar.getInstance() ; // in
your method int currentYear = calendar.get(Calendar.YEAR) ;

The ID card and the calling card don't expire. What should you do to reflect this fact in
your implementation?

Add a method getExpiredCardCount, which counts the number of expired cards in the
billfold, to the Billfold class.

Try it out with CardDriver class that populates a billfold with a phone card and an
expired driver license.

								
To top