• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calculating Change

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
reply
    Bookmark Topic Watch Topic
  • New Topic