Solved

Java Multiple Switch Statements

Posted on 2008-10-11
10
1,813 Views
Last Modified: 2013-11-23
I'm in a beginning Java class and our assignment is to use multiple switch statements to print the Twelve Days of Christmas verses so it loops through each day and the corresponding gifts. Not looking for the entire code, but can anyone give me some general guidance on how to set this up? Our textbook only gives examples of one switch statement and I haven't been able to get my code to pick up the second switch.

Thanks
0
Comment
Question by:mcsusan
10 Comments
 
LVL 6

Expert Comment

by:manuel_m
ID: 22694550
Please go to the Sun java tutorial. There you will find an example and guidance:

https://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html
0
 
LVL 9

Accepted Solution

by:
mbodewes earned 500 total points
ID: 22694662
The nicest way is probably to use an enum (or if you haven't seen one of those yet) an integer representing the day. Int is probably even better. Your switch statement could use fall through (be sure to document it though). So you have in psuedo code:

for i = 1 to 12
print ...my true love etc ...
switch
  case 12:
    printGift(getGift(i))
  case 11:
    comment: fall through
....
   default:
      error: wrong day

and a method getGift(giftForDay)

switch(giftForDay)
...

I think this is the maximum I can do without spoiling everything. I'll leave the coding up to you. Make sure you do it very neatly, they may give you more points for the actual readability than for the solution itself.
0
 
LVL 3

Expert Comment

by:sreehariharan
ID: 22695237
Why don't you go for with Array Concept...

day[0] = "Firstday Gift"
day[1] = "Second day Gift"
.
.
.
day[11] = "Lastdat Gift"



0
 
LVL 9

Expert Comment

by:mbodewes
ID: 22695355
sreehariharan: "I'm in a beginning Java class and our assignment is to use multiple switch statements"
0
 
LVL 3

Expert Comment

by:sreehariharan
ID: 22695430


so in case he can use the syntax below

day = '10' // You can assign or get as parameter

swith (day)
{
case 1: System.out.print("I day Gift"); break;
case 2: System.out.print("II day Gift"); break;
.
.
.
case 12: System.out.print("12th day Gift"); break;
default: System.out.print("No Gifts other than these days"); break;
}

And Dont Confuse Multiple Switch mean as Nested Switch like

swith(var)
{
case 1:
     switch(var1)
       {
        case 1: ***** break;
        case 2: ***** break;
        }
}

My recommend is Using Switch Case will degrade your performance and ordinary switch statement itself called as Multiple switch CASE statement.. Since you are having your assignment with Multiple switch statement, you can follow above snippet...


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:mbodewes
ID: 22695491
sreehariharan, I don't want to be pedantic, but you're not helping here.
- I don't understand what is wrong with my solution, and wrong with your solution
- Your code does not only not compile and is formatted and styled very badly
- Your spelling is pretty bad, capitalization is off and your sentences don't run
- You're using real code, but you aren't using the code snippet option
- You're providing real code to someone that is using Java for an assignment
- You're bringing in performance to a problem where performance is absolutely not required.
- Switch cases are performing, basically you can use less conditionals using switch statements
Please take some time and read through some questions and solutions provided in the Java area.

Answers to "New to Java"  are pretty thick, so you have to be quick and precise when answering them. Only answer with clear solutions and before someone has given an answer you feel is sufficient.
0
 
LVL 9

Expert Comment

by:mbodewes
ID: 22695493
"- I don't understand what is wrong with my solution, and wrong with your solution"

should be

"- I don't understand what is wrong with my solution, that's right with your solution"
0
 
LVL 3

Expert Comment

by:sreehariharan
ID: 22697630
Hi come on... am not telling that ur solution is wrong...
i just explained him with clear. since he is beginner of Java...

And in this forum is meant for suggest to the unknown and not arguing with others...

Ok If so, in your solution for loop is not needed...

eg: consider today's christmas day is 2
according to ur logic it will be run for all the days...
i mean from 2 to 12... but he expect the gift on second day....

be COooOooL.....
0
 
LVL 9

Expert Comment

by:mbodewes
ID: 22698138
Author: If you've got any problem with the implementation, don't hesitate to ask within this thread. We've got to do something to earn the 500 points. I regret the argument that has sprung up.
0
 

Author Closing Comment

by:mcsusan
ID: 31505323
Thanks for the help.  I turned the assignment in late but it DID work.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Strange loading of website behaviour 3 32
custom annotations 9 33
oracle 11g 23 51
hibernate example for saving data 19 6
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now