#### Archived

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

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

This topic is 5453 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
Is this homework perhaps?

##### Share on other sites
take just the cash as double and leave the others as int, modulus should work fine on those...

##### Share on other sites
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 on other sites
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 on other sites
well, i think i have it, i just was casting at the wrong point in each equation. i''m gonna try it differently now.

##### Share on other sites
here''s what i decided to do:

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

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 on other sites

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 on other sites
ah yeah... if the cs geeks are coming here to get their homework solved, i fear for our future!

##### Share on other sites
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.

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
16
5. 5

• 9
• 12
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632593
• Total Posts
3007277

×