Name______________________________

Data Structures and Algorithms I - Lab 1

This lab covers basic concepts related to inheritance. You should print a copy of this lab so you can record your answers to questions. Turn in the completed questions and any supporting printouts as required in the lab. Here is a link to the API documentation in case you need it. You may use jGrasp, Eclipse, or your favorite Java development environment. Create a project (or more than one in some cases) for each lab and organize your work in appropriately named folders. You can either add existing files to the project, or copy and paste the source code into an empty file.

Start with the following class file: Card.java. You will be adding 3 new class definitions inside this file; usually each class definition goes in its own file, but we will simplify the structure for the lab and write these classes inside the same file. Note that these cannot be public classes. We will give them package scope.

You are extending class Card. Additional data required in each class is shown in the second column. Write definitions for each of the subclasses. For each subclass, supply the required additional private instance variables. All are of type int. Do not add any methods or constructors yet!

Class

Data

IDCard

ID number

CallingCard

Card number, PIN

DriverLicense

Expiration year

Test that you can compile and run. What is the name of the method that is used to assemble the individual card information that appears in the output? In what class is this method found?

Answer: ___________________________________________________

Delete the default (parameterless) constructor from the Card class and try the program again. What compile error do you get and on what line (just the first error will be sufficient)? Why is a default constructor in the Card class required even though there is no statement that would directly use it, such as new Card()?

Answer:

 

 

Implement one constructor for each of the three subclasses. The constructor will require specific information for each field belonging to its object. Remember that the parent's fields are also present in these objects. Each constructor should call the superclass constructor to set the cardholder's name. The first constructor is done for you:

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

After adding the three constructors (you should not change anything in main yet), try to compile the Card class. You will get the following error (if you get errors in the child classes, you must correct those first):

Card.java: cannot find symbol
   symbol : constructor IDCard()
   location: class IDCard
   cards.add(new IDCard());

What causes the error? Adding explicit constructors to the classes removes the default constructor that is always supplied automagically. With the new constructors, these child objects cannot be instantiated without additional information. Modify the test program to use the new constructors to create cards with names and data. You can make up information to be used in the new objects. Be sure the program runs correctly.

Next, modify the println argument in main to use the format() method that is found in Card. Test! The output will be very similar to what was displayed before, just a small difference will allow you to conclude that the format method was used (instead of toString). What difference proves this?

Answer:

Override the format method for each of the three subclasses. The methods should return a formatted description (as a String) of the card details. The subclass methods must call the superclass format method to get the formatted name of the cardholder, then append additional information. Implement the format methods so the output is appears like this:

Item 0: Card holder: Card H. Older
Item 1: Card holder: ID, ID: 34
Item 2: Card holder: CC, Card Number: 12, PIN: 34
Item 3: Card holder: DL, Exp Date: 2008

Draw a UML diagram for the Card class and its children.

Answer:










Add another file to your project named Billfold.java. In this file, devise a class, Billfold , which can hold any number of Cards. You will need to add a data member. Complete the two methods in this class and modify main to test your billfold.

Explain how polymorphism is used in the formatCards method of Billfold. Be specific, stating where in the code it is needed.

Answer:

 

 

 

The Card superclass defines a method isExpired, which always returns false . This method is not appropriate for the driver license. Override this in the DriverLicense class to return true if the driver license is already expired (i.e., the expiration year is less than the current year). You will need to use the Calendar class (and two methods, getInstance() and get(Calendar.YEAR)). Look these up in the API!

The other two Card subclasses do not expire. Do you need to make additional modifications to the classes so the isExpired method works for every subclass of Card? Explain what you need to do or why no modifications are needed:

Answer:

 

 

Add a method getExpiredCardCount(), which counts (and returns) the number of expired cards in the billfold, to the Billfold class. Modify the test code so there are expired and unexpired cards, and still cards of every type, in your billfold. Add a line to your test harness to check that this new method works correctly. Test!

Define toString methods for the three subclasses. The methods should print the class name and all instance members (including instance members of the superclass). Typical format: PhoneCard[name=Bjarne Stroustrup][number=4156646425,pin=2234]

Add a toString method to the Billfold class that returns a String representing all of the cards in the Billfold; each card followed by a newline character. Add a line to the test harness to test this new toString method.

Print the Java files for this part of the project.

You should have 2 printed files attached to this lab report. Be sure your name is included in the documentation of each program and that they are securely attached with a staple.