I have a minor problem - one of the assignment's for my AP Comp Sci class is to code a little proggy that finds the change due for a retail transaction. However, I can't get it to work with money in it's typical format - i.e. 12.87 for $12.87. Instead, I have to use 1287. It saves a keystroke anyway, but just for the sake of figuring out how, I can't rest until I figure it out. Below is the code to the app as is. Please be aware that the lib that provides my ConsoleIO commands only accepts strings, ints, and doubles. So I need to use a double.
When I tried this initially, in the first rev, I discovered that it makes some crazy, horrendous rounding error. What it was doing was 13.00 - 12.87 and coming out with .1299999999999993 or somthing like that. So I switched over to the ints (And therefore dropped the decimal point) just to get the assignment done. But WHY? And how can I make it work the way it's supposed to?
As for the two libs, chn.util is the only one in use. apcslib is unused in this rev. As you can see, I am no Klingon warrior - I comment my code at a staggaring ratio.
/*
*Change2.java
*Calculates neccessary coinage with support for whole bills
*9/5/03
*By: Tony Bathgate
*/
import chn.util.*;
import apcslib.*;
class Change2
{
public static void main ( String args[] )
{
/* Variable uses
* diff = difference in pennies
* a = [Used in earlier revision - removed]
* b = number quarters
* c = modulus after taking out quarters
* d = number of dimes
* e = modulus after taking out dimes
* f = number of nickels
* g = number of pennies
* h = number of hundred dollar bills
* i = modulus after taking out hundreds
* j = number of fifties
* k = modulus after taking out fifties
* l = number of twenties
* m = modulus after twenties
* n = nubmer of tens
* o = modulus after tens
* p = number of fives
* q = modulus after fives
* r = number of ones
* s = modulus after ones
*/
int due, cash;
int b,c,d,e,f,g,diff,h,i,j,k,l,m,n,o,p,q,r,s;
//Initialize input device
ConsoleIO keyboard = new ConsoleIO();
//User prompt
System.out.println("Enter all amounts without a decimal point.");
System.out.println("For Example, 1300 equals $13.00");
System.out.println("Amount due: ");
due = keyboard.readInt();
System.out.println("Amount tendered: ");
cash = keyboard.readInt();
//Find difference
diff = cash - due;
//Take out hundreds
h = diff/10000;
i = diff%10000;
//Take out fifties
j = i/5000;
k = i%5000;
//Take out twenties
l = k/2000;
m = k%2000;
//Take out tens
n = m/1000;
o = m%1000;
//Take out fives
p = o/500;
q = o%500;
//Take out ones
r = q/100;
s = q%100;
//Take out quarters
b = s/25;
c = s%25;
//Take out dimes
d = c/10;
e = c%10;
//Take out nickels, find pennies
f = e/5;
g = e%5;
//Output starts here
System.out.println("Change to customer: " + diff);
System.out.println("Broken down as:");
System.out.println("Hundred dollar bills: " + h);
System.out.println("Fifty dollar bills: " +j);
System.out.println("Twenty dollar bills: " + l);
System.out.println("Ten dollar bills: " + n);
System.out.println("Five dollar bills: " +p);
System.out.println("One dollar bills: " +r);
System.out.println("Quarters: " + b);
System.out.println("Dimes: " + d);
System.out.println("Nickels: " + f);
System.out.println("Pennies: " + g);
}
}
When I tried this initially, in the first rev, I discovered that it makes some crazy, horrendous rounding error. What it was doing was 13.00 - 12.87 and coming out with .1299999999999993 or somthing like that. So I switched over to the ints (And therefore dropped the decimal point) just to get the assignment done. But WHY? And how can I make it work the way it's supposed to?
As for the two libs, chn.util is the only one in use. apcslib is unused in this rev. As you can see, I am no Klingon warrior - I comment my code at a staggaring ratio.
/*
*Change2.java
*Calculates neccessary coinage with support for whole bills
*9/5/03
*By: Tony Bathgate
*/
import chn.util.*;
import apcslib.*;
class Change2
{
public static void main ( String args[] )
{
/* Variable uses
* diff = difference in pennies
* a = [Used in earlier revision - removed]
* b = number quarters
* c = modulus after taking out quarters
* d = number of dimes
* e = modulus after taking out dimes
* f = number of nickels
* g = number of pennies
* h = number of hundred dollar bills
* i = modulus after taking out hundreds
* j = number of fifties
* k = modulus after taking out fifties
* l = number of twenties
* m = modulus after twenties
* n = nubmer of tens
* o = modulus after tens
* p = number of fives
* q = modulus after fives
* r = number of ones
* s = modulus after ones
*/
int due, cash;
int b,c,d,e,f,g,diff,h,i,j,k,l,m,n,o,p,q,r,s;
//Initialize input device
ConsoleIO keyboard = new ConsoleIO();
//User prompt
System.out.println("Enter all amounts without a decimal point.");
System.out.println("For Example, 1300 equals $13.00");
System.out.println("Amount due: ");
due = keyboard.readInt();
System.out.println("Amount tendered: ");
cash = keyboard.readInt();
//Find difference
diff = cash - due;
//Take out hundreds
h = diff/10000;
i = diff%10000;
//Take out fifties
j = i/5000;
k = i%5000;
//Take out twenties
l = k/2000;
m = k%2000;
//Take out tens
n = m/1000;
o = m%1000;
//Take out fives
p = o/500;
q = o%500;
//Take out ones
r = q/100;
s = q%100;
//Take out quarters
b = s/25;
c = s%25;
//Take out dimes
d = c/10;
e = c%10;
//Take out nickels, find pennies
f = e/5;
g = e%5;
//Output starts here
System.out.println("Change to customer: " + diff);
System.out.println("Broken down as:");
System.out.println("Hundred dollar bills: " + h);
System.out.println("Fifty dollar bills: " +j);
System.out.println("Twenty dollar bills: " + l);
System.out.println("Ten dollar bills: " + n);
System.out.println("Five dollar bills: " +p);
System.out.println("One dollar bills: " +r);
System.out.println("Quarters: " + b);
System.out.println("Dimes: " + d);
System.out.println("Nickels: " + f);
System.out.println("Pennies: " + g);
}
}