Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My question is 1.6, 1.8, 1.12 and 1.13 Thank you! 1.1. Consider using the follow

ID: 3679157 • Letter: M

Question

My question is 1.6, 1.8, 1.12 and 1.13

Thank you!

1.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 a hierarchy of related classes:

Class                                                         Data      

IDCard                                                        ID number

CallingCard                                                 Card number, PIN

DriverLicense                                              Expiration year

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.

Note that DriverLicense entends IDCard, not Card. Also note that this class will not compile until you complete the next sections.

1.2. Implement 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;

}

1.3. Replace 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.

1.4. Devise another class, Billfold, as below, which contains slots for two cards, card1 and card2, a method void addCard(Card) and a method String formatCards().

public class Billfold

{

private Card card1;

private Card card2;

public void ad

dCard(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.

Of course, formatCards invokes the format method on each non-null card and produces a string with the format

[card1|card2]

1.5. Write a tester programthat 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?

1.6. Explain why the output of your BillfoldTester program demonstrates polymorphism.

1.7 The Card superclass defines a method isExpired, which always returns false. This method was overridden in DriverLicense with an identical body, but the method is not appropriate for the driver license. Supply a method 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. 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)

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

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

What is your tester class?

1.11. Implement 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]

Write the code for your toString methods.

1.12. Implement 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.

1.13. Change the Card class and give protected access to name. Would that change simplify the toString method of the CallingCard class? How?

Is this change advisable?

Explanation / Answer

1.6

The 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.

Of course, formatCards invokes the format method on each non-null card and concatenates the resulting strings. This exemplifies polymorphism, since when Billfold calls card.format(), it won't know which format method will run, the one in IDCard or the one in DriverLicense or the one in CallingCard.


/**
A Billfold object holds a couple of cards
*/
public class Billfold

{
private Card card1, card2 ;
/**
Constructs a Billfold object with empty string name.
*/
public Billfold()
{
   card1 = card2 = null ;
}
/**
Mutator method to add a card to the billfold
@param card ...the card to add
*/
public void addCard(Card card1)
{
   //TODO: fill this in
}
/**
Gives a String format for all the cards in the billfold
@return the formatted String representing the cards
*/
public String format()
{
   //TODO: fill this in (in a better way)
   return "" ;
}
}


/**
CardDriver is a basic driver with a simple main method.
It tests the Card class and subclasses
*/


public class CardDriver
{
public static void main(String[] args)
{
   /*
   P1. The basic super class
   */
   Card card = new Card() ;
   System.out.println("Card format after default constructor: "
           + card.format()) ;
   card = new Card("Ilya Kuriakin") ;
   System.out.println("Card format after constructor is given a name: "
           + card.format()) ;
   /*
   P2. extending the superclass and using constructors
   */
   card = new CallingCard() ;
   System.out.println("CallingCard format after default constructor: "
           + card.format()) ;
   card = new CallingCard("Ilya Kuriakin", "999", "abcd1234") ;
   System.out.println("CallingCard format, constructor was given name, number and PIN: "
           + card.format()) ;
   /*
   P3. extending the superclass and using constructors
   */
   card = new CallingCard() ;
   System.out.println("CallingCard format after default constructor: "
           + card.format()) ;
   card = new CallingCard("Ilya Kuriakin", "999", "abcd1234") ;
   System.out.println("CallingCard format, constructor was given name, number and PIN: "
           + card.format()) ;
   /*
   P5. using the Billfold
   */
   Billfold billfold = new Billfold() ;
   System.out.println("Billfold before adding any cards: "
           + billfold.format()) ;
   billfold.addCard(new CallingCard("White Horse", "555", "Wh33Ho22")) ;
   System.out.println("Billfold after adding one card: "
           + billfold.format()) ;
   billfold.addCard(new Card("Joe Schmoe")) ;
   System.out.println("Billfold after adding second card: "
           + billfold.format()) ;
   billfold.addCard(new CallingCard("Bugs Bunny", "1234", "bb1234")) ;
   System.out.println("Billfold after adding third card: "
           + billfold.format()) ;
}
}

1.8


/**
A Billfold object holds a couple of cards
*/
public class Billfold

{
private Card card1, card2 ;
/**
Constructs a Billfold object with empty string name.
*/
public Billfold()
{
   card1 = card2 = null ;
}
/**
Mutator method to add a card to the billfold
@param card ...the card to add
*/
public void addCard(Card card1)
{
   //TODO: fill this in
}
/**
Gives a String format for all the cards in the billfold
@return the formatted String representing the cards
*/
public String format()
{
   //TODO: fill this in (in a better way)
   return "" ;
}
/**
Counts the number of expired cards in the wallet
@return the number of expired cards.
*/
public int getExpiredCardCount()
{
   //TODO: fill this in (in a better way)
   return 0 ;
}
}


/**
CardDriver is a basic driver with a simple main method.
It tests the Card class and subclasses
*/


public class CardDriver
{
public static void main(String[] args)
{
   /*
   P5. overriding methods
   */
   System.out.println("-----------------------------") ;
   System.out.println("Number expired cards in Billfold after adding: ") ;
   Billfold billfold = new Billfold() ;
   System.out.println("no cards: "
           + billfold.getExpiredCardCount()) ;
   billfold.addCard(new DriverLicense("Mrugatshi", 2006)) ;
   System.out.println("one card (expiring 2006): "
           + billfold.getExpiredCardCount()) ;
   billfold.addCard(new DriverLicense("Sharif", 2004)) ;
   System.out.println("one more card (expiring 2004): "
           + billfold.getExpiredCardCount()) ;
   System.out.println("-----------------------------") ;
   System.out.println("Number expired cards in Billfold after adding: ") ;
   billfold = new Billfold() ;
   System.out.println("no cards: "
           + billfold.getExpiredCardCount()) ;
   billfold.addCard(new CallingCard("Philip", "012345", "kkk123")) ;
   System.out.println("one phone card: "
           + billfold.getExpiredCardCount()) ;
   billfold.addCard(new DriverLicense("Atesa", 2004)) ;
   System.out.println("one more card (expiring 2004): "
           + billfold.getExpiredCardCount()) ;
  
}
}

1.12
Below is the Card class with an equals method which checks that the two card objects have the same type and the same names. 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.


/**
A Card object has a name and a few methods to get the name
and format the card. It never expires.
*/
public class Card

{
private String name;
/**
Constructs a Card object with empty string name.
*/
public Card()
{
   name = "";
}
/**
Constructs a Card object with given name
@param name1 the given name
*/
public Card(String name1)
{
   name = name1 ;
}
/**
Accessor method for the name
@return the name
*/
public String getName()
{
   return name;
}
/**
Tests whether the card is expired.
@return false, since this type of card is never expired
*/
public boolean isExpired()
{
   return false;
}
/**
Gives a String format for the card
@return the formatted String representing this card
*/
public String format()
{
   return "Card holder: " + name;
}
/**
Returns a String representation of instance variables
Note the fancy way of getting the name of the class. Here
we know the class name is Card, but when subclasses run this
method we want the subclass name.
@return string denoting the object
*/
public String toString()
{
   return getClass().getName() + "[name = "
   + name + "]" ;
}
/**
Returns true if two cards have the same name and class.
Returns false if one card is null or not same class.
Note the fancy way of checking the name of the class. Here
we know the class name is Card, but when subclasses run this
method we want the subclass name.
@return true if the two objects are same type and have same
instance variable values
*/
public boolean equals(Object other)
{
   if (other == null) return false ;
   if (! getClass().equals(other.getClass())) return false ;
   Card card = (Card) other ;
   return name.equals(card.name) ;
}
}

1.13
Below is the Card class with a toString method which uses the getClass() method. The purpose of this, rather than hardcoding the name of the class as Card is so that when the subclasses call super.toString() they will get the name of their own class.


/**
A Card object has a name and a few methods to get the name
and format the card. It never expires.
*/
public class Card

{
private String name;
/**
Constructs a Card object with empty string name.
*/
public Card()
{
   name = "";
}
/**
Constructs a Card object with given name
@param name1 the given name
*/
public Card(String name1)
{
   name = name1 ;
}
/**
Accessor method for the name
@return the name
*/
public String getName()
{
   return name;
}
/**
Tests whether the card is expired.
@return false, since this type of card is never expired
*/
public boolean isExpired()
{
   return false;
}
/**
Gives a String format for the card
@return the formatted String representing this card
*/
public String format()
{
   return "Card holder: " + name;
}
/**
Returns a String representation of instance variables
Note the fancy way of getting the name of the class. Here
we know the class name is Card, but when subclasses run this
method we want the subclass name.
@return string denoting the object
*/
public String toString()
{
   return getClass().getName() + "[name = "
   + name + "]" ;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote