Answer in Java please

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter4: Selection Structures
Section4.3: Nested If Statements
Problem 1E
icon
Related questions
Topic Video
Question

Answer in Java please 

PP 6.10 Write a program that prints the verses of the song "The Twelve
Days of Christmas," in which each verse adds one line. The first
two verses of the song are:
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.
Use a switch statement in a loop to control which lines get
printed. Hint: Order the cases carefully and avoid the break
statement. Use a separate switch statement to put the appropri-
ate suffix on the day number (1st, 2nd, 3rd, etc.). The final verse
of the song involves all 12 days, as follows:
On the 12th day of Christmas, my true love gave to me
Twelve drummers drumming,
Eleven 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.
Transcribed Image Text:PP 6.10 Write a program that prints the verses of the song "The Twelve Days of Christmas," in which each verse adds one line. The first two verses of the song are: 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. Use a switch statement in a loop to control which lines get printed. Hint: Order the cases carefully and avoid the break statement. Use a separate switch statement to put the appropri- ate suffix on the day number (1st, 2nd, 3rd, etc.). The final verse of the song involves all 12 days, as follows: On the 12th day of Christmas, my true love gave to me Twelve drummers drumming, Eleven 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.
Expert Solution
Step 1

import java.util.Scanner;

// Create the class name of Twelve Days

public class TwelveDays

{

 // Create a main method of the program

   public static void main(String[] args)

   {

 

       final int MAX = 12;

     

  

       int lastDay = 0; // last day for the song, user will update

     

       Scanner scan = new Scanner(System.in);

     

       //Get the last day and use input validation

 

     //Begin 1st while

       // Here starting with do while loop

       // Because initially one-time user

       //enter in the  Christmas day

       do {

         

       //The user should ONLY be able to

       //enter a day between 1 and 12.

           System.out.print("How many days (1 to 12)? ");

           lastDay = scan.nextInt();

       }

       //A while loop for input validation.

       while (lastDay < 1 || lastDay > 12);

      

       int day = 1; // loop control variable for song verses

 

     //Begin 2nd while

       // A second while loop to output the verses

       while (day <= lastDay)

       {

     //Output: "On the" + day

 

 

        //Output the suffix for the day

       // (Day 1 should be 1st,

       //day 2 should be 2nd,

       //day 3 should be 3rd,

       //and all other days should end with “th”. )

           String string1="On the ";

           String string2=" day of Christmas my true love gave to me";

           String day1="st";

           String day2="nd";

           String day3="rd";

           String otherdays="th";

           // In this loop we use two switch statements.

         //Begin 1st switch

           //The first switch statement should add

           //the appropriate suffix to the day.

           switch (day)

           {

           case 1: {

               System.out.println(string1+day+day1+string2);

               break;

           }

           case 2: {

               System.out.println(string1+day+day2+string2);

               break;

           }

           case 3: {

               System.out.println(string1+day+day3+string2);

               break;

           }

 

           case 4: {

               System.out.println(string1+day+otherdays+string2);

               break;

           }   case 5: {

               System.out.println(string1+day+otherdays+string2);

               break;

           }   case 6: {

               System.out.println(string1+day+otherdays+string2);

               break;

           }   case 7: {

               System.out.println(string1+day+otherdays+string2);

               break;

           }   case 8: {

               System.out.println(string1+day+otherdays+string2);

               break;

           }   case 9: {

               System.out.println(string1+day+otherdays+string2);

               break;

           }   case 10: {

               System.out.println(string1+day+otherdays+string2);

               break;

           }   case 11: {

               System.out.println(string1+day+otherdays+string2);

               break;

           }   case 12: {

               System.out.println(string1+day+otherdays+string2);

               break;

           } 

 

           }

 

      //Output " day of Christmas my true love gave to me "

 

           //Output the gift

 

           //Begin 2nd switch

           // The second switch statement should output

           //the gift for the day.

           switch (day)

 

           {

 

           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, and");

           }

           //  don’t use the break statement in this one

           // Using default

           default: {

               System.out.println("A partridge in a pear tree.\n\n");

           }

           }

           // Increment the loop control variable

           day++;

       }

   }

}

 

 

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr