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 10 of 10

Thread: Help Calculating a tip

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help Calculating a tip

    Hello everyone! Having a bit of trouble with the math on this one. When I run the program it doesn't read the gratuity as a percentage, which is why it's showing up as a whole number. I need this program to basically output the tip amount by itself, and then the grand total. Of course allowing the user to input whatever the rate of gratuity is. Any help/advice is appreciated!

    package calculatingtip;
    import java.util.Scanner;
    public class CalculatingTip
    {
     
        public static void main(String[] args)
        {
            Scanner input=new Scanner(System.in);
            double billBeforeTip;
            double tipRate;
            double tipAmount;
            double grandTotal;
     
            System.out.println("Enter Bill Amount");
            billBeforeTip = input.nextDouble();
            System.out.println("Enter Rate of Gratuity");
            tipRate = input.nextDouble();
            System.out.println("The Amount before the tip is: " + billBeforeTip + 
                    " The Gratuity rate is " + tipRate + "%");
            tipAmount = billBeforeTip * tipRate;
            grandTotal = tipAmount + billBeforeTip;
            System.out.println("The Grand Total comes to " + grandTotal +
                    "The Gratuity is: " + tipAmount);
        }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Help Calculating a tip

    Well, think about this logically: how do you calculate a percentage?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help Calculating a tip

    Let me guess: Divide the smaller number by the bigger one and multiply by one hundred?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,855
    My Mood
    Mellow
    Thanks
    178
    Thanked 631 Times in 618 Posts

    Default Re: Help Calculating a tip

    "15 percent" means literally, 15 / 100. 15 percent, or '%', of something is

    something * ( 15 / 100 )

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help Calculating a tip

    I'm still having issues implementing this into my code. I see how I would need to get the number and divide it by 100, but I'm not sure which variables I need to include, and how the percentage can be chosen by the user. I appreciate the help so far.

  6. #6
    Member
    Join Date
    May 2013
    Posts
    96
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: Help Calculating a tip

    The standard tip rate isn't 15%, it's 0.15 when you actually calculate. So you would want to update whichever variable represents the tip amount to reflect that.

  7. The Following User Says Thank You to mstabosz For This Useful Post:

    Sephyncloud (September 13th, 2013)

  8. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help Calculating a tip

    Thanks for the help everyone, I got my code working great now! Just a quick question though. When the tip amount is displayed at the end of my new program, it shows up as $1.5 (When 15% is the tip amount). Is there a way to make it show up as $1.50? I am mostly inquiring about the aesthetics of the output, not necessarily the function. Thanks again!

  9. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help Calculating a tip

    Do a search on formatting java output

  10. #9
    Member
    Join Date
    May 2013
    Posts
    96
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: Help Calculating a tip

    hanks for the help everyone, I got my code working great now! Just a quick question though. When the tip amount is displayed at the end of my new program, it shows up as $1.5 (When 15% is the tip amount). Is there a way to make it show up as $1.50? I am mostly inquiring about the aesthetics of the output, not necessarily the function.
    There's a function called printf used for formatting console output.

  11. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Help Calculating a tip

    And yet another class called DecimalFormat.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Could use some beginners tip here..
    By obie in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 16th, 2012, 02:55 PM
  2. Calculating Percentile
    By lakshmivaraprasad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2011, 07:26 PM
  3. Calculating Strings?
    By SkyAphid in forum Java Theory & Questions
    Replies: 2
    Last Post: August 30th, 2011, 09:38 PM
  4. question on calculating
    By meowCat in forum Java Theory & Questions
    Replies: 5
    Last Post: August 9th, 2010, 05:06 PM
  5. Tip of the Month
    By helloworld922 in forum The Cafe
    Replies: 6
    Last Post: July 5th, 2010, 02:42 AM