4 Replies - 289 Views - Last Post: 25 December 2012 - 10:17 PM Rate Topic: -----

#1 mathematicsnerd888  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-December 12

Code will not entirely print out the recursive song in java.

Posted 24 December 2012 - 08:51 PM

I need the song to be printed out the way it would be sung. If I type in 2, it should print out:

On the first day of Christmas
My true love gave to me
A partridge in a pear tree.

On the second day of Christmas
My true love gave to me
Two turtle doves, and
A partridge in a pear tree.

Instead of just:
On the second day of Christmas
My true love gave to me
Two turtle doves, and
A partridge in a pear tree.

public class RecursiveSong 
{
	public static void main(String [] args)
	{
		final int LAST_VERSE = 12;
		String day = "";
		
		switch (LAST_VERSE) 
		{
			case 1: 
				day = "first";
				break;
			
			case 2: 
				day = "second";
				break;
			
			case 3: 
				day = "third"; 
				break;
			
			case 4: 
				day = "fourth"; 
				break;
			
			case 5: 
				day = "fifth"; 
				break;
			
			case 6: 
				day = "sixth"; 
				break;
			
			case 7: 
				day = "seventh"; 
				break;
			
			case 8: 
				day = "eighth"; 
				break;
			
			case 9: 
				day = "ninth";
				break;
			
			case 10: 
				day = "tenth"; 
				break;
			
			case 11: 
				day = "eleventh"; 
				break;
			
			case 12: 
				day = "twelth"; 
				break;
		}

		System.out.println("On the " + day + " day of Christmas" + "\nMy true love gave to me");
		
		String verse = RecursiveVerse(LAST_VERSE);
		System.out.println(verse);
		
	}

	public static String RecursiveVerse(int DAY)
	{
		if (DAY == 0)
			return "";
		
		else
		{
			switch (DAY) 
			{
				case 1: 
					System.out.println("A partridge in a pear tree"); 
					break;
				
				case 2: 
					System.out.println("Two turtle doves, and"); 
					break;
				
				case 3: 
					System.out.println("Three French Hens "); 
					break;
				
				case 4: 
					System.out.println("Four calling birds"); 
					break;
				
				case 5: 
					System.out.println("Five golden rings"); 
					break;
				
				case 6: 
					System.out.println("Six geese a laying"); 
					break;
				
				case 7: 
					System.out.println("Seven swans a swimming"); 
					break;
				
				case 8: 
					System.out.println("Eight maids a milking"); 
					break;
				
				case 9: 
					System.out.println("Nine ladies dancing"); 
					break;
				
				case 10: 
					System.out.println("Ten lords a leaping"); 
					break;
				
				case 11: 
					System.out.println("Eleven pipers piping"); 
					break;
				
				case 12: 
					System.out.println("Twelve drummers drumming"); 
					break;
			}
			
			return RecursiveVerse(DAY - 1);
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Code will not entirely print out the recursive song in java.

#2 gonzaw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-December 12

Re: Code will not entirely print out the recursive song in java.

Posted 25 December 2012 - 12:21 AM

You'd need 2 recursions I'd guess.
One that you already have, "RecursiveVerse", and another one that keeps substracting 1 from LAST_VERSE and call "ResursiveVerse(LAST_VERSE)".

Like this:
private String recursiveStanza(int day){
if(day==1) return RecursiveVerse(day);
else return recursiveStanza(day-1)+"\n\n"+RecursiveVerse(day);
}

String verse = recursiveStanza(LAST_VERSE);
System.out.println(verse);


Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Code will not entirely print out the recursive song in java.

Posted 25 December 2012 - 04:02 AM

Or a simple for loop enclosing the body of the main() method:
    public static void main( String [] args )
    {
        final int LAST_VERSE = 12;
        String day = "";

        for ( int i = 1 ; i <= LAST_VERSE ; i++ )
        {
            switch ( i ) 
            {
                // the switch clause as it currently exists
            }

            System.out.println("On the " + day + " day of Christmas" + "\nMy true love gave to me");

            String verse = RecursiveVerse( i );
            System.out.println(verse);
        }
    }

Do you know or are you allowed to use arrays? The switch statements could be reduced to single line assignment statements if you could use arrays. If not, the switches are fine.

This post has been edited by GregBrannon: 25 December 2012 - 04:27 AM

Was This Post Helpful? 0
  • +
  • -

#4 mathematicsnerd888  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-December 12

Re: Code will not entirely print out the recursive song in java.

Posted 25 December 2012 - 09:30 AM

Thank you GregBrannon! It worked.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8323
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Code will not entirely print out the recursive song in java.

Posted 25 December 2012 - 10:17 PM

A switch() statementr is using a Cadillac to delivver a pizza in you case
An array would simplify your job a lot

String[] dayStr = {"first", "second", "third", ...., "twelth"};
...
day = dayStr[num-1];
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1