Question: Lab 1 Inheritance Instructions: Code the classes in your IDE and submit the .java files through the Lab 1 submission link. 1. Consider using the
Lab 1 Inheritance Instructions:
Code the classes in your IDE and submit the .java files through the Lab 1 submission link.
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 the above class as a superclass to implement the following 3 subclasses:
Class Instance Field
IDCard ID number
CallingCard Card number, PIN
DriverLicense Expiration year
a. Write declarations 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.
b. Code constructors for each of the three subclasses. Each constructor should call the superclass constructor to set the name. Here is one example:
public IDCard(String n, String id)
{
super(n); idNumber = id;
}
c. Replace the code 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 then add the additional formatted instance fields. Here is an example for IDCard:
public String format()
{
return super.format() + ", ID: " + idNumber;
}
d. 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). Use the information that learned from working with dates
e. Code toString methods for the Card class and its three 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]
CallingCard[name=Bjarne Stroustrup][number=4156646425,pin=2234]
f. Code 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.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
