## galacticwavesXX 2 years ago Can someone help me with my java code? i am stuck on how to come up with an mathematical equation that can take any amount between 0 - 999.99 and break it into smaller monetary amounts

1. galacticwavesXX

my code so far is import java.util.Scanner; //Need Scanner class public class Money { public static void main(String[] args) { double amount; //To hold the monetary amount int newAmount; //To hold the total amount of Dollars double amount_Of_Coins; //To hold the total amount of coins int bill20, bill10, bill5, bill1; //To hold the units of bills of the monetary amount int coin25, coin10, coin5,coin1; //To hold the units of coins of the monetary amount Scanner keyboard = new Scanner(System.in); //Ask the user for the monetary amount System.out.println("Enter a monetary amount between 0 - 999.99: "); amount = keyboard.nextDouble(); newAmount = (int)amount;

2. e.mccormick

You can use division and subtraction to split off parts.

3. galacticwavesXX

So for the division i can use to split the left side and right side of a value....supposing the value was 36.41. Subtraction could be used to calculate the amount of coins there are for quarters, dimes, nickels and pennies, right?

4. e.mccormick

$432.11 First, *100 to get it into integer math. 43211 Divide by 10000 to find the number of 100 bills. b100 = 4 Subtract 4*b100 to get 3211. Divide by 2000 to get the number of$20 bills. b20=1 and so on...

5. galacticwavesXX

im sorry openstudy just stopped working for the longest so i couldn't do anything but try to get the code working a little. I got the dollar bills to work fine...now im stuck on the coins.

6. galacticwavesXX

the code i have so far is: import java.util.Scanner; //Need Scanner class public class Money { public static void main(String[] args) { double amount; //To hold the monetary amount Scanner keyboard = new Scanner(System.in); //Ask the user for the monetary amount System.out.println("Enter a monetary amount between 0 - 999.99: "); amount = keyboard.nextDouble(); int newAmount; //To hold new amount newAmount = (int)amount; int bill20 = newAmount/20; newAmount %= 20; int bill10 = newAmount/10; newAmount %= 10; int bill5 = newAmount/5; newAmount %= 5; int bill1 = newAmount/1; newAmount %= 1; System.out.println(bill20 + " amount of 20 dollar bills"); System.out.println(bill10 + " amount of 10 dollar bills"); System.out.println(bill5 + " amount of 5 dollar bills"); System.out.println(bill1 + " amount of 1 dollar bills");

7. galacticwavesXX

@thomaster can you help me on how to solve for coins in my java program?

8. e.mccormick

That is why I said start with multiplying by 100. But if you get it don to just the coins, you could multiply by 100 there and then it is the same basic thing.

9. galacticwavesXX

So using your example of $432.11, for doing it with the dollar bills i'm multiplying 432.11 * 100. or just in the form .11 * 100 part? 10. e.mccormick Either way works, but you have the dollars work out. So you take the remainder, .11, and go from there. *100 and it will be cents as in per 100. /25 for quarters, mode 25 to get what is left to get the dimes, etc. 11. galacticwavesXX ok so i can put in a mathematical operation such as dollar coin = newAmount - amount; since the user has to put an amount and see if the monetary system runs then get quarter = (coin * 100)/25; quarter %= 25; 12. galacticwavesXX since it all has to be keyboard input 13. e.mccormick To coninue the way you had it.... int bill1 = newAmount/1; newAmount %= 1; newAmount *= 100; int coin25 = newAmount/25; newAmount %= 25; int coin10 = newAmount/10; newAmount %= 10; int coin5 = newAmount/5; int coin1 = newAmount % 5; 14. e.mccormick Hmmm.... looks like you already stripped the ints... so... more like: int newAmount; //To hold new amount newAmount = (int)amount; int newCents; // To hold the cents newCents = (int)((amount-newAmount)*100) 15. e.mccormick Then use that to find the cents... 16. e.mccormick or to find the coins... 17. galacticwavesXX ohh okay that makes more sense.... trying to calculate for the coins was tricky...i didnt know you had to multiply it by 100. but thanks man, you help me out a LOT!! 18. e.mccormick Yah, the 100 is to change from a decimal to an int. Then everything else you did works. 19. galacticwavesXX so i input int coin25 = newCents/25; newCents %= 25; 20. e.mccormick Cent and percent have the same base. Per Centum, or Per 100. Well, per 100 has another representation: $$\dfrac{x}{100}$$ It is the same thing about converting percentage. 21. galacticwavesXX ohh so divide everything by 100 then 22. e.mccormick Well, you want to go from a decimal to a whole number, so multiply by 100. 23. e.mccormick $$\dfrac{x}{100}\cdot 100 = x$$ And you want x cents. $$.11 \implies \dfrac{11}{100}$$ and you want 11. 24. e.mccormick Because you are going from .11 dollars to 11 cents. So really, unit conversion. 25. galacticwavesXX ok so i should input int quarter = newCents * 100; that is what your saying 26. galacticwavesXX i meant int coin25 27. e.mccormick No, because newCents is the entire cents. Then you use the same thing you did before: int coin25 = newCents/25; newCents %= 25; 28. e.mccormick Repeat for 10 and 5, no need to repeat for 1. Just use the %5 to get pennies. 29. e.mccormick Oh, and you may notice that what you type here collapses when people paste it. If you put  above and below on a seperate line it will do proper coding.  int newAmount; //To hold new amount newAmount = (int)amount; int newCents; // To hold the cents newCents = (int)((amount-newAmount)*100) ` int newAmount; //To hold new amount newAmount = (int)amount; int newCents; // To hold the cents newCents = (int)((amount-newAmount)*100) Try to copy and paste both of those. 30. galacticwavesXX Ohh okay i get it now, so many little things to look into and know....i need to practice more on these kinds of programs you want me to copy and paste it here or on my program? 31. e.mccormick I just meant for you to try it so you would know what I am talking about. 32. galacticwavesXX int newAmount; //To hold new amount newAmount = (int)amount; int newCents; // To hold the cents newCents = (int)((amount-newAmount)*100) int newAmount; //To hold new amount newAmount = (int)amount; int newCents; // To hold the cents newCents = (int)((amount-newAmount)*100) 33. e.mccormick For me it does this: int newAmount; //To hold new amount newAmount = (int)amount; int newCents; // To hold the cents newCents = (int)((amount-newAmount)*100) int newAmount; //To hold new amount newAmount = (int)amount; int newCents; // To hold the cents newCents = (int)((amount-newAmount)*100) 34. e.mccormick Note how it messes up the bottom? Whee. 35. e.mccormick I wanted to do one detailed example because this actually applies to anything. Physics, geology, etc. you can use this. If you think of it in unit conversion terms it is a ratio: 1$:100¢ Therefore to find x$in ¢, you set thing up as fractions. Now, to convert, the units you convert from must cancel. $$.11 \implies \dfrac{11}{100}$$ Because$ is on the top, I need to set up my cancel ratio with it on the bottom: $$\dfrac{11}{100}\cdot \dfrac{100¢}{1}\implies$$ $$\dfrac{11\bcancel{}}{\cancel{100}} \cdot \dfrac{\cancel{100}¢}{1\bcancel{}} \implies$$ $$\dfrac{11}{1}\cdot \dfrac{¢}{1}=11¢$$

36. e.mccormick

If you have feet to inches or weight chanegs or anything with a unit change, that principal works.

37. galacticwavesXX

Yup i see it, i didnt know it made that much of a difference. so many things to know

38. e.mccormick

Well, it is just a general principal. In this case you are using it to get integer math for cents. Cause it makes sense... hehe.

39. galacticwavesXX

lol yeah its just when you program sometimes those general concepts slip out because of all the new language you need to remember or are trying to use properly

40. e.mccormick

Hehe. And it is a general concept in math and perhaps any other science you learned. That is why they teach all that math. It lets you use the general concepts in other things.

41. galacticwavesXX

Yeah with all the math classes i've taken they brought back geometry and algebra concepts but they weren't hard.....it's only the trig applied in calculus that kills integrity

42. e.mccormick

Well, calc uses evberything down to 1+1=2!

43. e.mccormick

$$\int x\rm dx$$ when you do that, you add one to the power, right? $$\int x\rm dx\implies \int x^1\rm dx\implies \dfrac{x^{1+1}}{1+1}+c$$ So in calculus you use 1+1=2. =)

44. galacticwavesXX

hahaha of course integration was fun even though there were so many kind of ways to do it, depending on the question given

45. e.mccormick

Yah. I just enjoy pointing out that everything finally comes together in math at about trig. From then on, it is "And you get to use everything you learned from preeschool on at this point." Parts of math before that seem unrelated or distant cousins at times. Once you hit trig, you use everything all at once. And calculus is the same, and inclused the trig. Linear Algebra can be used with calculus in it.... so really nasty at times. And so on.

46. galacticwavesXX

I'm in linear algebra right now.....Matrices seems too run the world and i just found out that Google is composed of matrices itself we are doing application problems to figure out if it has solutions or not, depending if the system is consistent or not working with matrices is a lot faster i might say

47. e.mccormick

Yah, it it invertable... does it have a determinant. Presto. Many qustions answered right there. Google uses a hash map. Hash maps can be represnted in matrices.

48. e.mccormick

This is one representation of the concept: http://www.alglib.net/matrixops/sparse.php

49. galacticwavesXX

Exactly makes solving problems less strenuous

50. e.mccormick

Another thing is you have rules about what is done. So you can apply the rules to the equation and get the result... let me see if I can find this thing. It was like an early question ansewered. Was not thinking when I did it the first time and redid it for the guy properly.

51. e.mccormick
52. galacticwavesXX

Ok, i'm still trying to understand this sparse matrices concept but i'm looking at it and i'm like i haven't learned some these things yet

53. e.mccormick

I only went to intro linear myself, so there are tons I don't get. Hehe.

54. e.mccormick

Well, I am going to sneak off. Have fun!

55. galacticwavesXX

you too thanks for the help!