Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- We generally recognize inheritance with the ‘is-a’ relationship. In such cases, we would
- extend a class to derive a new class that employs the behavior of the parent class and/or
- overrides those behaviors that specific to that child class.
- This differs from the ‘has-a’ relationship. In this case, a class might use objects of a
- different type, e.g., the String class, but does not employ inheritance.
- For the following, begin by determining the proper relationship of the class you are
- constructing.
- 1. Consider using the following Card class.
- public class Card {
- private String name;
- 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;
- }
- }
- Use this class as a superclass to implement the following hierarchy of related classes
- having specific instance data:
- Class Data
- IDCard ID number String
- CreditCard Card number, PIN String
- DriverLicense Expiration year int
- Write declarations for each of the subclasses based upon the proper ‘is-a’
- relationship. For each subclass, supply private instance variables. Leave the bodies of
- the constructors and the format methods blank for now.
- 2. Implement constructors for each of the three subclasses. Each constructor must call
- the superclass constructor to set the name. Here is one example:
- public IDCard(String n, String id) {
- super(n);
- idNumber = id;
- }
- Be sure to include the data (parameters) specific to each class.
- 3. Override the implementation of the format method for each of 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.
- 4. Devise another class, Billfold, which contains slots for two cards, card1 and card2, a
- method void addCard(Card card) and a method String formatCards(). Note that this
- class employs the ‘has-a’ relationship.
- The addCard method checks whether 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.
- Of course, formatCards invokes the format method on each non-null card and
- produces a string with the format
- [card1|card2]
- What is your Billfold class?
- 5. Write a tester program that adds two objects of different subclasses of Card to a
- Billfold object. Test the results of the formatCards methods.
- What is the code for your BillfoldTester class?
- 6. The Card superclass defines a method isExpired, which always returns false. This
- method is not appropriate for the driver license. Supply a method header and body for
- DriverLicense.isExpired() that checks whether the driver license is already expired
- (i.e., the expiration year is less than the current year).
- To work with dates, you can use the methods and constants supplied in abstract class
- Calendar which are inherited by the concrete class GregorianCalendar. This class
- must be imported from the java.util package. You create a Calendar as follows:
- GregorianCalendar calendar = new GregorianCalendar();
- Then, you can obtain the current year using the constant Calendar.YEAR and method
- get in GregorianCalendar. The constant indicates that the method should return the
- current year. By comparing the returned value with the expYear field in
- DriverLicense, you can determine if the card is expired. The code below will retrieve
- the current year:
- calendar.get(Calendar.YEAR)
- 7. The ID card and the credit card don’t expire. What should you do to reflect this fact in
- your implementation?
- 8. Add a method getExpiredCardCount, which counts the number of expired cards in
- the billfold, to the Billfold class.
- 9. Write a BillfoldTester class that populates a billfold with a phone calling card and an
- expired driver license. Then call the getExpiredCardCount method. Implement the
- following tester class to verify that your method works correctly.
- public class BillfoldTester {
- public static void main(String[] args) {
- DriverLicense d = new DriverLicense("John Doe", "08-097654", 2003);
- CreditCard c = new CreditCard("Omega Card", "301233985945", 1030);
- Billfold b = new Billfold();
- b.addCard(d);
- b.addCard(c);
- System.out.println(b.formatCards());
- System.out.println("Number of expired cards: " + b.getExpiredCardCount());
- System.out.println("Expected: 1");
- }
- }
- 10. Implement toString methods for the Card class and its subclasses. The methods
- should print:
- the name of the class
- the values of all instance variables (including inherited instance variables)
- Typical formats are:
- Card[name=Edsger W. Dijkstra]
- CreditCard[name=Bjarne Stroustrup][number=4156646425,pin=2234]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement