Question

ris as Write a program that prints the verses of the song The Twelve days of Christmas in which each verse adds one line. The user should enter one of the twelve days and the code should output the song, starting from the first verse Here is the final verse for reference 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 You will need: 1. An assignment statement to store the day entered 2. A while loop for input validation. The user should ONLY be able to enter a day between 1 and 12 3. A second while loop to output the verses 1. In this loop you will use two switch statements 1. The first switch statement should add the appropriate suffix to the day. (Day 1 should be 1st, day 2 should be 2nd, day 3 should be 3rd, and all other days should en with th.) 2. The second switch statement should output the gift for the particular day. You should utilize the fall through method for this switch statement (in order words, dont use the break statement in this one.) Please stick to the format shown in the final verse and in the sample output. All gifts should appear on a new line

import java.util.Scanner;

public class TwelveDays
{
   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
      {


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

//Begin 2nd while
      {
         //Output: "On the" + day


         //Output the suffix for the day

         //Begin 1st switch

         {


         }

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

         //Output the gift

         //Begin 2nd switch

         {


           }

         //Increment the loop control variable
    }
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/**
* The java program TwelveDays that prompts user to enter
* last day number then prints the verse of christmas song
* to console.
* */
//TwelveDays.java
import java.util.Scanner;
public class TwelveDays
{
   public static void main(String[] args)
   {
       final int MAX = 12;
       int lastDay = 0; //last day for the song, user will update
       //create a scanner class object
       Scanner scanner = new Scanner(System.in);

       //Get the last day and use input validation
       do
       {
           //prompt for lastDay value
           System.out.println("Enter last day number [1-12]");
           lastDay=Integer.parseInt(scanner.nextLine());

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


       //set lastDay to day
       int day = lastDay;    

       System.out.print("On the ");
       //first switch case
       switch(day)
       {
       case 1:
           System.out.print("first ");
           break;
       case 2:
           System.out.print("second");
           break;
       case 3:
           System.out.print("third");
           break;
       case 4:
           System.out.print("fourth");
           break;
       case 5:
           System.out.print("fifth");
           break;
       case 6:
           System.out.print("sixth");
           break;
       case 7:
           System.out.print("seventh");
           break;
       case 8:
           System.out.print("eighth");
           break;
       case 9:
           System.out.print("ninth");
           break;
       case 10:
           System.out.print("tenth");
           break;
       case 11:
           System.out.print("eleventh");
           break;
       case 12:
           System.out.print("twelfth");
           break;

       }
       System.out.println(" day of Christmas, my true love sent to me:");

       //second switch case to select gift of 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.print("Two Turtle Doves");

       case 1:
           if(day>1){
               System.out.println(",and ");
           }
           System.out.println("A Partridge in a Pear Tree.");
           break;

       }
   }
}//end of the class

------------------------------------------------------------------------------------------------------

Sample output:

Enter last day number [1-12]
12
On the twelfth day of Christmas, my true love sent 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.


sample run2"

Enter last day number [1-12]
11
On the eleventh day of Christmas, my true love sent to me:
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.

Add a comment
Know the answer?
Add Answer to:
import java.util.Scanner; public class TwelveDays {    public static void main(String[] args)    {       final...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; public class Lab6d { public static void main(String[] args) {    Scanner scnr =...

    import java.util.Scanner; public class Lab6d { public static void main(String[] args) {    Scanner scnr = new Scanner(System.in); // TODO: get user choice    // TODO: call printTable method passing choice as the parameter    } public static void printTable(int stop) { // TODO: print header    // TODO: loop to print table rows up to stop value    } Write a Java program where the main () method prompts the user to select an integer value between 1 and...

  • import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {       ...

    import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);               Mileage mileage = new Mileage();               System.out.println("Enter your miles: ");        mileage.setMiles(input.nextDouble());               System.out.println("Enter your gallons: ");        mileage.setGallons(input.nextDouble());               System.out.printf("MPG : %.2f",mileage.getMPG());           } } public class Mileage {    private double miles;    private double gallons;    public double getMiles()...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

  • import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {...

    import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {        String name;        String answer;        int correct = 0;        int incorrect = 0;        Scanner phantom = new Scanner(System.in);        System.out.println("Hello, What is your name?");        name = phantom.nextLine();        System.out.println("Welcome " + name + "!\n");        System.out.println("My name is Danielle Brandt. "            +"This is a quiz program that...

  • Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args)...

    Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • import java.util.Scanner; public class creditScore { public static void main(String[]args) { // declare and initialize variables...

    import java.util.Scanner; public class creditScore { public static void main(String[]args) { // declare and initialize variables int creditScore; double loanAmount,interestRate,interestAmount; final double I_a = 5.56,I_b = 6.38,I_c = 7.12,I_d = 9.34,I_e = 12.45,I_f = 0; String instructions = "This program calculates annual interest\n"+"based on a credit score.\n\n"; String output; Scanner input = new Scanner(System.in);// for receiving input from keyboard // get input from user System.out.println(instructions ); System.out.println("Enter the loan amount: $"); loanAmount = input.nextDouble(); System.out.println("Enter the credit score: "); creditScore...

  • //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args )...

    //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args ) { Scanner in = new Scanner( System.in ); LinkedList teamList = new LinkedList(); final int TEAM_SIZE = Integer.valueOf(in.nextLine()); for (int i=0; i<TEAM_SIZE; i++) { String newTeamMember = in.nextLine(); teamList.append(newTeamMember); } while (in.hasNext()) { String removeMember = in.nextLine(); teamList.remove(removeMember); }    System.out.println("FINAL TEAM:"); System.out.println(teamList); in.close(); System.out.print("END OF OUTPUT"); } } =========================================================================================== //PoD import java.util.NoSuchElementException; /** * A listnked list is a sequence of nodes with...

  • import java.util.Scanner; class Main { public static void main(String[] args) { Scanner inp = new Scanner(System.in);...

    import java.util.Scanner; class Main { public static void main(String[] args) { Scanner inp = new Scanner(System.in); System.out.print("In:"); int nn = inp.nextInt(); int n0, n1, n2, n3, n4;    //Change only the code below //Set n0, n1, . . . n4 to each digits in nn by replacing //the blanks _A_, _B_, ect //Hint: the ones digit of 1234 is 1234%10 // the hundredth digit of 31415 is (31415/100)%10 n0 = _A_; n1 = _B_; n2 = _C_; n3 = _D_;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT