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

Thread: Help with my Java program: Making change from an entered double.

  1. #1
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Lightbulb Help with my Java program: Making change from an entered double.

    Hi everyone. I am a beginner of beginners in Java. I'm not a CS or programming major but I am now taking a Java course because it is required. I look forward to hearing your insight.

    I'm working on a program that is designed to make change from an entered value, only using Ten, Five, and One dollar bills and Quarters, Dimes and Nickels. The amount must round up to the nearest nickel. So for example if I entered $35.77, the output should be something like;

    Ten dollar bills: 3
    Five dollar bills: 1
    One dollar bills: 0
    Quarters: 3
    Dimes: 0
    Nickels: 1 //because it is rounding up.

    I'm very confused on what methods I should be using to write this program. This is what I have so far, can someone tell me what is wrong? Thank you in advance, much appreciated.

    Basically what I've done so far is convert the amount entered into cents. Than dividing it by the appropriate amount to convert it into Tens, Fives, Ones etc. Than subtracting that from the amount entered. I declared a variable "change" and "difference" but I have not used them below yet. Thanks again!
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    import java.util.Scanner;

    public class makingChange
    {

    public static void main(String[] args)
    {
    Scanner scan=new Scanner(System.in);
    double amt,totalCents,change, difference;
    double ten,five,one,quarter,dime,nickel;

    ten=0;
    five=0;
    one=0;
    quarter=0.0;
    dime=0.0;
    nickel=0.0;

    System.out.println("Please enter the amount to be changed");
    amt=scan.nextDouble();

    totalCents= amt*100;

    //difference=
    ten=amt-(totalCents/1000);
    five=amt-(totalCents/500);
    one=amt-(totalCents/100);
    quarter=amt-(totalCents/25);
    dime=amt-(totalCents/10);
    nickel=amt-(totalCents/5);

    System.out.println("Ten dollar bills: " + ten);
    System.out.println("Five dollar bills: " + five);
    System.out.println("One dollar bills: " + one);
    System.out.println("Quarters: " + quarter);
    System.out.println("Dimes: " + dime);
    System.out.println("Nickels: " + nickel);
    }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    can someone tell me what is wrong?
    It would help very much if *you* said what was wrong.

    Specifically, does that code compile? If not, and you can't understand the compiler's messages, post those messages and indicate which lines of your code they are referring to. I am sure someone can explain what they mean.

    Or does the code compile, but not give the output you expected or intended when you run it? In that case you have described the output you want for some specific input, but what output do you actually get for that input? Or is there a runtime exception? In either case, post the actual output that you see.

  3. #3
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    Quote Originally Posted by pbrockway2 View Post
    It would help very much if *you* said what was wrong.

    Specifically, does that code compile? If not, and you can't understand the compiler's messages, post those messages and indicate which lines of your code they are referring to. I am sure someone can explain what they mean.

    Or does the code compile, but not give the output you expected or intended when you run it? In that case you have described the output you want for some specific input, but what output do you actually get for that input? Or is there a runtime exception? In either case, post the actual output that you see.
    Sorry. No errors, I just don't get the output I need.

    I get this;

    Please enter the amount to be changed
    $35.77

    Ten dollar bills: 32.193000000000005
    Five dollar bills: 28.616000000000003
    One dollar bills: 0.0
    Quarters: -107.31
    Dimes: -321.93000000000006
    Nickels: -679.6300000000001

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    Ten dollar bills: 32.193000000000005
    This indicates a couple of things. First (obviously) you can't have 32.19... ten dollar bills. In fact the number of bills will be a whole number (0, 1, 2, ...) without a fraction. In your code it would be better for ten to be declared this way: as an int rather than a double. Something similar should be done to other (but not all) variables in the code.

    The second thing concerns the value (thirty two point something). You'll go out of business at that rate! Clearly there is something awry with the arithmetic.

    ten=amt-(totalCents/1000);

    This line is making the number of ten dollar bills equal to amt - which is the number of dollars - minus something small - which is why the answer is so large.

    You are starting with 35.77 dollars, or, equivalently, 3577 cents. Try and think of a simple formula that will give the answer 3 starting from this value. In general you want the number two places to the left of the decimal place. (or three places from the end if you are working in cents.)

    Make sure you understand how integer division works. If you change the type of the variables as indicated above, then integer division quickly leads to a more sensible formula for the number of ten dollar bills.

  5. #5
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    Thank you! Okay i will do a check on the arithmetic. If I change the dollar amounts to int values, wouldn't I have problems because the "amt" is a double value? Would I have to use some kind of data conversion?

  6. #6
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    Okay so I figured out how to cast a double to an int. But I'm still having trouble with the arithmetic to subtract from the total. If you look at my code below, all it's going to output is change in each type of currency. I'm confused on how to subtract the calculated currency from the amount entered so that the number of currency can be calculated from the new amount.
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------
    import java.util.Scanner;

    public class makingChange
    {

    public static void main(String[] args)
    {
    Scanner scan=new Scanner(System.in);
    double amt;
    int ten,five,one,quarter,dime,nickel,totalCents;

    System.out.println("Please enter the amount to be changed");
    amt=scan.nextDouble();

    totalCents=(int)amt;
    totalCents= (int) (100*amt);

    ten=(totalCents/1000);
    five=(totalCents/500);
    one=(totalCents/100);
    quarter=(totalCents/25);
    dime=(totalCents/10);
    nickel=(totalCents/5);

    System.out.println("Ten dollar bills: " + ten);
    System.out.println("Five dollar bills: " + five);
    System.out.println("One dollar bills: " + one);
    System.out.println("Quarters: " + quarter);
    System.out.println("Dimes: " + dime);
    System.out.println("Nickels: " + nickel);
    }
    }
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------
    So now the output is;

    Please enter the amount to be changed
    35.77
    Ten dollar bills: 3
    Five dollar bills: 7
    One dollar bills: 35
    Quarters: 143
    Dimes: 357
    Nickels: 715

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    I'm confused on how to subtract the calculated currency from the amount entered so that the number of currency can be calculated from the new amount.
    You subtract using -

    The thing is to subtract each time you have figured out one of the values:

    ten=(totalCents/1000);
    totalCents -= ten * 1000; // this line subtracts the ten*1000 cents already given in change
    five=(totalCents/500);
    // something similar needed here
    one=(totalCents/100);
    // etc

    -----

    I wonder about the following calculation of the number of cents:

    totalCents=(int)amt;	
    totalCents= (int) (100*amt);

    It's easy to check:

    System.out.println("Amount entered: " + amt);
    totalCents=(int)amt;	
    totalCents= (int) (100*amt);
    System.out.println("Cents: " + totalCents);

    To make matters worse, you are actually looking to convert 35.77 dollars to 3580 cents. As you think about that remember that the (int) cast will always round downwards.

  8. The Following User Says Thank You to pbrockway2 For This Useful Post:

    iDizzle (March 18th, 2012)

  9. #8
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    Okay. So I worked on it a little more and I think I almost got it. Except the fact that the nickel value is not rounding up. So 35.77 will not give me "1 nickel" from the 2 cents.
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------
    import java.util.Scanner;

    public class makingChange
    {

    public static void main(String[] args)
    {
    Scanner scan=new Scanner(System.in);
    double amt;
    int ten,five,one,quarter,dime,nickel,totalCents;

    System.out.println("Please enter the amount to be changed");
    amt=scan.nextDouble();

    totalCents=(int)amt;
    totalCents= (int) (100*amt);

    ten=(totalCents/1000);
    five=(int) ((amt-(ten*10))/5); //(totalCents/500);
    one=(int) ((amt-(ten*10)-(five*5))/1);
    quarter=(int) ((amt-(ten*10)-(five*5)-(one*1))/.25);
    dime=(int) ((amt-(ten*10)-(five*5)-(one*1)-(quarter*.25))/.10);
    nickel=(int) ((amt-(ten*10)-(five*5)-(one*1)-(quarter*.25)-(dime*.10))/.05);


    System.out.println("Ten dollar bills: " + ten);
    System.out.println("Five dollar bills: " + five);
    System.out.println("One dollar bills: " + one);
    System.out.println("Quarters: " + quarter);
    System.out.println("Dimes: " + dime);
    System.out.println("Nickels: " + nickel);
    }
    }
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------
    Please enter the amount to be changed
    35.77
    Ten dollar bills: 3
    Five dollar bills: 1
    One dollar bills: 0
    Quarters: 3
    Dimes: 0
    Nickels: 0

  10. #9
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    [QUOTE=
    To make matters worse, you are actually looking to convert 35.77 dollars to 3580 cents. As you think about that remember that the (int) cast will always round downwards.[/QUOTE]

    uh oh... that's probably why. What can I do about this? =/

  11. #10
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    hmmm. Honestly, as you can see, I'm terrible at this. But even I feel like I'm doing this the hard way. If you look at my last code, I feel that I'm relying wayyy too much on math and arithmetic instead of using the java environment to its full potential. It just seems like there's an easier way to do this or am I wrong?

  12. #11
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    Are there any packages or classes than can be included that will do the rounding?

  13. #12
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    So I was searching online and I found something called "ceiling" or "cover" that might do what I want? Would these algorithms take care of my problem? And if they will, how do I use them?

  14. #13
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    Well, giving change is mostly about arithmetic. I don't think there is some API shortcut. The problem you were given is basically meant to make you think about the fussy arithmetic details.

    Things like "nickel=(int) ((amt-(ten*10)-(five*5)-(one*1)-(quarter*.25)-(dime*.10))/.05);" look rather ugly, however. Did you understand the -= operator I used in the code I posted? It simplifies things greatly: both the arithmetic and the (unnecessary?) casts.

    -----

    Once you have determined the number of nickels you can check if you have any stray "pennies" left over. And if so add one to nickel.

  15. #14
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    There is a ceil() method in the Math class. But, beware: it takes a double argument and returns a double value.

    I think the approach I mentioned before (look for stray pennies and add 1 nickel if required) is the most conceptually straight forward. The good thing about working in cents (pennies) is that you can work with int rather than double all the way through the calculation.

  16. #15
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    I do not understand the -= operator. I don't believe we have learned that yet. I feel pretty crappy in that class because everyone is a CS/programming major so most people are very familiar with the stuff. =/.

    So by adding a nickel to stray pennies do you mean adding .05 outside the last set of parenthesis in the nickel value?

    You're right, the arithmetic in my code does look ugly. lol. And you have to understand that I AM NOT a math person either. I'm actually an anthropology major, but for some odd reason one of the schools I applied to for transfer requires a java programming course. So I sat here squinting at my scratch paper while doing the math. lol
    Last edited by iDizzle; March 18th, 2012 at 03:57 AM.

  17. #16
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    Ok so for the nickel value I changed it to this..

    nickel=(int) (((amt-(ten*10)-(five*5)-(one*1)-(quarter*.25)-(dime*.10))+.05)/.05);

    I added the .05 there. and now it did give me the correct output. I tried it with a few different cent values too and it seems to work okay. But would there be a situation where the added .05 in the nickel value would give me an extra nickel when its not needed?

    *edit* Ok so I tried $1.05. And this time the output gave me;

    Please enter the amount to be changed
    1.05
    Ten dollar bills: 0
    Five dollar bills: 0
    One dollar bills: 1
    Quarters: 0
    Dimes: 0
    Nickels: 2

    Would it make it better if I added .04 instead of the .05? That way it would be enough to round up to the next nickel without adding an extra nickel in the event it was an even $1.05?
    Last edited by iDizzle; March 18th, 2012 at 04:21 AM.

  18. #17
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    First on the -= operator because I think it does simplify things a lot. x-=y is just a shorthand way of saying x=x-y or, in words "subtract y off x". But it's your code! Stay with what you're comfortable with.

    What happens when the original amount ends in 5 cents? Like $35.05.

    The best thing would be to test it with each of the hundred values between $35.00 and $35.99.

  19. #18
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    Yes 0.04 strikes me as better than 0.05

  20. #19
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    So -= is kind of like x++ or x-- from C? I think I will leave my code as is since it is working now but for future reference how would my code have looked if I had implemented the -=?

    And again, thanks a lot for your help. I appreciate you not giving me hand outs, I actually understand what I did now.

  21. #20
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    You're welcome - I'm glad you've got it doing what you want.

    Yes, x-=1 is rather like --x. --x, x--,++x, x++ exist in Java too.

    The code would look like that in #7.

  22. #21
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with my Java program: Making change from an entered double.

    Great, thanks again! I'll be frequenting this forum more often with java help =]

Similar Threads

  1. Replies: 0
    Last Post: October 13th, 2011, 07:05 PM
  2. Making change
    By Jerick in forum Algorithms & Recursion
    Replies: 3
    Last Post: October 7th, 2011, 06:49 PM
  3. So I'm making this program...
    By SkyAphid in forum The Cafe
    Replies: 2
    Last Post: August 29th, 2011, 05:55 PM
  4. Replies: 1
    Last Post: December 4th, 2010, 05:26 PM
  5. Need help making program
    By ixjaybeexi in forum Collections and Generics
    Replies: 5
    Last Post: December 6th, 2009, 11:36 PM