aspose file tools*
The moose likes Beginning Java and the fly likes Calculating Change Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calculating Change " Watch "Calculating Change " New topic
Author

Calculating Change

Mark Captain
Ranch Hand

Joined: Nov 01, 2011
Posts: 30
Hi, im trying to calculate the amount of each coin given in change and to have the least amount of coins returned. I have a formula going, however, it is only calculating the amount of each coin seperatly. Say there was 9.50 in change, it is returning me change = 9 dollars, 38 quarters, 95 dimes, 190 nickels, 950 pennies. While the answer i was looking for would be 9 dollars, 2 quarters, 0 dimes, 0 nickels, 0 pennies.

It looks as if every time a method runs, the change does not transfer over. Im stuck on how to fix this

The code i have in the main driver is



and the methods from the Machine class

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

What have you done so far to isolate the problem? Have you stepped through the code with the IDE's debugger? or if you don't have a good IDE, have you put in System.out.println() statements
in the code? Have you debugged one case yet?

A critical aside, not directly related to your problem, but I have to mention it: never use float or double for money. Ever. Use the integer number of pennies. Floating point numbers don't add up the way pennies do. The problems will be subtle and hard to find, but some accountant will find it and jump all over your, er, back.


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8223
    
  23

Pat Farrell wrote:A critical aside, not directly related to your problem, but I have to mention it: never use float or double for money. Ever. Use the integer number of pennies.

Or alternatively BigDecimal.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calculating Change