Results 1 to 12 of 12
Like Tree2Likes
  • 1 Post By Pacoheadley
  • 1 Post By Pacoheadley

Thread: Switches question.

  1. #1
    Pacoheadley is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default 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!

  2. #2
    Pacoheadley is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default 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!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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!

  4. #4
    Pacoheadley is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Switches question.

    Thank you! I'll reply again if I need any extra help or have a question.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Switches question.

    Quote Originally Posted by Pacoheadley View Post
    Thank you! I'll reply again if I need any extra help or have a question.
    Sounds like a good plan. Good luck with the assignment!

  6. #6
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default 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 03:28 AM.

  7. #7
    Pacoheadley is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Switches question.

    Quote Originally Posted by penguinCoder View Post
    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.
    I know I said "corrupted", but it is worse than that. The flash drive did not have a cover, and it is basically unusable now. I was going to get around to backing it all up, but hadn't yet. The flash drive can't be read, nothing, it is damaged.

  8. #8
    Pacoheadley is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default 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...

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  10. #10
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default 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 03:50 AM. Reason: Grammar

  11. #11
    Pacoheadley is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Switches question.

    Quote Originally Posted by penguinCoder View Post
    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.
    Hmmm, we have not gone over do-while loops yet in class, just while loops, so that isn't really an option at the moment. I did figure it out with just while, so I am good on that part!
    Fubarable likes this.

  12. #12
    Pacoheadley is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Switches question.

    Just about done with the assignment now, just about to start the final testing on it, and everything seems good so far, thanks for the help!
    Fubarable likes this.

Similar Threads

  1. I am having trouble with my switches.
    By Louby85 in forum New To Java
    Replies: 7
    Last Post: 08-27-2011, 03:32 AM
  2. Compiling with -d -cp switches
    By codeAJ in forum New To Java
    Replies: 8
    Last Post: 06-05-2011, 04:25 AM
  3. Using switches...
    By besweeet in forum New To Java
    Replies: 11
    Last Post: 03-04-2010, 05:48 PM
  4. help with switches
    By spots of fire in forum New To Java
    Replies: 1
    Last Post: 01-23-2010, 10:02 PM
  5. Help with switches
    By Daniel in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 09:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •