Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (anonymous):

Java help please? Sort change into dollars, quarters, dimes, nickels and pennies using integer division?

OpenStudy (anonymous):

Is the change in dollars or cents? I am assuming it is dollars. I can help you by providing pseudo-code. I hope you can code in Java. If you don't know how to code in Java, then let me know. 1) Create an integer variable dollars. it should be equal to change/1. Remember: since you are forcing an integer division, you will get an integer quotient which will the dollars in the change. 2) Subtract dollars from change and store it in a double variable (say new_change). 3) Create another integer variable quarters. It should be equal to new_change/0.25 4) Subtract the variable quarters from the variable new_change and store it in new_change again (if you don't know how to do it, then store it in another double variable new_change1) 5) Create an integer variable dimes. dimes = new_change1/0.10 6) Subtract dimes from new_change1 and store it in double new_change2. Continue that way until pennies. Any doubts? Ask!

OpenStudy (kainui):

When you say integer division, if you have $3.52 we can represent this as the integer: 352. So what I'd do is slightly different, I'm going to use the % which is the modulus operator and all it does is give you the leftover after division.

OpenStudy (kainui):

public class ChangeSorter { static int m = 352; static int amountType; public static void main(String... args) { amountType = 100; System.out.print(gimmeChange(m, amountType) + " dollars, "); amountType = 25; System.out.print(gimmeChange(m, amountType) + " quarters, "); amountType = 10; System.out.print(gimmeChange(m, amountType) + " dimes, "); amountType = 5; System.out.print(gimmeChange(m, amountType) + " nickels, "); amountType = 1; System.out.print(gimmeChange(m, amountType) + " pennies."); } public static int gimmeChange(int c, int a) { c = m % a; c = (m - c) / a; m = m - (c * a); return c; } }

OpenStudy (kainui):

So, don't just completely rip off the code for your assignment or whatever, but play around with it and understand what's going on. I'm basically saying 352 was our $3.52 and you can put any number you want there. Then I made a nice little method that takes two parameters, the amount of money we have and the amount that we want to sort it into. Since we're descending, I also made the method subtract off all the money it already counted, that way it doesn't count every dollar in the original amount as 10 dimes since that was already paid out as 1 dollar.

OpenStudy (kainui):

Actually now that I'm rereading your question, I think the point was to exploit how integer division rounds down. In that case, don't even bother... My answer is wrong for your intent. At any rate, I'll leave it up just for funsies.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!