10 Replies - 956 Views - Last Post: 31 May 2020 - 05:39 PM Rate Topic: -----

#1 MuniiPanda   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-May 20

Writing "Twelve Days of Christmas" using a switch statement.

Posted 30 May 2020 - 04:21 PM

Hi, I'm having trouble with some Java homework and I've been at it for a few hours searching online but I can't figure out what I'm doing wrong.

The prompt for my homework reads as follows:
"5.29 (“The Twelve Days of Christmas” Song) Write an application that uses iteration and switch statements to print the song “The Twelve Days of Christmas.” Use iteration structure to loop through the days. A switch statement should be used to print the days and verse associated with each day"

I am able to cycle through the days and get the correct day to be said (i.e. "first", "second", "third", etc.), but I am having issues getting the program to write the previous days along with the current day.

Instead of printing:
"On the second day of Christmas, my true love gave to me...
Two turtle doves
And a partridge in a pear tree!"

It will only print:
"On the second day of Christmas, my true love gave to me...
Two turtle doves"

Here's my code (also attached the .java file). Any help would be appreciated!

 

public class TwelveDaysOfChristmas 
{

   public static void main(String[] args) 
   {
      
      int dayNumber = 0;
      String saidDay = "null";
      String saidPhrase = "null";
      String day1 = "A partridge in a pear tree.";
      String day1Alt = "and a partridge in a pear tree!";
      String day2 = "Two turtle doves";
      String day3 = "Three French hens";
      String day4 = "Four calling birds";
      String day5 = "Fiiiive golden rings!";
      String day6 = "Six geese a laying";
      String day7 = "Seven swans a swimming";
      String day8 = "Eight maids a milking";
      String day9 = "Nine ladies dancing";
      String day10 = "Ten lords a leaping";
      String day11 = "Eleven pipers piping";
      String day12 = "Twelve drummers drumming";
      
      for (int dayCounter = 1; dayCounter <= 12; dayCounter++)
      {
         
         dayNumber++;
         
         switch (dayCounter)
         {
            
            case 12: saidDay = "twelfth";
                    saidPhrase = day12;
                    break;

            case 11: saidDay = "eleventh";
                     saidPhrase = day11;
                     break;

            case 10: saidDay = "tenth";
                     saidPhrase = day10;
                     break;

            case 9: saidDay = "ninth";
                    saidPhrase = day9;
                    break;

            case 8: saidDay = "eighth";
                    saidPhrase = day8;
                    break;

            case 7: saidDay = "seventh";
                    saidPhrase = day7;
                    break;

            case 6: saidDay = "sixth";
                    saidPhrase = day6;
                    break;

            case 5: saidDay = "fifth";
                    saidPhrase = day5;
                    break;

            case 4: saidDay = "fourth";
                    saidPhrase = day4;
                    break;

            case 3: saidDay = "third";
                    saidPhrase = day3;
                    break;

            case 2: saidDay = "second";
                    saidPhrase = day2;
                    break;

            case 1: saidDay = "first";
                    if (dayNumber > 1)
                    {
                        saidPhrase = day1Alt;
                    }
                    else
                    {
                        saidPhrase = day1;
                    }
                    break;
                    
         } //End switch statement.
         
         System.out.println("On the " + saidDay + " day of christmas, my true love gave to me...");
         System.out.println(saidPhrase);
         System.out.println();
         
      } //End while loop.
      
   } //End of main().
   
} //End of class TwelveDaysOfChristmas.




Is This A Good Question/Topic? 0
  • +

Replies To: Writing "Twelve Days of Christmas" using a switch statement.

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: Writing "Twelve Days of Christmas" using a switch statement.

Posted 30 May 2020 - 04:32 PM

Quote

issues getting the program to write the previous days along with the current day.

There needs to be a print statement executed for each pair of lines.
Perhaps a method to print the lines for a day
Then remove the break statements.

This post has been edited by NormR: 30 May 2020 - 04:44 PM

Was This Post Helpful? 1
  • +
  • -

#3 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2410
  • View blog
  • Posts: 5,050
  • Joined: 11-December 07

Re: Writing "Twelve Days of Christmas" using a switch statement.

Posted 30 May 2020 - 08:43 PM

You don't need print statements inside the switch, but you do need to append to the variable.

For example:

saidPhrase = saidPhrase + day2;


You also need to remove the break statements. Can you work out why?

Eventually you will want to think about new lines.

One of your comments is giving incorrect information. Comments have a habit of doing this. ;)
Was This Post Helpful? 1
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3698
  • View blog
  • Posts: 16,952
  • Joined: 20-September 08

Re: Writing "Twelve Days of Christmas" using a switch statement.

Posted 31 May 2020 - 05:24 AM

Actually the CS-ish elegant solution to this is a recursive one, so i'm thinking you could do it that way and use switch to implement the "and a partridge/A partridge" thing. But then it does say to use iteration, so maybe not. What do you think cfoley?
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11948
  • View blog
  • Posts: 20,277
  • Joined: 19-March 11

Re: Writing "Twelve Days of Christmas" using a switch statement.

Posted 31 May 2020 - 09:43 AM

The point about removing the breaks is important, since it illustrates a piece of switch behavior that really is the point of this assignment.

I would also separate out the saidDay and just do that as a lookup (use an array or int-indexed Map of strings)

Also, saidPhrase should not be a String, it should be a StringBuilder. At least, I think StringBuilder is the current best practice, but things might have moved on - in any case, you don't want to be doing string concatenation in a loop in Java, since it's a wasteful operation.
Was This Post Helpful? 1
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3698
  • View blog
  • Posts: 16,952
  • Joined: 20-September 08

Re: Writing "Twelve Days of Christmas" using a switch statement.

Posted 31 May 2020 - 10:46 AM

Quote

... since it illustrates a piece of switch behavior that really is the point of this assignment.

Yes, i see that now, so maybe ignore the thing about recursion [though it makes it so much easier;)]

Quote

... but things might have moved on - in any case, you don't want to be doing string concatenation in a loop in Java, since it's a wasteful operation.

I doubt they have. The difference of course, is if you use the concatenation operator instead, all that will happen is that you'll get StringBuilder created in the compiled bytecode, repeatedly and not optimized for concatenation buffer size.

Actually, since the requirement is just a printout, there's no need for any kind of concatenation.

This post has been edited by g00se: 31 May 2020 - 11:16 AM
Reason for edit:: Clarification

Was This Post Helpful? 1
  • +
  • -

#7 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2410
  • View blog
  • Posts: 5,050
  • Joined: 11-December 07

Re: Writing "Twelve Days of Christmas" using a switch statement.

Posted 31 May 2020 - 01:39 PM

I agree that recursion would be a nicer solution but this is about switching.

For the end of the song I might do something like this to eliminate the if statement.

case 3:
  result += "Three French hens.\n";
case 2:
  result += "Two turtle doves\nand a partridge in a  pear tree!";
  break;
case 1:
  result += "A partridge in a  pear tree.";


People get all worried about StringBuilders when building Strings in Java. It's an important technique to know but applying it blindly, it's premature optimisation. For something small like this, it really doesn't help performance and concatenation is clearer.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11948
  • View blog
  • Posts: 20,277
  • Joined: 19-March 11

Re: Writing "Twelve Days of Christmas" using a switch statement.

Posted 31 May 2020 - 02:02 PM

View Postcfoley, on 31 May 2020 - 03:39 PM, said:

People get all worried about StringBuilders when building Strings in Java. It's an important technique to know but applying it blindly, it's premature optimisation. For something small like this, it really doesn't help performance and concatenation is clearer.


It's true that we're not really worried about performance in a program of this sort, but I mention it because it's important to expose beginners to these issues early on, both to develop good habits and to lay the groundwork for thinking about memory usage later on.
Was This Post Helpful? 1
  • +
  • -

#9 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3698
  • View blog
  • Posts: 16,952
  • Joined: 20-September 08

Re: Writing "Twelve Days of Christmas" using a switch statement.

Posted 31 May 2020 - 04:42 PM

Quote

I mention it because it's important to expose beginners to these issues early on, both to develop good habits and to lay the groundwork for thinking about memory usage later on.

Definitely. If it's not learned early it's often not learned at all. Quite a few questions in which i've been involved in the past have featured 'mysterious' memory issues. Of course there's nothing so mysterious when you know how StringBuffer/StringBuilder work

But again, it's not relevant here as string concatenation (of any kind) isn't required

This post has been edited by g00se: 31 May 2020 - 04:45 PM
Reason for edit:: Clarification

Was This Post Helpful? 1
  • +
  • -

#10 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2410
  • View blog
  • Posts: 5,050
  • Joined: 11-December 07

Re: Writing "Twelve Days of Christmas" using a switch statement.

Posted 31 May 2020 - 05:05 PM

View Postg00se, on 01 June 2020 - 12:42 AM, said:

But again, it's not relevant here as string concatenation (of any kind) isn't required


I think getting the computer to sing the song is extra credit. ;)
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11948
  • View blog
  • Posts: 20,277
  • Joined: 19-March 11

Re: Writing "Twelve Days of Christmas" using a switch statement.

Posted 31 May 2020 - 05:39 PM

Getting it to _want_ to sing the song is a PhD thesis.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1