# Help with my Java program: Making change from an entered double.

Printable View

• March 17th, 2012, 21:56
iDizzle
Help with my Java program: Making change from an entered double.
Hi everyone. I am a beginner of beginners in Java. I'm not a CS or programming major but I am now taking a Java course because it is required. I look forward to hearing your insight.

I'm working on a program that is designed to make change from an entered value, only using Ten, Five, and One dollar bills and Quarters, Dimes and Nickels. The amount must round up to the nearest nickel. So for example if I entered \$35.77, the output should be something like;

Ten dollar bills: 3
Five dollar bills: 1
One dollar bills: 0
Quarters: 3
Dimes: 0
Nickels: 1 //because it is rounding up.

I'm very confused on what methods I should be using to write this program. This is what I have so far, can someone tell me what is wrong? Thank you in advance, much appreciated.

Basically what I've done so far is convert the amount entered into cents. Than dividing it by the appropriate amount to convert it into Tens, Fives, Ones etc. Than subtracting that from the amount entered. I declared a variable "change" and "difference" but I have not used them below yet. Thanks again!
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;

public class makingChange
{

public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
double amt,totalCents,change, difference;
double ten,five,one,quarter,dime,nickel;

ten=0;
five=0;
one=0;
quarter=0.0;
dime=0.0;
nickel=0.0;

System.out.println("Please enter the amount to be changed");
amt=scan.nextDouble();

totalCents= amt*100;

//difference=
ten=amt-(totalCents/1000);
five=amt-(totalCents/500);
one=amt-(totalCents/100);
quarter=amt-(totalCents/25);
dime=amt-(totalCents/10);
nickel=amt-(totalCents/5);

System.out.println("Ten dollar bills: " + ten);
System.out.println("Five dollar bills: " + five);
System.out.println("One dollar bills: " + one);
System.out.println("Quarters: " + quarter);
System.out.println("Dimes: " + dime);
System.out.println("Nickels: " + nickel);
}
}
• March 17th, 2012, 22:12
pbrockway2
Re: Help with my Java program: Making change from an entered double.
Quote:

can someone tell me what is wrong?
It would help very much if *you* said what was wrong.

Specifically, does that code compile? If not, and you can't understand the compiler's messages, post those messages and indicate which lines of your code they are referring to. I am sure someone can explain what they mean.

Or does the code compile, but not give the output you expected or intended when you run it? In that case you have described the output you want for some specific input, but what output do you actually get for that input? Or is there a runtime exception? In either case, post the actual output that you see.
• March 17th, 2012, 22:28
iDizzle
Re: Help with my Java program: Making change from an entered double.
Quote:

Originally Posted by pbrockway2
It would help very much if *you* said what was wrong.

Specifically, does that code compile? If not, and you can't understand the compiler's messages, post those messages and indicate which lines of your code they are referring to. I am sure someone can explain what they mean.

Or does the code compile, but not give the output you expected or intended when you run it? In that case you have described the output you want for some specific input, but what output do you actually get for that input? Or is there a runtime exception? In either case, post the actual output that you see.

Sorry. No errors, I just don't get the output I need.

I get this;

Please enter the amount to be changed
\$35.77

Ten dollar bills: 32.193000000000005
Five dollar bills: 28.616000000000003
One dollar bills: 0.0
Quarters: -107.31
Dimes: -321.93000000000006
Nickels: -679.6300000000001
• March 17th, 2012, 23:07
pbrockway2
Re: Help with my Java program: Making change from an entered double.
Quote:

Ten dollar bills: 32.193000000000005
This indicates a couple of things. First (obviously) you can't have 32.19... ten dollar bills. In fact the number of bills will be a whole number (0, 1, 2, ...) without a fraction. In your code it would be better for ten to be declared this way: as an int rather than a double. Something similar should be done to other (but not all) variables in the code.

The second thing concerns the value (thirty two point something). You'll go out of business at that rate! Clearly there is something awry with the arithmetic.

Code :

`ten=amt-(totalCents/1000);`

This line is making the number of ten dollar bills equal to amt - which is the number of dollars - minus something small - which is why the answer is so large.

You are starting with 35.77 dollars, or, equivalently, 3577 cents. Try and think of a simple formula that will give the answer 3 starting from this value. In general you want the number two places to the left of the decimal place. (or three places from the end if you are working in cents.)

Make sure you understand how integer division works. If you change the type of the variables as indicated above, then integer division quickly leads to a more sensible formula for the number of ten dollar bills.
• March 18th, 2012, 00:54
iDizzle
Re: Help with my Java program: Making change from an entered double.
Thank you! Okay i will do a check on the arithmetic. If I change the dollar amounts to int values, wouldn't I have problems because the "amt" is a double value? Would I have to use some kind of data conversion?
• March 18th, 2012, 02:44
iDizzle
Re: Help with my Java program: Making change from an entered double.
Okay so I figured out how to cast a double to an int. But I'm still having trouble with the arithmetic to subtract from the total. If you look at my code below, all it's going to output is change in each type of currency. I'm confused on how to subtract the calculated currency from the amount entered so that the number of currency can be calculated from the new amount.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;

public class makingChange
{

public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
double amt;
int ten,five,one,quarter,dime,nickel,totalCents;

System.out.println("Please enter the amount to be changed");
amt=scan.nextDouble();

totalCents=(int)amt;
totalCents= (int) (100*amt);

ten=(totalCents/1000);
five=(totalCents/500);
one=(totalCents/100);
quarter=(totalCents/25);
dime=(totalCents/10);
nickel=(totalCents/5);

System.out.println("Ten dollar bills: " + ten);
System.out.println("Five dollar bills: " + five);
System.out.println("One dollar bills: " + one);
System.out.println("Quarters: " + quarter);
System.out.println("Dimes: " + dime);
System.out.println("Nickels: " + nickel);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
So now the output is;

Please enter the amount to be changed
35.77
Ten dollar bills: 3
Five dollar bills: 7
One dollar bills: 35
Quarters: 143
Dimes: 357
Nickels: 715
• March 18th, 2012, 03:11
pbrockway2
Re: Help with my Java program: Making change from an entered double.
Quote:

I'm confused on how to subtract the calculated currency from the amount entered so that the number of currency can be calculated from the new amount.
You subtract using - :)

The thing is to subtract each time you have figured out one of the values:

Code :

```ten=(totalCents/1000); totalCents -= ten * 1000; // this line subtracts the ten*1000 cents already given in change five=(totalCents/500); // something similar needed here one=(totalCents/100); // etc```

-----

I wonder about the following calculation of the number of cents:

Code :

```totalCents=(int)amt; totalCents= (int) (100*amt);```

It's easy to check:

Code :

```System.out.println("Amount entered: " + amt); totalCents=(int)amt; totalCents= (int) (100*amt); System.out.println("Cents: " + totalCents);```

To make matters worse, you are actually looking to convert 35.77 dollars to 3580 cents. As you think about that remember that the (int) cast will always round downwards.
• March 18th, 2012, 03:14
iDizzle
Re: Help with my Java program: Making change from an entered double.
Okay. So I worked on it a little more and I think I almost got it. Except the fact that the nickel value is not rounding up. So 35.77 will not give me "1 nickel" from the 2 cents.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;

public class makingChange
{

public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
double amt;
int ten,five,one,quarter,dime,nickel,totalCents;

System.out.println("Please enter the amount to be changed");
amt=scan.nextDouble();

totalCents=(int)amt;
totalCents= (int) (100*amt);

ten=(totalCents/1000);
five=(int) ((amt-(ten*10))/5); //(totalCents/500);
one=(int) ((amt-(ten*10)-(five*5))/1);
quarter=(int) ((amt-(ten*10)-(five*5)-(one*1))/.25);
dime=(int) ((amt-(ten*10)-(five*5)-(one*1)-(quarter*.25))/.10);
nickel=(int) ((amt-(ten*10)-(five*5)-(one*1)-(quarter*.25)-(dime*.10))/.05);

System.out.println("Ten dollar bills: " + ten);
System.out.println("Five dollar bills: " + five);
System.out.println("One dollar bills: " + one);
System.out.println("Quarters: " + quarter);
System.out.println("Dimes: " + dime);
System.out.println("Nickels: " + nickel);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Please enter the amount to be changed
35.77
Ten dollar bills: 3
Five dollar bills: 1
One dollar bills: 0
Quarters: 3
Dimes: 0
Nickels: 0
• March 18th, 2012, 03:16
iDizzle
Re: Help with my Java program: Making change from an entered double.
[QUOTE=
To make matters worse, you are actually looking to convert 35.77 dollars to 3580 cents. As you think about that remember that the (int) cast will always round downwards.[/QUOTE]

uh oh... that's probably why. What can I do about this? =/
• March 18th, 2012, 03:19
iDizzle
Re: Help with my Java program: Making change from an entered double.
hmmm. Honestly, as you can see, I'm terrible at this. But even I feel like I'm doing this the hard way. If you look at my last code, I feel that I'm relying wayyy too much on math and arithmetic instead of using the java environment to its full potential. It just seems like there's an easier way to do this or am I wrong?
• March 18th, 2012, 03:29
iDizzle
Re: Help with my Java program: Making change from an entered double.
Are there any packages or classes than can be included that will do the rounding?
• March 18th, 2012, 03:35
iDizzle
Re: Help with my Java program: Making change from an entered double.
So I was searching online and I found something called "ceiling" or "cover" that might do what I want? Would these algorithms take care of my problem? And if they will, how do I use them?:rolleyes:
• March 18th, 2012, 03:46
pbrockway2
Re: Help with my Java program: Making change from an entered double.
Well, giving change is mostly about arithmetic. I don't think there is some API shortcut. The problem you were given is basically meant to make you think about the fussy arithmetic details.

Things like "nickel=(int) ((amt-(ten*10)-(five*5)-(one*1)-(quarter*.25)-(dime*.10))/.05);" look rather ugly, however. Did you understand the -= operator I used in the code I posted? It simplifies things greatly: both the arithmetic and the (unnecessary?) casts.

-----

Once you have determined the number of nickels you can check if you have any stray "pennies" left over. And if so add one to nickel.
• March 18th, 2012, 03:49
pbrockway2
Re: Help with my Java program: Making change from an entered double.
There is a ceil() method in the Math class. But, beware: it takes a double argument and returns a double value.

I think the approach I mentioned before (look for stray pennies and add 1 nickel if required) is the most conceptually straight forward. The good thing about working in cents (pennies) is that you can work with int rather than double all the way through the calculation.
• March 18th, 2012, 03:55
iDizzle
Re: Help with my Java program: Making change from an entered double.
I do not understand the -= operator. I don't believe we have learned that yet. I feel pretty crappy in that class because everyone is a CS/programming major so most people are very familiar with the stuff. =/.

So by adding a nickel to stray pennies do you mean adding .05 outside the last set of parenthesis in the nickel value?

You're right, the arithmetic in my code does look ugly. lol. And you have to understand that I AM NOT a math person either. I'm actually an anthropology major, but for some odd reason one of the schools I applied to for transfer requires a java programming course. So I sat here squinting at my scratch paper while doing the math. lol
• March 18th, 2012, 04:05
iDizzle
Re: Help with my Java program: Making change from an entered double.
Ok so for the nickel value I changed it to this..

nickel=(int) (((amt-(ten*10)-(five*5)-(one*1)-(quarter*.25)-(dime*.10))+.05)/.05);

I added the .05 there. and now it did give me the correct output. I tried it with a few different cent values too and it seems to work okay. But would there be a situation where the added .05 in the nickel value would give me an extra nickel when its not needed?

*edit* Ok so I tried \$1.05. And this time the output gave me;

Please enter the amount to be changed
1.05
Ten dollar bills: 0
Five dollar bills: 0
One dollar bills: 1
Quarters: 0
Dimes: 0
Nickels: 2

Would it make it better if I added .04 instead of the .05? That way it would be enough to round up to the next nickel without adding an extra nickel in the event it was an even \$1.05?
• March 18th, 2012, 04:24
pbrockway2
Re: Help with my Java program: Making change from an entered double.
First on the -= operator because I think it does simplify things a lot. x-=y is just a shorthand way of saying x=x-y or, in words "subtract y off x". But it's your code! Stay with what you're comfortable with.

What happens when the original amount ends in 5 cents? Like \$35.05.

The best thing would be to test it with each of the hundred values between \$35.00 and \$35.99.
• March 18th, 2012, 04:31
pbrockway2
Re: Help with my Java program: Making change from an entered double.
Yes 0.04 strikes me as better than 0.05
• March 18th, 2012, 04:51
iDizzle
Re: Help with my Java program: Making change from an entered double.
So -= is kind of like x++ or x-- from C? I think I will leave my code as is since it is working now but for future reference how would my code have looked if I had implemented the -=?

And again, thanks a lot for your help. I appreciate you not giving me hand outs, I actually understand what I did now. :o
• March 18th, 2012, 13:53
pbrockway2
Re: Help with my Java program: Making change from an entered double.
You're welcome - I'm glad you've got it doing what you want.

Yes, x-=1 is rather like --x. --x, x--,++x, x++ exist in Java too.

The code would look like that in #7.
• March 18th, 2012, 15:16
iDizzle
Re: Help with my Java program: Making change from an entered double.
Great, thanks again! I'll be frequenting this forum more often with java help =]
• May 6th, 2016, 19:59
Pixelfaced
Re: Help with my Java program: Making change from an entered double.
import java.util.Scanner;

public class makingChange {

public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);

double amt;
int ten,five,one,quarter,dime,nickel,penny,totalCents;

System.out.println("Please enter the amount to be changed");
amt=scan.nextDouble();

//deleted unnecessary code
totalCents= (int) (100*amt);

ten=(totalCents/1000);
five=(int) ((amt-(ten*10))/5);
one=(int) ((amt-(ten*10)-(five*5))/1);
quarter=(int) ((amt-(ten*10)-(five*5)-(one*1))*4);
dime=(int) ((amt-(ten*10)-(five*5)-(one*1)-(quarter*.25))*10);
nickel=(int) ((amt-(ten*10)-(five*5)-(one*1)-(quarter*.25)-(dime*.10))*20);
penny=(int) ((amt-(ten*10)-(five*5)-(one*1)-(quarter*.25)-(dime*.10)-(nickel*.05))*100.01); //100.01 for integer rounding reasons

System.out.println("Ten dollar bills: " + ten);
System.out.println("Five dollar bills: " + five);
System.out.println("One dollar bills: " + one);
System.out.println("Quarters: " + quarter);
System.out.println("Dimes: " + dime);
System.out.println("Nickels: " + nickel);
System.out.println("Pennies: " + penny);

}}

I fixed the Nickels by multiplying by 20 and added pennies. THIS IS THE PERFECTED PROGRAM!