Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* * Write an application that prints the verses * to The Twelve Days of Christmas. On the 12th day of Christmas, my true love

/* * Write an application that prints the verses * to "The Twelve Days of Christmas". On the 12th day of Christmas, my true love sent to me: Twelve Drummers Drumming Eleve Pipers Piping Ten Lords a Leaping Nine Ladies Dancing Eight Maids a Milking Seven Swans a Swimming Six Geese a Laying Five Golden Rings Four Calling Birds Three French Hens Two Turtle Doves and a Partridge in a Pear Tree * A switch statement will be the best way to do * this. You will need to order your cases very * carefully and avoid break statements in most, * but not all, places. You will also need another * switch statement for "1st", "2nd", "3rd", etc., * but this one will not require as many cases as * the other if you can use string concatenation. * */ import java.util.Scanner; public class TwelveDays { public static void main(String[] args) throws Exception{ // This section determines how many "days of Christmas" // to be printed int days; Scanner scan = new Scanner(System.in); // Get the number of days from the user System.out.print("How many days should we do? (1 to 12): "); days = scan.nextInt(); System.out.println(); // This is here in case the user inputs a number // less than 1 or greater than 12 if (days < 1) days = 1; if (days > 12) days = 12; // This is a "for loop", and the code within will execute // 1 to 12 times, depending on the value of the // variable "days". You do not need to worry about // how the loop itself works. Just focus on writing the // code to execute a SINGLE verse of the song, based on // the value of the variable "i" AT THAT TIME for (int i = 1; i <= days; i++){ // YOUR CODE GOES HERE // Remember, because of the previous code, // the int variable "i" is GUARANTEED to // be at least 1 and at most 12. System.out.println(); pause(1000); } } private static void pause(int milliseconds) throws Exception{ Thread.sleep(milliseconds); } } 

Your task, then, is to work in the area of the code I specify, using the value of the int variable i. If i is equal to 1, the program will print:

On the 1st day of Christmas, my true love gave to me: A Partridge in a Pear Tree 

If i is 2, it will print:

On the 2nd day of Christmas, my true love gave to me: Two Turtle Doves and a Partridge in a Pear Tree

And so forth... In the indicated area, you will need to do the following things:

  1. Print the first line of verse i: On the _____ day of Christmas, my true love gave to me: Note that, depending on the value of the variable i, the blank will be filled with "1st", "2nd", "3rd", "4th", etc. Use a switch statement to do this. (Note that only "1st", "2nd", and "3rd" are irregular. As such, you won't necessarily need 12 cases in this switch statement.)
  2. Use another switch statement to print the appropriate lines of verse i, in descending order. This will entail setting up the order of the cases, without breaks, so that control will drop down to the appropriate case inside of the switch, execute that case's code, and continue through the following cases, executing that code as well...until it reaches the end of the switch statement.

Remember how a switch statement works in a program:

  1. The program reaches the switch statement.
  2. The value of the content of the parentheses is determined.
  3. The program goes down through each of the cases until it reaches either a case with a matching value or the default case -- if either exists. (Otherwise, it may just reach the end of the switch statement, exit the switch statement altogether, and continue with the rest of the program.)
  4. The program goes down from there, executing every statement until it reaches either a break or the end of the switch statement.

Note that because of user input, the output will look different each time you run the program. To this end, I have provided multiple examples of how the output could look.

Program output examples:

Example 1:

How many days should we do? (1 to 12): 3 On the 1st day of Christmas, my true love gave to me: A Partridge in a Pear Tree On the 2nd day of Christmas, my true love gave to me: Two Turtle Doves and a Partridge in a Pear Tree On the 3rd day of Christmas, my true love gave to me: Three French Hens Two Turtle Doves and a Partridge in a Pear Tree

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions