change money program - newbie question  
Author Message
reuben.nantogmah@gmail.com





PostPosted: 2005-2-25 12:14:00 Top

java-programmer, change money program - newbie question Hi,

I would appreciate any help I can get with this. The program accepts a
range of numbers (dollors) and provides a combination of the least
amount of bills and coins needed to make change for the input. This
program works for most numbers but there is a bug. try 19.99 and you
will see that the change is a penny short. Thanks for your help.

-R

public class MoneyDriver {

public static void main(String[] args) {
if(args.length != 1){
System.out.println("Please suppy only one argument on the command
line");
return;
}
double dblAmount = Double.parseDouble(args[0]);
Money m = new Money();
m.changeMoney(dblAmount);
}
}


public class Money {
Money (){
}
public void changeMoney(double dblAmount){
if((dblAmount < 0.01) || dblAmount > 9999.99){
System.out.println("The number you entered is out of range. Please
enter a number between 0.01 and 9999.99");
return;
}
double [] dblMoneyDenomination = {100.00, 50.00, 20.00, 10.00, 5.00,
1.00, 0.25, 0.10, 0.05, 0.01};
int count = 0;
for(int i = 0; i < dblMoneyDenomination.length; i++){
while((dblAmount > dblMoneyDenomination[i])){
++count;
dblAmount = dblAmount - dblMoneyDenomination[i];
}
if(count != 0){
System.out.println("Change is " + count + " " +
dblMoneyDenomination[i] + " bill(s)");
}
/* if(i + 1 == dblMoneyDenomination.length){
System.out.println("Change is " + ++count + " " +
dblMoneyDenomination[i] + " bill(s)");
}
*/
count = 0;
}
}
}

 
Tilman Bohn





PostPosted: 2005-2-25 15:34:00 Top

java-programmer >> change money program - newbie question [X-post and Followup-To set to clj.help]

In message <email***@***.com>,
email***@***.com wrote on 24 Feb 2005 20:13:37 -0800:

> Hi,
>
> I would appreciate any help I can get with this. The program accepts a
> range of numbers (dollors) and provides a combination of the least
> amount of bills and coins needed to make change for the input. This
> program works for most numbers but there is a bug. try 19.99 and you
> will see that the change is a penny short. Thanks for your help.

You are using doubles for doing calculations on numbers that have
simple representations in base 10. This simplicity, however, is
misleading, as their representations in base 2 are not generally as
simple. For example, work out what the binary representation of 0.1 is,
and you will quickly see what your problem is.

The immediate lesson you're supposed to learn from this exercise is to
use integer types internally when dealing with money (or anything else
where exact results are imperative). Can you see how to do this in your
case?

The deeper lesson you're supposed to learn from this exercise is to
always be aware of the internal representation of the data you're
working on, be aware of the mechanics of its manipulation, and be aware
of possible limitations on accuracy. And choose the representation most
appropriate for the problem at hand.

Having said all that, let me point out I haven't closely checked your
code after seeing the use of doubles, so I don't know if there might be
other algorithmic mistakes. If so, you'll have to go back to them once
you have the representation of your money right. But the latter most
definitely needs to be fixed first.

--
Cheers, Tilman

`Boy, life takes a long time to live...' -- Steven Wright
 
Thomas Weidenfeller





PostPosted: 2005-2-25 18:23:00 Top

java-programmer >> change money program - newbie question email***@***.com wrote:
> Hi,
>
> I would appreciate any help I can get with this. The program accepts a
> range of numbers (dollors) and provides a combination of the least
> amount of bills and coins needed to make change for the input. This
> program works for most numbers but there is a bug. try 19.99 and you
> will see that the change is a penny short. Thanks for your help.

Beginner's questions are best asked in comp.lang.java.help. But be
careful, like the people here, the people there don't like to do other
people's homework.

That said, you want to learn a little bit about the representation of
numbers inside a computer, especially about the double floating point
format you are using. Since this question comes up again and again in
this group, you might want to search an archive of the group for a start.

/Thomas

--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq