Question: Hello could someone help with this assignment please? We are using Eclipse. Thanks !! COSC 1174 Java II Lab Chapter 11 Polymorphism You are creating
Hello could someone help with this assignment please? We are using Eclipse. Thanks !!
COSC 1174 Java II Lab
Chapter 11 Polymorphism
You are creating a Java application called Billfold. Billfold is implemented as a class that holds up to two cards. The cards can be one or more of an ID card, a debit card, or a driver license (although more than one ID card or driver license sounds a bit suspicious!). The hierarchy of inheritance for the cards are a shown below:
Card
| |
V V
IDCard DebitCard
|
V
DriverLicense
The Card class is shown below:
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 a hierarchy of related classes:
IDCard
- idNumber: String
+IDCard(name: String, idNumber: String)
+Format(): String
DebitCard
-cardNum: String
-pin: int
+DebitCard(name: String, cardNum: String, pin: int)
+Format(): String
DriverLicense
-expirationYear: int
+DriverLicense(name: String, licenseNum: String, expirationYear: int)
+Format(): String
The constructor for each of the classes implemented should call the superclass constructor to set the value of name. Here is an example:
public IDCard(String n, String id) {
super(n);
idNumber = id;
}
Implement 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.
Create another class called Billfold, which contains slots for two cards, card1 and card2, a void method called addCard(card) that adds a card to the Billfold, and a String formatCards() method. The Billfold class is shown below:
public class Billfold {
private Card card1;
private Card card2;
public void addCard(Card c) {
if (card1 == null)
card1 = c;
else if (card2 == null)
card2 = c;
}
public String formatCards() {
return "[" + card1.format() + " | " + card2.format() + "]";
}
}
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.
Implement an equals method 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 idNumber, pin, and expirationYear for driver licenses) match. You will need to use the instanceof operator for these methods.
Write a test class for Billfold that adds two different cards to a Billfold object and then prints out the contents using the formatCards methods. Add two identical cards to another Billfold object and verify they are equal using the equals method. Print the results using formatCards.
Write the following method that shuffles the elements in an ArrayList of integers. Write the class ShuffleArrayList with a main method to invoke the shuffle method. Populate the list with the numbers 0 through 9, then shuffle the list, then print out the resulting list of numbers. Write your own shuffle method (i.e., do NOT use the Collections shuffle method). You may want to review the shuffle algorithm we covered in Chapter 7, the chapter on arrays.
Public static void shuffle(ArrayList
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
