print the song The Twelve Days of Christmas

Dell / Optiplex gx270...
October 8, 2009 at 11:27:22
Specs: Microsoft Windows XP Professional, 2.261 GHz / 2045 MB
Requirements of the program:
1) Write a Java Application that uses repetition and switch statements to print the song "The Twelve Days of Christmas."
2) One switch statement should be used to print the day (i.e. "first", "second", etc.) A separate switch statement should be used to print the remainder of each verse.
3) Show the song in a dialog box with a test area and a scroll bar instead of in the console. Code for this requirement is given as follow:
JTextArea songArea = new JTextArea(20, 30);
JScrollPane scroller = new JScrollPane(songArea);
JOptionPane.showMessageDialog(null, scroller, "Twelve Days of Christmas",

Here is what I have which doesn't meet all of the requirements

//Ravi Shah
//CIS 226
//Assignment5: 12 Days of Christmas

public class ChristmasSong {

public static void main(String[] args) {
// TODO Auto-generated method stub
int number;
String prize = "";
String day = "";
String song = "";
number = 12;

for (int j = 1; j <= number; j++)
switch (j)
case 1:
day = "First";
prize = "A Partridge in a Pear Tree \n ";

case 2:
day = "Second";
prize = "\nTwo turtle doves, \nAnd " + prize;

case 3:
day = "Third";
prize = "\nThree French Hens," + prize;

case 4:
day = "Four";
prize = "\nFour Calling Birds," + prize;

case 5:
day = "Five";
prize = "\nFive Golden Rings," + prize;

case 6:
day = "Six";
prize = "\nSix Geese a Laying," + prize;

case 7:
day = "Seven";
prize = "\nSeven Swans a Swimming," + prize;

case 8:
day = "Eight";
prize = "\nEight Maids a Milking," + prize;

case 9:
day = "Nine";
prize = "\nNine Ladies Dancing," + prize;

case 10:
day = "Ten";
prize = "\nTen Lords a Leaping," + prize;

case 11:
day = "Eleven";
prize = "\nEleven Pipers Piping," + prize;

case 12:
day = "Twelve";
prize = "\n12 Drummers Drumming," + prize;

song +="\nOn the " + day + " day of Christmas \nmy true love sent to me: " + prize;


See More: print the song The Twelve Days of Christmas

Report •

October 8, 2009 at 15:22:16
You are nearly half-way there. The day is ok (except it's fourth
not just four, etc.) You can keep the current switch statement
for selecting the day name.

Your method appears to work ok (though I haven't tested it.)
But read the question again. It says use two switch
statements. I think the reason for that is that they want you to
show a different switch structure for selecting the gifts. You
are currently using a loop and string concatenation for that.
There is a way to do it that doesn't involve a loop. I'll leave it
for you to find as a brainteaser.

Report •

October 8, 2009 at 15:55:04
uses repetition and switch statements
I swear, if the instructor is teaching the Loop-switch antipattern, someone should hit him. Repeatedly.

Report •

October 9, 2009 at 09:55:11
Razor3.2: <em>someone should hit him. Repeatedly.</em>

What, you mean with a switch? Just in case? Until he breaks?

Seriously though, I can see a legitimate use for a loop and two switch statements in this application, so I don't think he's necessarily advocating the antipattern. Very good of you to bring attention to it though. It's something everyone ought to know.

Report •

Related Solutions

Ask Question