1.

P1. Inheritance

Consider using the following Card class.

class Card
{
  public Card()
  {
     name = "";
  }
  public Card(String n)
  {
     name = n;
  }
  public String getName()
  {
     return name;
  }
  public boolean isExpired()
  {
     return false;
  }
  public String format()
  {  
     return "Card holder: " + name;
  }
  private String name;
}


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.


Answer:


2.

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:

IDCard(String name, int id)
{
  super(name);
  idNumber = id;
}


Answer:


3.

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.


Answer:


4.

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.

What is your Billfold class?


Answer:


5.

Write a class with a test program that adds two objects of different subclasses of Card to a Billfold object. Print the results of the formatCards methods.

What is the code for your test program?

(Alternatively, if you use BlueJ, explain how you set up the Billfold object.)


Answer:


6.

What is the output of your test program?


Answer:


7.

Explain why the output of your program demonstrates polymorphism.


Answer:


8.

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() that checks if the driver license is already expired (i.e., the expiration year is less than the current year).


Answer:


9.

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


Answer:


10.

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


Answer:


11.

Write a test class that populates a billfold with a phone card and an expired driver license. Then call the getExpiredCardCount method. Run your test program to verify that your method works correctly.

What is your test program?


Answer:


12.

Define toString methods for the Card class and its three subclasses. The methods should print:

   the name of the class
   the values of all instance fields (including inherited fields)

Typical formats are:

Card[name=Edsger W. Dijkstra]
PhoneCard[name=Bjarne Stroustrup][number=4156646425,pin=2234]

Give the code for your toString methods.


Answer:


13.

Define equals methods for the Card class and its three subclasses. Cards are the same if the objects belong to the same class, and if the names and other information (such as the expiration year for driver licenses) match.

Give the code for your equals methods.


Answer:


14.

Define clone methods for the Card class and its three subclasses. The cloned card should be another card of the same class, with the same information.

Give the code for your clone methods.


Answer: