Results 1 to 12 of 12
Thread: Switches question.
- 10-11-2012, 01:55 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
Switches question.
Ok, to start off, I am VERY new to Java, or any type of programming. I am taking an introduction to programming course at college, and we are not very far yet. I have a homework assignment where we need to use "switch", due tonight.
I keep all my notes and examples in this class on how to do everything in my flash drive, which is my Eclipse workstation. Unfortunately, my flash drive got corrupted, so all of my notes on how to do everything so far are gone, and we can't get any notes from our class on our school's website. Our book also barely goes over this for some reason, so it has been no help for me.
I have attached a PDF file that explains the assignment. As of right now, I am stuck on the early part, where the user has to choose which destination that they want. I can not for the life of me remember how to get the user to be able to choose the number, and have the program remember which destination this is, for the later cost calculation purposes.
If someone could help me out on this, that would be wonderful! THANK YOU for any help at all in advance, it would really be nice...
If any more information is needed, just ask!
- 10-11-2012, 02:01 AM #2
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
Re: Switches question.
Won't let me post an attachment so I'll post the main parts here.
__________________________________________
1. Must include a well-written program description to describe the task of the problem.
2. Use named constants for the fixed values.
3. Display a menu for the user to make a selection. The menu items may be labeled 1, 2, 3,
etc. Use switch statements to handle the user's selection of destination and corresponding
transportation method. Assign values to appropriate variables according to user's selection.
Check the validity of user's inputs using default clause in switch statements. Terminate
the execution using System.exit(1); when an invalid input is encountered.
4. Do the same for hotel selection.
5. Correctly calculate the costs for transportation (round trip), hotel, meals, and the total.
6. The output produced by your program should include itemized costs (i.e. cost of
transportation, cost of hotel, and cost of meals) in addition to the total cost, and must
be descriptive and well formated.
7. Test your program with various input data, including the minimum of the following:
Travel to Cleveland, stay at Comfort Inn.
Travel to Las Vegas, stay at Marriott.
Drive to Washington DC, stay at Marriott.
Fly to Washington DC, stay at Hyatt.
Invalid input.
A sample run may look like this:
Here are the places you may go for vacation.
--------------------------------------------
1. Cleveland
2. Las Vegas
3. Washington DC
--------------------------------------------
Please select the number of the destination --> 3
To Washington DC, do you want to drive ('d') or fly ('f')? d
These are the hotels having vacancies:
----------------------------------------
1. Comfort Inn $ 85.00
2. Marriott $159.00
3. Hyatt $238.00
----------------------------------------
Please make your selection --> 2
Cost for your trip:
(drive to Washington DC, staying at Marriott for 3 days):
---------------------------
Trasportation $ 170.51
Hotel cost : $ 477.00
Cost of meals: $ 156.00
---------------------------
Total cost : $ 803.51
---------------------------
Have a nice vacation!
-
Re: Switches question.
Lucky for you there's a great resource available free online to learn Java including how to use the switch statement. You can find it here:
Good luck and happy coding!
- 10-11-2012, 02:14 AM #4
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
Re: Switches question.
Thank you! I'll reply again if I need any extra help or have a question.
-
- 10-11-2012, 02:26 AM #6
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
Re: Switches question.
I am going to take a wild guess and say that you are probably using Windows, since if you were using Linux you first question would have been "How do I repair my corrupted flash drive.".... Lol. You can use the chkdsk command with the /r option to attempt to repair the flash drive in a Windows System. The syntax is, 'chkdsk <driveLetter>: /r' I believe this should work, though I havn't used Windows in close to a year.. If it doesn't, feel free to post the errors on here, and I will attempt to give you further advice; though I most say that Windows is not my forte.
Last edited by penguinCoder; 10-11-2012 at 02:28 AM.
- 10-11-2012, 02:37 AM #7
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
Re: Switches question.
- 10-11-2012, 02:40 AM #8
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
Re: Switches question.
Well, another question...
After I make my switches for the destination choice, and I get to default, how would I make it so it asks the question over and over again until the user inputs either a 1, 2, or 3? Would it involve "while"? Another thing that was in my examples that got lost...
-
Re: Switches question.
Yep, that is exactly what you'd use, a while loop.
- 10-11-2012, 02:49 AM #10
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
Re: Switches question.
Each of the loops could potentially be used for anything. However, in general:
For loops are used when the programmer knows, or can compute the exact amount of times the loop will be run.
Do While loops is used when the programmer wants the loop to be executed between 1 and infinity times.
While loops is used when the programmer wants the loop to be executed between 0 and infinity times.
Seems like you would want it executed a minimum of 1 time, while giving the user the chance to execute it as many times as they wish. So a Do While loop would probably be what you are looking for.Last edited by penguinCoder; 10-11-2012 at 02:50 AM. Reason: Grammar
- 10-11-2012, 03:02 AM #11
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
- 10-11-2012, 03:50 AM #12
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
I am having trouble with my switches.
By Louby85 in forum New To JavaReplies: 7Last Post: 08-27-2011, 02:32 AM -
Compiling with -d -cp switches
By codeAJ in forum New To JavaReplies: 8Last Post: 06-05-2011, 03:25 AM -
Using switches...
By besweeet in forum New To JavaReplies: 11Last Post: 03-04-2010, 04:48 PM -
help with switches
By spots of fire in forum New To JavaReplies: 1Last Post: 01-23-2010, 09:02 PM -
Help with switches
By Daniel in forum New To JavaReplies: 2Last Post: 07-04-2007, 08:37 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks