go to previous page   go to home page   go to next page

Answer:

Yes. All that Card asked for was:

public abstract void greeting();

Each sibling did that in its own way.


Complete Program

Here is a complete program with all three card classes and objects of each type. If you were deficient in greeting cards this year, you might wish to copy this program to a file and run it a few times.

import java.util.*;

abstract class Card
{
  protected String recipient;
  public abstract void greeting();
}

class Holiday extends Card
{
  public Holiday( String r )
  {
    recipient = r;
  }

  public void greeting()
  {
    System.out.println("Dear " + recipient + ",");
    System.out.println("Season's Greetings!\n");
  }
}

class Birthday extends Card
{
  private int age;

  public Birthday ( String r, int years )
  {
    recipient = r;
    age = years;
  }

  public void greeting()
  {
    System.out.println("Dear " + recipient + ",");
    System.out.println("Happy " + age + "th Birthday\n");
  }
}

class Valentine extends Card
{
  private int kisses;

  public Valentine ( String r, int k )
  {
    recipient = r;
    kisses    = k;
  }

  public void greeting()
  {
    System.out.println("Dear " + recipient + ",");
    System.out.println("Love and Kisses,");
    for ( int j=0; j < kisses; j++ )
      System.out.print("X");
    System.out.println("\n);
  }
}

public class CardTester
{
  public static void main ( String[] args )
  {
    String me;
    Scanner input = new Scanner( System.in );
    System.out.print("Your name: ");
    me = input.next();

    Holiday   hol = new Holiday( me );
    hol.greeting();

    Birthday  bd  = new Birthday( me, 21 );
    bd.greeting();

    Valentine val = new Valentine( me, 7 );
    val.greeting();

  }
}

QUESTION 12:

This is a fairly long program ― 80 lines! Do you think that you could design a few more card classes without any problems?


go to previous page   go to home page   go to next page