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

Started by
13 comments, last by fadilthrejk 20 years, 6 months ago
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.
Advertisement
Is this homework perhaps?
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
take just the cash as double and leave the others as int, modulus should work fine on those...
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.
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]
well, i think i have it, i just was casting at the wrong point in each equation. i''m gonna try it differently now.
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.
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]
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
ah yeah... if the cs geeks are coming here to get their homework solved, i fear for our future!
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.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net

This topic is closed to new replies.

Advertisement