Archived

This topic is now archived and is closed to further replies.

fadilthrejk

[java] Hmmm...algorigthm troubles...or something

Recommended Posts

fadilthrejk    100
basically, in Java, how would you accept a dollar amount as input from a user, then figure out how many quarters, dimes, nickels, and pennies, exactly, are in that amount? here''s the best way i''ve found to do it: double cash = (input...whatever); double quarters = (int)(cash * 4); double dimes = (int)((cash % .25) * 10); double nickels = (int)((cash % .10) * 20); double pennies = (int)((cash % .05) * 100); alas, a problem arises when you try to output the expressions. when attempts to make the output an integer. if you cast them to integers, sometimes they become zero and that''s no good. so i tried rounding them...i can''t get that right, because it always wants to screw up one way or another. the input needs to be a double, and the output has to be an integer. damn. i had this figured out once, but i lost it. actually, i came here like 9 months ago to figure this out and i got it workin.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
take just the cash as double and leave the others as int, modulus should work fine on those...

Share this post


Link to post
Share on other sites
fadilthrejk    100
it is homework. i know i''m not supposed to ask for answers, and in truth, i''m not. i just want to know if there''s a way to get past java''s strongly typed-ness, which prevents me from having the quarter, nickel, dime, and penny variables be integers.

Share this post


Link to post
Share on other sites
Thr33d    382
Look up casting (or cast, or static cast, or dynamic cast) in your java book. It's the starting point to "getting around java's strongly typed-ness"

And in case you don't catch on, your prens are in the wrong place. Think about in Math - the order of operations. Then think about the limitations of modulus (% operator)
Then... You've got it.

-Michael

[edited by - Thr33d on October 13, 2003 8:05:00 PM]

Share this post


Link to post
Share on other sites
fadilthrejk    100
here''s what i decided to do:

public static void main(String[] args)
{
System.out.println("How much money do you have?");
double cash = Keyboard.readDouble();

double quarters = cash / .25;
double dimes = 0;
double nickels = 0;
double pennies = 0;

double qRemainder = cash % .25;
double dRemainder = qRemainder % .10;
double nRemainder = dRemainder % .05;

if (qRemainder != 0){
dimes = qRemainder / .10;
}

if ((qRemainder != 0) && (dRemainder != 0)){
nickels = dRemainder / .05;
}

if ((qRemainder != 0) && (dRemainder != 0) && (nRemainder != 0)){ //Somewhere I am off in the pennies occasionally, by no
pennies = nRemainder / .01; //more than a penny. why???
}

System.out.println("You have " + (int)quarters + " quarters");
System.out.println("You have " + (int)dimes + " dimes");
System.out.println("You have " + (int)nickels + " nickels");
System.out.println("You have " + (int)pennies + " pennies");



the pennies is the only problem now...it''s kooky. i don''t see where i''m losing a penny anywhere.

Share this post


Link to post
Share on other sites
grhodes_at_work    1385
fadilthrejk,

Thank you for clarifying that you're just looking at a Java issue, not really the math behind this. And thanks also for pasting your work at the end. Since in the end this became a Java question, I am moving the thread to the Java forum,

Gamedev Java Development Forum

[edit: er, when I moved this, I thought that it had been posted to math & physics. Heck, it might have been. But I had two windows open, one to For Beginners and I believe this thread may have been started in For Beginners, in which case it was probably okay. I apologize if I moved it out of line...]

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

[edited by - grhodes_at_work on October 15, 2003 3:00:16 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
ah yeah... if the cs geeks are coming here to get their homework solved, i fear for our future!

Share this post


Link to post
Share on other sites
grhodes_at_work    1385
quote:
Original post by Anonymous Poster
ah yeah... if the cs geeks are coming here to get their homework solved, i fear for our future!


I wouldn''t worry too much. There are always those who rise to the top. Those who cheat and never learn may have a hard time keeping a job or rising through the ranks. And will fast get a reputation in the workplace!

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
You''re probably losing the penny due to rounding errors in the floating-point types. The same problem occured in the *judge''s* code for a change-making problem at the ACM pacific northwest regional programming competition last year... Made a right mess of things while they sorted that out.

I suggest that you not use floating-point types anywhere. Find a way to solve the problem entirely in integers. (And yes, there are many ways to do it.)

Share this post


Link to post
Share on other sites
CaptainJester    523
I ran your program a few times and got accurate results each time. What number are you entering that is not working correctly?



First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
if you need more precision you could use the class BigDecimal, which has arbitrary precision.

but personally i''d prefer using only integers. an algorithm with only ints isn''t too hard.

-smoke

Share this post


Link to post
Share on other sites