Can Java not do math or somthing?

EyeMWing

Banned
Jun 13, 2003
15,670
1
0
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);
}
}
 

PrincessGuard

Golden Member
Feb 5, 2001
1,435
0
0
The problem is the way floats are defined. They do not have infinite precision so 0.13 may not have an exact float representation.

You can read in a float, multiply it by 100, round, and store the integer part.

And please use more decriptive variable names.
 

Drekce

Golden Member
Sep 29, 2000
1,398
0
76
Shouldn't a person understand the BASICS of data types before beginning to program?
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
you'll get this problem in any language where you use the ieee definition of a float. there are math packages out there that give you better precision if you need it.
 

EyeMWing

Banned
Jun 13, 2003
15,670
1
0
Yes, I know my variable names are crap. Kinda hard to shake old habits (this one goes back to TI-BASIC and it's single-character variables).

And I actually went through the chapter on data types a half dozen times trying to figure it out before I asked.

Finally, re: multiplying and rounding - heh, it works. Albeit it's a little messy, but whatever works.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
That's the ugliest piece of sh!t of a program I've ever seen. If I was your teacher and you turned that in to me you'd get an F, even if it worked.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: notfred
That's the ugliest piece of sh!t of a program I've ever seen. If I was your teacher and you turned that in to me you'd get an F, even if it worked.

LOL :D
 
Aug 16, 2001
22,505
4
81
Originally posted by: notfred
That's the ugliest piece of sh!t of a program I've ever seen. If I was your teacher and you turned that in to me you'd get an F, even if it worked.

LOL!

I guess you were not around when memory was scarse.
;)

 

Electric Amish

Elite Member
Oct 11, 1999
23,578
1
0
Originally posted by: notfred
That's the ugliest piece of sh!t of a program I've ever seen. If I was your teacher and you turned that in to me you'd get an F, even if it worked.

I was thinking the same thing. ;)
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
My guess is that there's something screwy with input class and/or the way you convert to double...

This works best for console imo. It's best to use applets in Java...but whatever...here we go...I'm not gonna give you everything...just the lines you'd need to use it.

import java.io.*;

BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String s = buf.readLine();
double d = Double.parseDouble(s);

Explanation:
System.in is essentially the input stream for the program (input stream = thing that understands keyboard/file inputs). InputStreamReader is a very basic class that has some simple functions to read from the inputstream. BufferedReader is a class that "wraps" around InputStreamReader, provinding additional functionality (for example, a readLine() function). Double is a class that gives you a lot of functionality with double primitive types. For example, it holds the function parseDouble which will take the string you pass it and return the double (primitive type) value of that string. NOTE: the string can only have ONE number in it. For example, if s = 205.28, this code would work. If is = 205 260 (2 #s), parseDouble would fail. Java also defines similar classes for ints, floats, etc--Integer, Float, etc. Try Sun's class index for more info.

edit: btw, no offense...but I agree with notfred. Your program can be...massively simplified. But I'm just going to give you the (what I've found to be, at least) accepted method for inputting double values.
 

Sunner

Elite Member
Oct 9, 1999
11,641
0
76
Originally posted by: 911paramedic
<HTML>
<HEAD>
<BODY>
<B>Damn, you guy's are cruel!</B>
</BODY>
</HTML>

copy/paste

:D

What?
No <title>, and you're leaving me with that ugly grey background most browsers use when you don't set it???

Your HTML sucks!

;)
 

PrincessGuard

Golden Member
Feb 5, 2001
1,435
0
0
Originally posted by: eLiu
My guess is that there's something screwy with input class and/or the way you convert to double...

This works best for console imo. It's best to use applets in Java...but whatever...here we go...I'm not gonna give you everything...just the lines you'd need to use it.

import java.io.*;

BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String s = buf.readLine();
double d = Double.parseDouble(s);

The problem isn't that he can't read in doubles; it's that he loses precision when doing so. Using parseDouble() won't fix that. If you really want to get down to it, you could use DecimalFormat to parse() the whole thing, including dollar sign, into a BigDecimal.
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
Originally posted by: PrincessGuard
Originally posted by: eLiu
My guess is that there's something screwy with input class and/or the way you convert to double...

This works best for console imo. It's best to use applets in Java...but whatever...here we go...I'm not gonna give you everything...just the lines you'd need to use it.

import java.io.*;

BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String s = buf.readLine();
double d = Double.parseDouble(s);

The problem isn't that he can't read in doubles; it's that he loses precision when doing so. Using parseDouble() won't fix that. If you really want to get down to it, you could use DecimalFormat to parse() the whole thing, including dollar sign, into a BigDecimal.

Hmmm...is it a problem with compilers then or something? Because I've never gotten any strange roundoff problems like that using parseDouble (hence why I don't usually bother with BigDecimal...though it is really neat imo). I'm using Eclipse (latest version..2.1.1 or something like that) if it matters.

Btw...I've only been working w/Java for about 1/2 month (it's my first real programming experience)...soo >.< lol. I'm in an AP CS class too, though I find the stuff too easy...latest thing was an expression evaluator -- *, /, -, +, ^, (, and ) were the required ones. I finished way early so I've been messing around with display stuff...it's so much fun hehe :)

-Eric
 

charlie21

Senior member
Oct 10, 1999
491
0
76
Originally posted by: notfred
That's the ugliest piece of sh!t of a program I've ever seen. If I was your teacher and you turned that in to me you'd get an F, even if it worked.
Wow, that's harsh. Kid's taking a high school programming class. I bet your first programs were a work of art.
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
Originally posted by: charlie21
Originally posted by: notfred
That's the ugliest piece of sh!t of a program I've ever seen. If I was your teacher and you turned that in to me you'd get an F, even if it worked.
Wow, that's harsh. Kid's taking a high school programming class. I bet your first programs were a work of art.

Eh..past hello world, this is the first thing I wrote (looks weird b/c the indentions dont' carry over, but whatever)

#include<apvector.h>
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>

const int index = 100;

void Quicksort(apvector <int> &a, int first, int last);
void swap(int &a, int &b);

int main()
{
apvector <int> a(index);
int temp;

for(int n=0; n<index; n++)
a[n]=rand()%100;

for(int c=0;b<index;c++)
cout<<a[c]<<endl;

cout<<endl;

Quicksort(a,0,index-1);

for(int c=0;b<index;c++)
cout<<a[c]<<endl;


getch();
}

void Quicksort(apvector <int> &a, int first, int last)
{
int pivot=first;
int left=first+1;
int right=last;
if(first<last)
{
while (left<right)
{
while (left<last && a
<=a[pivot])
left++;
while (right>pivot && a
>=a[pivot])
right--;
if(left<right &&(a
<a[pivot] && a
>a[pivot]))
{
swap(a
,a
);
right--;
left++;
}
}

if(a
<a[pivot])
{
swap(a
,a[pivot]);
pivot=right;
}

Quicksort(a,first,pivot-1);
Quicksort(a,pivot+1,last);
}
}

void swap(int &a, int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}


Anyway, it's a quicksort coded in C++. apvector is a class written by the CollegeBoard (ppl who do AP exams) that simplifies arrays slightly (biggest advantage is that it's impossible to kill yourself with an out of bounds error). Anyway...I wrote this after just reading a book...no formal instruction or anything; it was right before the start of senior year. It aint the prettiest thing ever, no...but it's a shot better than a lot of stuff I've seen from kids w/more experience, sooo...​