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

1.6) Explain why the output of your BillfoldTester program demonstrates polymorp

ID: 3695135 • Letter: 1

Question

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 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. 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) The ID card and the phone card don’t expire. What should you do to reflect this fact in your implementation?

1.9) 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 calling 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?

Explanation / Answer

1.6)

The polymorphism example. Not sure if the functionally is exactly what you need, but you can see the whole idea (I hope). See the showAllFormat() method of Billfold class.

The whole point is inside different format() methods of the DriverLicense and IDCard. Depending on the 'real' (or initially assigned) object the different method will be called even if you just only refer to 'Card' class.

NOTE: You didn't provide your DriverLicense implementation, and my is just from head. I have a bit different constructor to show this sub-classes may be totally different.

1.8)

   Hints only! I'm not coding this for you, because it looks like a "learning exercise".

You could implement the behaviour of the superclass so that an instance of Card can be expired. And override the behaviour it in the relevant subclasses where necessary. You then need to decide whether the "unexpirable" subclasses are going to use or ignore the variable in the superclass that says whether the card is expired ... and implement accordingly.

Alternatively, you could implement the superclass as an abstract class and make isExpired an abstract method.

DON'T override the isExpired method in your subclasses IDCard and PhoneCard as your parent class Card already gives you the 'don't expire' behavior. Make sure if there is a field in your parent class called isExpired then it should be private and there should be no setter for it.

I'm not going to say which is "the right way" to do this. I want you to think that through for yourself.

1.9)

  

package card;

public class Billfold {

    Card card1 = null;

    Card card2 = null;

    /**

     * Method that takes a card object as a parameter and adds it to a billfold

     * object, provided it doesn't already have 2 card objects stored

     *

     * @param card Card object that method attempts to add to a billfold

     */

    public void addCard(Card card) {

        if (card1 == null) {

            card1 = card;

        }

        else if (card2 == null) {

            card2 = card;

        }

       }

      /**

     * Method that returns the data of cards currently in the billfold

     *

     * @return Returns data on current card objects in billfold

     */

       public String formatCards() {

        String cardTemp = "";

        if (card1 != null) {

            cardTemp += card1.format();

        }

        if (card1 != null) {

           cardTemp += " " + card2.format();

        }

        return cardTemp;

    }

     /**

     * Method that counts the number of expired cards in the billfold

     *

     * @return Returns the number of expired cards in the billfold

     */

    public int getExpiredCardCount() {

        int count = 0;

        if (card1.isExpired()) {

            count++;

        }

        if (card2.isExpired()) {

            count++;

        }

        return count;

    }

}