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); songArea.setText(songString); JOptionPane.showMessageDialog(null, scroller, "Twelve Days of Christmas", JOptionPane.PLAIN_MESSAGE);
Here is what I have which doesn't meet all of the requirements
//Ravi Shah
//CIS 226
//Assignment5: 12 Days of Christmas
//10/08/09
public class ChristmasSong {
public static void main(String[] args) {
// TODO Auto-generated method stub
int number;
String prize = "";
String day = "";
String song = "";
System.out.print("");
number = 12;
System.out.println();
for (int j = 1; j <= number; j++)
{
switch (j)
{
case 1:
day = "First";
prize = "A Partridge in a Pear Tree \n ";
break;
case 2:
day = "Second";
prize = "\nTwo turtle doves, \nAnd " + prize;
break;
case 3:
day = "Third";
prize = "\nThree French Hens," + prize;
break;
case 4:
day = "Four";
prize = "\nFour Calling Birds," + prize;
break;
case 5:
day = "Five";
prize = "\nFive Golden Rings," + prize;
break;
case 6:
day = "Six";
prize = "\nSix Geese a Laying," + prize;
break;
case 7:
day = "Seven";
prize = "\nSeven Swans a Swimming," + prize;
break;
case 8:
day = "Eight";
prize = "\nEight Maids a Milking," + prize;
break;
case 9:
day = "Nine";
prize = "\nNine Ladies Dancing," + prize;
break;
case 10:
day = "Ten";
prize = "\nTen Lords a Leaping," + prize;
break;
case 11:
day = "Eleven";
prize = "\nEleven Pipers Piping," + prize;
break;
case 12:
day = "Twelve";
prize = "\n12 Drummers Drumming," + prize;
break;
}
song +="\nOn the " + day + " day of Christmas \nmy true love sent to me: " + prize;
}
System.out.println(song);
}
}

New Topic/Question
Reply



MultiQuote



|