Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: There must be a better way...

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default There must be a better way...

    But I don't know what it is. I'm a student and this is my first (university) class in Java programming.

    Basically, what I'm trying to do is write a simple tip calculator. The trouble is with how my instructor wants it done. He wants the user to be presented with this as the tip types:

    1- 10%
    2- 15%
    3- 20%
    4- 25%

    and have the user input 1-4 instead of, say, "15" or "0.15". We haven't really done any examples like this in class so I'm not sure how to assign the 1-4 to each percentage. Complicating matters (and my math) is that we can only use integers; no double or float. I declared my variables as;

    int tipRate1 = 5 + (1 * 5);
    int tipRate2 = 5 + (2 * 5);
    int tipRate3 = 5 + (3 * 5);
    int tiprate4 = 5 + (4 * 5);

    and the base tip rate is 5 + tip type * 5. I know the math is right, but I'm not sure how to assign the values 1-4 AND the equation without listing the tip types twice- once to assign the base tip rate equation and again to assign the input value to it. Does that make sense? Is there a way to combine that into one line per tip type so it looks cleaner and more efficient?

    The other thing I'm having trouble with is how to get the tip amount. I know I have to do something like this;

    (foodBill + barTab)*100 to get the bill total in cents and then somehow get it to calculate the tip in the same line without using decimals. My instructor wants to output as, for example, "Your tip should be __dollars and __cents." I'm not sure how to turn it back into dollars and have a remainder, much less output as words and not just as a number.

    Anyways, it's not due until Feb 1st, but I've spent a lot of hours working on it for the last week and I've finally had to break down and seek some guidance. I'm getting a tad frustrated and feel like there's a simple solution I'm just not seeing. If anyone has some suggestions to point me in the right director, that would be fantastic!

  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: There must be a better way...

    Welcome RockDoc to Java Programming Forums. Don't forget to read the rules.
    Well, to accomplish this assignment, you must have to first look at Scanner class in java. When you are done with it, move on to the conditional statements and might be you need loop statements as well. So, according to my point of view you only need these 3 things to complete your assignment.
    Good Luck and if you stuck anywhere, let us know, we will be pleased to help you but not spoon feed.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: There must be a better way...

    we can only use integers
    That will require you to work in pennies vs dollars.
    a way to combine that into one line
    Look at using an array to hold the rates and use the type as an index

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: There must be a better way...

    Honestly, I'm loving the idea of using an array, but I cannot. No loop or "if/else" statements either. My instructor gave us a very short list of methods we can use to accomplish this. He wants the input done using JOptionPane and Scanner (I understand JOptionPane completely, Scanner I'm less sure of so I've been reading more about it), and output using JOptionPane and echoed with System.out.println (no troubles there).
    Inputting numbers has to be done as a string and converted to an integer with parsing tools. I understand that I need to convert all the dollar amounts to cents before performing any calculations because I have to use whole numbers. Complicating matters is that I'm expected to add a command that will calculate the tip on either the base rate*the fod bill OR (the base rate - 5%) * the total of the food bill and bar tab...whichever is larger. That I have no idea how to do without using an if statement. ETA: Never mind. That's done like this, correct? tipTotal = (foodBill+barTab)*tipRate/100

    Just to clarify, I don't want anyone to give me the answers or specific code. Sorry if it sounded like that. I really do want to learn the material and understand how the programs work, or I'm going to be in big trouble when the exams roll around.

    I'm going to try recoding this in Scanner class and report back. I think I've almost got it figured out.
    Last edited by RockDoc; January 28th, 2012 at 10:02 PM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: There must be a better way...

    whichever is larger
    How do you detect that without using an if statement? Did the instructor give an clues?

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    11
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: There must be a better way...

    How do you detect that without using an if statement? Did the instructor give an clues?
    I'm just starting Java too, so my solution may be really ugly. For figuring out which one is larger you could use interger division to produce which tip is larger. It's a roundabout way, and I'm sure there's something more elegant.

    Ex.
    int tipTotal1 = 5 ;
    int tipCompare1 = tipTotal1/tipTotal2 ;

    int tipTotal2 = 2 ;
    int tipCompare2 = tipTotal2/tipTotal1 ;

    int Tip = ((tipCompare1/tipCompare1) * tipTotal1 ) + ((tipCompare2/tipCompare2) * tipTotal2 ) ;

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: There must be a better way...

    int Tip = ((tipCompare1/tipCompare1) * tipTotal1 ) + ((tipCompare2/tipCompare2) * tipTotal2 ) ;
    This statement will always return with either a runtime error or tipTotal1+tipTotal2.