# Thread: Need help for Java Class

1. Member Join Date
Aug 2010
Posts
6
Rep Power
0

## Need help for Java Class

Hi, I just started a Java class in college and am having a heck of a time with one of my exercises and I need a little help. I can't get this question:

Compute Change.java.

Here is the original program:

Java Code:
```import java.util.Scanner;

public class Listing_210_ComputeChange {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);

System.out.print(
"Enter an amount in double, for example 11.56: ");
double amount = input.nextDouble();

int remainingAmount = (int)(amount * 100);

// Find the number of one dollars
int numberOfOneDollars = remainingAmount / 100;
remainingAmount = remainingAmount % 100;

// Find the number of Quarters
int numberOfQuarters = remainingAmount / 25;
remainingAmount = remainingAmount % 25;

// Find the number of dimes in the remaining amount
int numberOfDimes = remainingAmount / 10;
remainingAmount = remainingAmount % 10;

// Find the number of  nickels in the remaining amount
int numberOfNickles = remainingAmount / 5;
remainingAmount = remainingAmount % 5;

// Find the number of pennies in the remaining amount
int numberOfPennies = remainingAmount;

// Display results
System.out.println("Your amount " + amount + " consists of \n" +
"\t" + numberOfOneDollars + " dollars\n" +
"\t" + numberOfQuarters + " quarters\n" +
"\t" + numberOfDimes + " dimes\n" +
"\t" + numberOfPennies + " pennies");
}
}```
Output:
Enter an amount in double, for example 11.56: 10.03
10 dollars
0 quarters
0 dimes
2 pennies
** NOTICE the 2 pennies. which leads to the problem:

My assignment is "...to fix the possible loss of accuracy when converting a double value to an int value. Enter the input as an integer whose last two digits represent the cents. For example, the input 1003 represents 10 dollars and 03 cents."

Here my solution:

Java Code:
```import java.util.Scanner;

public class Exercise_29_FinancialApplicationMonetaryUnits {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);

// Receive the amount in cents
System.out.print(
"Enter an amount as an integer, for example 1156: ");
int amount = input.nextInt();

// Find the number of one dollars
int numberOfOneDollars = amount / 100;
amount = amount % 100;

// Find the number of Quarters
int numberOfQuarters = amount / 25;
amount = amount % 25;

// Find the number of dimes in the remaining amount
int numberOfDimes = amount / 10;
amount = amount % 10;

// Find the number of  nickels in the remaining amount
int numberOfNickles = amount / 10;
amount = amount % 10;

// Find the number of pennies in the remaining amount
int numberOfPennies = amount;

// Display results
System.out.println("Your amount " + amount + " consists of \n" +
"\t" + numberOfOneDollars + " dollars\n" +
"\t" + numberOfQuarters + " quarters\n" +
"\t" + numberOfDimes + " dimes\n" +
"\t" + numberOfNickles + " nickles\n" +
"\t" + numberOfPennies + " pennies");
}
}```
Here is the output:
Enter an amount in int, for example 1156: 1003
10 dollars
0 quarters
0 dimes
0 nickles
3 pennies

** NOTICE the "3" in "Your amount...". Should be "1003". As in "Your amount 1003 consists of".

I've tried all sorts of different variations but cannot figure out how to get the 1003 in the wording. Where am I going wrong? I just can't see it.

Thanks for any help
Last edited by Fubarable; 08-28-2010 at 05:01 AM. Reason: quote tags changed to code tags  Reply With Quote

2. ## It's already solved for you above -- so just do what is done in the original program -- don't change the amount variable, but rather create and use a remainingAmount variable just like your example code uses. You'll also want to fix your nickel calculations to match the original program.

Also, I changed your quote tags to code tags so that your code doesn't lose its formatting. In the future you shouldn't use quote tags for code.

Good luck.  Reply With Quote

3. ## Oh, and welcome to our forum!!  Reply With Quote

4. Member Join Date
Aug 2010
Posts
6
Rep Power
0

##  Originally Posted by Fubarable It's already solved for you above -- so just do what is done in the original program -- don't change the amount variable, but rather create and use a remainingAmount variable just like your example code uses. You'll also want to fix your nickel calculations to match the original program.

Also, I changed your quote tags to code tags so that your code doesn't lose its formatting. In the future you shouldn't use quote tags for code.

Good luck.
Thanks for the quick response!
Sorry for the QUOTE tag, I completely overlooked the # code. Its on about 12 am and i've been at this for 5 hours now. Little cock-eyed. And the 4 Guiness doesn't help.

It took me a while to understand what you meant, but i think i got it. Is this correct:

Java Code:
```// Receive the amount
System.out.print(
"Enter an amount an an integer, for example 1156: ");
int amount = input.nextInt();

[COLOR="Red"]int remainingAmount = amount ;[/COLOR]

// Find the number of one dollars
int numberOfOneDollars = remainingAmount / 100;
remainingAmount = remainingAmount % 100;```
Output:
Enter an amount in double, for example 11.56: 1003
10 dollars
0 quarters
0 dimes
0 nickles
3 pennies

Which is what it should be! I mean is it correct as far as formatting? Never mind that doesn't make sense. I guess what I'm saying is i see what I did, but it hasn't hit me WHY I did it. Why did I have to define remainingAmount = amount. Maybe I'm just too tired to do the computations.

Also, i forgot to put the nickels i the orginal program. Eclipse was screaming at me and i ignored it.

Anyway, Thanks so much for the tip! I like when I can get a nudge, and not a full "here's the answer". It makes me think and learn what I need to do.

I'm sure i'll be back for some more!

--Stitch
Last edited by USMCstitch; 08-28-2010 at 05:33 AM.  Reply With Quote

5. ## The key to understanding this is to understand what each line of code does. Take this line for instance:
Java Code:
`amount = amount % 25;`
The right hand side of this line, "amount % 25", means mod amount by 25, or in other words, divide amount by 25 and keep just remainder. So if amount is 85, amount divided by 25 is 3 with a remainder of 10, and that means that amount mod 25 (amount % 25) equals 10.

Now the key here is the left hand side of the code above:
Java Code:
`amount = ....`
which means take what ever's on the right hand side and set amount equal to that value. In other words, you are changing the value of amount -- and this is key. Eventually when you're done going through all the equations, if amount started out as 1003, it will end as 3, as you're finding out. The way to prevent this is to not change the value that amount holds, and the way to do this is to create a dummy variable, here remainingAmount, that is given the value held by amount, and then that gets changed instead. Make sense?  Reply With Quote

6. Member Join Date
Aug 2010
Posts
6
Rep Power
0

##  Originally Posted by Fubarable The right hand side of this line, "amount % 25", means mod amount by 25, or in other words, divide amount by 25 and keep just remainder. So if amount is 85, amount divided by 25 is 3 with a remainder of 10, and that means that amount mod 25 (amount % 25) equals 10.
This part I got. That was easy. Originally Posted by Fubarable Now the key here is the left hand side of the code above:
Java Code:
`amount = ....`
which means take what ever's on the right hand side and set amount equal to that value. In other words, you are changing the value of amount -- and this is key. Eventually when you're done going through all the equations, if amount started out as 1003, it will end as 3, as you're finding out. The way to prevent this is to not change the value that amount holds, and the way to do this is to create a dummy variable, here remainingAmount, that is given the value held by amount, and then that gets changed instead. Make sense?
Sukat, his eyes uncovered!. This makes sense. I am now seeing the code in a whole new light. Its so simple now that I know what to look for/at. Thanks again!  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•