Use the Web to locate the lyrics to the traditional song “The Twelve Days of Chr
ID: 3672766 • Letter: U
Question
Use the Web to locate the lyrics to the traditional song “The Twelve Days of Christmas.” The song contains a list of gifts received for the holiday. The list is cumulative so that as each “day” passes, a new verse contains all the words of the previous verse, plus a new item. Write a class that displays the words to the song starting with any day the user enters. (Hint: Use a switch statement with cases in descending day order and without any break statements so that the lyrics for any day repeat all the lyrics for previous days.) Save the file as TwelveDays.java.
Don’t forget to create the application/project TwelveDaysTest Class that has the main method and an object to use the TwelveDays class.
Explanation / Answer
public class TwelveDays
{
public static void main(String[] args)
{
System.out.print("Twelve Days of Christmas ");
int index;
String ordinal_numbers[] = { "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"};
for(index = 1; index <= 12; ++index)
{
System.out.print("On the " + ordinal_numbers[index-1]+ " day of Christmas, ");
switch(index)
{
case 12:
System.out.println("Twelve Drummers Drumming");
case 11:
System.out.println("Eleven Pipers Piping");
case 10:
System.out.println("Ten Lords a Leaping");
case 9:
System.out.println("Nine Ladies Dancing");
case 8:
System.out.println("Eight Maids a Milking");
case 7:
System.out.println("Seven Swans a Swimming");
case 6:
System.out.println("Six Geese a Laying");
case 5:
System.out.println("Five Golden Rings");
case 4:
System.out.println("Four Calling Birds");
case 3:
System.out.println("Three French Hens");
case 2:
System.out.println("Two Turtle Doves");
case 1:
if(index == 0)
System.out.print("A Partridge in a Pear Tree. ");
else
System.out.print("and a Partridge in a Pear Tree. ");
break;
default:
System.out.print("Internal Error.");
System.exit(1);
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.