Complete Program

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

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
{
  String recipient;
  public abstract void greeting();
}

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

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

class Birthday extends Card
{
  int age;

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

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

class Valentine extends Card
{
  int kisses;

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

  public void greeting()
  {
    System.out.println("Dear " + recipient + ",/n");
    System.out.println("Love and Kisses,/n");
    for ( int j=0; j < kisses; j++ )
      System.out.print("X");
    System.out.println("/n/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 9:

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