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: Trying to Give Coins For Change

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to Give Coins For Change

    I am working on a program where you have to give change in coins. The program already gives the change amount, I'm just trying to figure out how to give the change in coins. This is what i have so far:

     
    /**
       A cash register totals up sales and computes change due.
    */
    package classes;
    public class CashRegister
    {
       // add two attributes (variables) purchase and payment
    private double purchase;
    private double payment;
     
     
       /**
          Constructor that constructs a cash register with no money in it.
       */
       public CashRegister()
       {
     
           //initialize the purchase and payment to 0.0
          double purchase = 0.0;
         double payment = 0.0;
     
       }
     
       /**
          Records the sale of an item.
          @param amount the price of the item
       */
       public void recordPurchase(double amount)
       {
     
           //add the amount of purchase to purchase
           purchase = purchase + amount;
     
       }
     
       /**
          Enters the payment received from the customer; should be called once
          for each coin type.
          @param coinCount the number of coins
          @param coinType the type of the coins in the payment
       */
     
     
     
     
     
       public void enterPayment(int coinCount, Coin coinType)
       {
     
     
          //calculate the payment
           payment = payment+ coinCount * coinType.getValue();
     
       }
     
       /**
          Computes the change due and resets the machine for the next customer.
          @return the change due to the customer
       */
       public double giveChange()
       {
         double change = purchase - payment;
     purchase = 0;
     payment = 0;
     return change;
     
     
     //calculate change,change is the difference between payment and purchase, return change
     
       }    
     
       public double giveCoins()
        {
           double change = purchase - payment;
     purchase = 0;
     payment = 0;
    double dollars;
    double quarters;
    double nickels;
    double dimes;
    double pennies;
     if(change>=1)
        {dollars=change/1;}
     
     change=change-dollars;
     
     if(change>=.25)
         {quarters=change/.25;}
     
     change=change-(quarters*.25);
     
     if(change>=.1)
     {dimes=change/.10; }
      change=change-(dimes*.10);
     
      if(change>=.05)
     {nickels=change/.05; }
      change=change-(nickels*.05);
     
        if(change>=.01)
     {pennies=change/.01; }
      change=change-(pennies*.01);
     
      return dollars + quarters + dimes + nickels + pennies;
        }
     
    }



    As you can see I'm trying to make it so say you have 5.55 in change due. The program will look at 5.50, it is greater than 1 so it will do 5.50/1 to get the dollar amount. Of course I need to find a way to round the 5.5 that will result to just 5. The program will then subtract 5 from 5.50 and continue to quaerters ad so forth.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trying to Give Coins For Change

    Hint: What happens when you convert a double value into an int?

    Or if you don't like that hint, you can always go the hard way: Math (Java Platform SE 6)

    Edit- Why are the values for the counts of each of your coins doubles instead of ints? For example, how can you have 1.5 quarters?
    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
    Jan 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to Give Coins For Change

    Okay So I went back and had the doubles converted to ints. Lol, however, I had to convert them back to double so the equations would work. I'm error free however, when I call the method it gives me 0.0. I have the method return 5 values so I should at least be getting 0.0, 0.0, 0.0, 0.0, 0.0...
    Any help is appreciated I'm not sure what the problem could be now.

     
       public double giveCoins()
        {
           double change = purchase - payment;
     purchase = 0;
     payment = 0;
    double dollars=0;
    double quarters=0;
    double nickels=0;
    double dimes=0;
    double pennies=0;
     if(change>=1)
        {dollars=change/1;}
     
     
            int dollarsint = (int)dollars;
     
            double dollarsafter = (double)dollarsint;
     
     change=change-dollarsafter;
     
     if(change>=.25)
         {quarters=change/.25;}
     
     int quartersint = (int)quarters;
     
            double quartersafter = (double)quartersint;
     
     change=change-(quartersafter*.25);
     
     if(change>=.1)
     
     {dimes=change/.10; }
     
      int dimesint = (int)dimes;
     
            double dimesafter = (double)dimesint;
     
     
     change=change-(dimesafter*.10);
     
     
     
      if(change>=.05)
     {nickels=change/.05; }
     
       int nickelsint = (int)nickels;
     
            double nickelsafter = (double)nickelsint;
     
     
      change=change-(nickelsafter*.05);
     
        if(change>=.01)
     {pennies=change/.01; }
     
        int penniesint = (int)pennies;
     
            double penniesafter = (double)penniesint;
     
      change=change-(penniesafter*.01);
     
      return dollars + quarters + dimes + nickels + pennies;
        }

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trying to Give Coins For Change

    I'm not sure what your code is supposed to do. What exactly are you supposed to return?

    But I see one major problem: you're setting both purchase and payment to 0 at the top of the method, so of course everything is going to be 0.0. You have a few other logic errors, but that's a good place to start.
    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!

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to Give Coins For Change

    So I put the code in the givechange() method instead and it gave me the result of 25.
    I found out this result is the total amount of coins to give back.
    return dollarsint + quartersint + dimesint + nickelsint + penniesint;
    How can I make it so the return statement separates the value instead of adding them together.
    I was trying to get a System.out.println() to work on the return but I couldn't get it

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to Give Coins For Change

    Okay so I'm making progress...
    I set the purchase at .25 and Pay with 1 penny, 1 dime, 8 quarters, and 1 nickel ($2.16)
    So I should get back 1.91

    Here is the Output
    The Change Due is: 1.9099999999999997 Dollars: 1 Quarters: 3 Dimes: 1 Nickels: 1 Pennies: 0

    Lol it is so close. There is something wrong with how the change is calculating any ideas?

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trying to Give Coins For Change

    That's happening because floating point arithmetic is not exact when talking about very small numbers. There are good explanations (try googling "what every computer scientist should know about floating-point arithmetic", but does anybody have the original link?), but the gist of it is this: you can't represent 1.91 exactly in binary, so the actual double value will be something very close to it but not quite exact.

    For this reason, anything dealing with things like currency should be handled as ints. Instead of 1.91 dollars, you would represent that as 191 cents, as an int. Displaying it how you want is up to you, but there are classes in the API specifically for that (look for 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. how to give inputs to my simple java program
    By pokuri in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 8th, 2011, 07:36 PM
  2. [SOLVED] Can't get boolean to change!!
    By Day2Day in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 2nd, 2010, 07:20 PM
  3. Change to JOptionPane
    By Liuric in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2010, 12:07 AM
  4. Sting format give more truble
    By ahmethbaai in forum Java Theory & Questions
    Replies: 1
    Last Post: December 17th, 2009, 01:19 PM
  5. New give thanks feature
    By JavaPF in forum The Cafe
    Replies: 0
    Last Post: April 11th, 2009, 11:14 AM