Money program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KoreyAusTex
    New Member
    • Feb 2007
    • 36

    Money program

    I am having a very hard time making this program work right, I am sort of lost and I was wondering if I might get some advice.

    This is what I have so far but I have a rounding error when I use the amount 10.03:

    [CODE=java]public class Money
    {
    public static void main(String[] args)
    {
    double TOTAL_AMOUNT = 10.03;

    int money = (int)(TOTAL_AMO UNT * 100);

    System.out.prin tln("Your money amount " + TOTAL_AMOUNT + " consists of");

    System.out.prin tln("\t" + (money / 100) + " dollars");

    money = money % 100;

    System.out.prin tln("\t" + (money / 25) + " quarters");
    money = money % 25;

    System.out.prin tln("\t" + (money / 10) + " dimes");
    money = money % 10;

    System.out.prin tln("\t" + (money / 5) + " nickels");
    money = money % 5;

    System.out.prin tln("\t" + (money / 1) + " pennies");
    }
    }[/CODE]

    I know Math.round() is a useful class but can't figure how to put it in my program
    Last edited by Ganon11; Feb 5 '08, 03:16 AM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Do you need to use a floating point (decimal) number to hold the money? I see you immediately convert it to an integer - maybe you should just start with an integer.

    Comment

    • hollywood115
      New Member
      • Feb 2008
      • 16

      #3
      You're problems is that you're not using Math.round.

      just replace:
      int money = (int)(TOTAL_AMO UNT * 100);
      with:
      int money = (int)Math.round (TOTAL_AMOUNT * 100);

      it should round a lot more accurately now, try it out.

      hope it helps :)

      Originally posted by KoreyAusTex
      I am having a very hard time making this program work right, I am sort of lost and I was wondering if I might get some advice.

      This is what I have so far but I have a rounding error when I use the amount 10.03:

      [CODE=java]public class Money
      {
      public static void main(String[] args)
      {
      double TOTAL_AMOUNT = 10.03;

      int money = (int)(TOTAL_AMO UNT * 100);

      System.out.prin tln("Your money amount " + TOTAL_AMOUNT + " consists of");

      System.out.prin tln("\t" + (money / 100) + " dollars");

      money = money % 100;

      System.out.prin tln("\t" + (money / 25) + " quarters");
      money = money % 25;

      System.out.prin tln("\t" + (money / 10) + " dimes");
      money = money % 10;

      System.out.prin tln("\t" + (money / 5) + " nickels");
      money = money % 5;

      System.out.prin tln("\t" + (money / 1) + " pennies");
      }
      }[/CODE]

      I know Math.round() is a useful class but can't figure how to put it in my program

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Loose the doubles and use ints -- cents.

        Comment

        • KoreyAusTex
          New Member
          • Feb 2007
          • 36

          #5
          Originally posted by BigDaddyLH
          Loose the doubles and use ints -- cents.
          My professor wanted it in the format of 10.03 or 15.34. She didn't let us break it up, if I understand you correctly?

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by KoreyAusTex
            My professor wanted it in the format of 10.03 or 15.34. She didn't let us break it up, if I understand you correctly?
            Well, I'm not sure to say since I don't know the whole story, but if I were writing the program for my own benefit, I would work with ints, and merely format the output to look "15.34".

            If you have time, ask your professor to clarify things for you.

            Comment

            • missdaisy14
              New Member
              • Jul 2013
              • 1

              #7
              This is very helpful. But what if the output is 0 and you don't want it to show in the results?

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                You can always catch specific cases with an if branch. When working with integers, you'd simply put something like this:
                Code:
                if(money / 25 > 0) {
                    System.out.println("\t" + (money / 25) + " quarters");
                }
                You could even use an if-else structure to give a more natural response:
                Code:
                if(money / 25 > 0) {
                    System.out.println("\t" + (money / 25) + " quarters");
                } else {
                    System.out.println("\tNo quarters");
                }
                There are shorter ways of doing this, but this is the easiest to understand.

                Comment

                Working...