• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

My program won't return the correct ammount of change

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off I would like to say that I have only been programing for about 4 weeks so I'm sorry if this post is abysmally silly. However, the assignment that I am currently working on doesn't seem to be working correctly and I cannot figure out why. It compiles just fine and runs fine but for some reason it will not return what I designed it to return





the problem appears to be with the pennie while statement...

the program will return the correct ammount of change for several values but it often returns the incorrect number of pennies. For instance 15.88 returns 3 pennies while 15.87 returns only 1


PLZZZZZZZZZ help my project is due in like 2 hours and I thought it worked fine but now it's broken. Any help would be greatly appreciated as I have spent several hours trying to troubleshoot it myself.

[ February 09, 2005: Message edited by: russell lloyd ]
[ February 09, 2005: Message edited by: russell lloyd ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Things to watch out for when doing floating point arithmetic:
http://java.sun.com/developer/JDCTechTips/2003/tt0204.html#2
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A quick fix would be to represent the money as int (number of pennies) rather than float (number of dollars) if that is possible.

Other than that, floating point arithmatic is not exact so you would want to use BigDecimal.
 
russell lloyd
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank both of you so much my program works now. I think it would have been nice of my teacher to point that out to us but hey she hasn't even taught us how to use while loops yet so I have spent the last 4 hours trying to learn about while loops. Both of you are eternally my heroes for you have saved my grade. THANK YOU THANK YOU THANK YOU
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you understand integer arithmetic, you can actually do this whole thing without any while loops at all. Since you've already done your own implementation, I don't feel bad at all showing you another way to do it:

Notice that the variable names I use above all start with lower case. This is a common convention. Usually class names start with upper case. I would strongly suggest that you follow this convention, especially since it will help others here at the Ranch understand your code more quickly.

Perhaps the trickiest part I use in the code above, is the -= operator. This is just a shortcut: "x -= y" is the same as "x = x - y". Also note that every variable above has type int. The division might work differently than you expect. I won't go into details, but if you'd like to run the code and figure out how it works, you'll need to learn about so-called "integer division".

Mostly, I hope this shows that there is often more than one way to tackle a problem. I don't wish to invalidate your attempts at all. Rather I'm just showing a different way of approaching the problem.

Keep Coding!

Layne
 
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic