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

Answer:

An array of Card:

Card[] cards = new Card[12];

Array of Related Objects

Card[] cards = new Card[12];

cards[0] = new YouthBirthday( "Valerie", 7 );
cards[1] = new AdultBirthday( "Walter", 47 );
cards[2] = new Birthday( "Zoe", 30 );
cards[3] = new Holiday( "Kelly" );
cards[4] = new Valentine( "Jill", 42 );

for ( int j=0; j <= 4; j++ )
  cards[j].greeting();

Any object that belongs in the hierarchy can fit into any cell of the array. This is like the greeting card display at the drug store that holds a different selection of cards in different seasons of the year.

This code creates 5 cards objects of various varieties and put them into the array. Then it asks each object in the array to write out its own version of the greeting. This works fine:

Dear Valerie,
Happy 7th Birthday
How you have grown!!

Dear Walter,
Happy 47th Birthday
You haven't changed at all!

Dear Zoe,
Happy 30th Birthday

Dear Kelly,
Season's Greetings!

Dear Jill,
Love and Kisses,
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

QUESTION 14:

Now say that this code follows the above:

Card temp;

temp     = cards[0];
cards[0] = cards[1];
cards[1] = temp;

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