1 Replies - 29542 Views - Last Post: 08 October 2009 - 11:43 AM Rate Topic: -----

#1 idle_09   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 02-March 08

"12 days of christmas" song

Posted 08 October 2009 - 11:19 AM

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);
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 :v:
//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);
		}
		
		

	}



Is This A Good Question/Topic? 0
  • +

Replies To: "12 days of christmas" song

#2 OliveOyl3471   User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: "12 days of christmas" song

Posted 08 October 2009 - 11:43 AM

I just did this exact program in C++ not too long ago. Remember, you'll have to change the cout's to system.out.println's but the logic should be the same. You'll have to do part 3) yourself, or maybe someone else here can help you with it. Good luck! :)

#include<iostream.h>
#include<conio.h> //getch();
int main()
	{
	int i;
	for(i=1;i<=12;i++){

		cout<<"\n\nOn the ";
		switch(i){
				case 1:
				cout<<"first";
				break;
				case 2:
				cout<<"second";
				break;
				case 3:
				cout<<"third";
				break;
				case 4:
				cout<<"fourth";
				break;
				case 5:
				cout<<"fifth";
				break;
				case 6:
				cout<<"sixth";
				break;
				case 7:
				cout<<"seventh";
				break;
				case 8:
				cout<<"eighth";
				break;
				case 9:
				cout<<"ninth";
				break;
				case 10:
				cout<<"tenth";
				break;
				case 11:
				cout<<"eleventh";
				break;
				case 12:
				cout<<"twelfth";
				break;

		}
				cout<<" day of Christmas\nmy true love sent to me:"<<endl;

				switch(i)
				{
				case 12:
				cout<<"12 Drummers Drumming\n";	   
				
				case 11:
				cout<<"Eleven Pipers Piping\n";
				
				case 10:
				cout<<"Ten Lords a Leaping\n";
				
				case 9:
				cout<<"Nine Ladies Dancing\n";
				
				case 8:
				cout<<"Eight Maids a Milking\n";
				
				case 7:
				cout<<"Seven Swans a Swimming\n";
				
				case 6:
				cout<<"Six Geese a Laying\n";
				
				case 5:
				cout<<"Five Golden Rings\n";
				
				case 4:
				cout<<"Four Calling Birds\n";
				
				case 3:
				cout<<"Three French Hens\n";
				
				case 2:
				cout<<"Two Turtle Doves\n";
	
				case 1:
					 if(i>1){
					 cout<<"And ";
					 }
				cout<<"A Partridge in a Pear Tree.";
				break;

		}
		}
	getch();
	return 0;
	}

This post has been edited by OliveOyl3471: 09 October 2009 - 05:30 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1