Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Change Maker Help
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Krocker




PostPosted: Thu Oct 20, 2011 10:04 am   Post subject: Change Maker Help

ok, so im trying to make a change maker for an assignment for school, we cannot use if statments, that is one reason why im stupped. Ok so i have the basics, but the program seems to be doing something wrong when it calculated pennies, quarters, nickels, tonnies and loonies (Canadian $ system). Need help:



import java.awt.*;
import hsa.Console;

public class ChangeMaker
{
public static void main (String[] args)
{
Console c = new Console ("ChangeMaker");
c.print ("Please enter the total price of the purchase: $");
double purchase = c.readDouble ();

c.print ("");

c.print ("Please enter the total amount paid: $");
double paid = c.readDouble ();

double change = paid - purchase;
c.println ("Therefor the change will be : $" + change);


double tens = Math.round (change) / 10;
double five = (Math.round (change) % 10) / 5;
double two = (Math.round (change) % 5) / 2;
double one = (Math.round (change) % 2) / 1;
double quar = (Math.round (change) % 1) / 0.25;
double nick = (Math.round (change) % 0.25) / 0.10;
double pen = (Math.round (change) % 0.10) / 0.01;

c.println (tens + " Ten dollar bills");
c.println (five + " Five dollar bills");
c.println ((two) + " Tonnies");
c.println ((one) + " Lonnies");
c.println ((quar) + " Quarters");
c.println ((nick) + " Nickels");
c.println ( (pen) + " Pennies");
}
}
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Thu Oct 20, 2011 10:11 am   Post subject: RE:Change Maker Help

There is a logic problem here. You shouldn't be using Math.round(). There's another function that does something similar in the Math class ( see here: http://download.oracle.com/javase/6/docs/api/java/lang/Math.html ) that does what you want. Ask yourself: If I owe you $9.99, how many tens should I give you? How many will your program give instead?

This whole operation is a lot more straightforward if you track how much change is remaining after you calculate each amount. For example, if the initial change required is $12.34, then after 1 x $10, then the remaining change is $2.34; after 0 x $5 it will be $2.34; after 1 x $2 it will be $0.34; etc.

Also, you are missing dimes.
Krocker




PostPosted: Thu Oct 20, 2011 10:21 am   Post subject: RE:Change Maker Help

ok... well im new to java, and i reviewed the document, but im still not sure what method ur referring to. im also terrible at math, so bare with me. also u said that i need to track the remaining, i did that when i declared the types of change (dollars and cents). im lost
DemonWasp




PostPosted: Thu Oct 20, 2011 10:46 am   Post subject: RE:Change Maker Help

Let's start from the beginning, for a moment. We'll use the following case: the change we must make is $12.34 .

First, we divide 12.34 by 10 and get 1.234. We round down (floor()) to get 1. Therefore, we give one $10 bill. We now owe $12.34 - $10 = $2.34 .

Second, we divide 2.34 by 5 and get 0.468. We round down to get 0. Therefore, we do not give any $5 bills. We still owe $2.34 .

Third, we divide 2.34 by 2 and get 1.17. We round down to get 1. Therefore, we give one $2 toonie. We still owe $0.34 .

Fourth, we divide 0.34 by 1 and get 0.34. We round down to get 0. Therefore, we do not give any $1 loonies. We still owe $0.34 .

Fifth, we divide 0.34 by 0.25 and get 1.36. We round down to get 1. Therefore, we give one $0.25 quarter. We still owe $0.09 .

Sixth, we divide 0.09 by 0.10 and get 0.9. We round down to get 0. Therefore, we do not give any $0.10 dimes. We still owe $0.09 .

Seventh, we divide 0.09 by 0.05 and get 1.8. We round down to get 1. Therefore, we give one $0.05 nickel. We still owe $0.04 .

Eighth, we divide 0.04 by 0.01 and get 4. We round down to get 4. Therefore, we give four $0.01 pennies. We now owe $0.00 and are done.

Try writing a program that will use the process above to find out how many tens you should give someone (ignore the other ones for now), and how much you will owe after giving that many tens. Try example values:

A) $12.34: one ten, $2.34 left over.
B) $9.99: zero tens, $9.99 left over.
C) $100: ten tens, $0.00 left over.

Once your program works correctly for each of those cases, start adding $5 bills and so on. After each one, make sure you still get the right answer.
Krocker




PostPosted: Fri Oct 21, 2011 11:54 am   Post subject: RE:Change Maker Help

it worked!!!! wow, it was so simple, thx, got 100% to. love this sight!!! if u want, ill post the codes online
DemonWasp




PostPosted: Fri Oct 21, 2011 12:46 pm   Post subject: RE:Change Maker Help

I'm glad you figured it out. Please don't post the code though; this is a common assignment for beginners and it's better that new students work out the answer themselves (perhaps assisted) than have it handed to them.
Krocker




PostPosted: Fri Oct 21, 2011 3:03 pm   Post subject: RE:Change Maker Help

true, k nvm, thx though
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: