Results 1 to 6 of 6
Thread: Need help for Java Class
 08282010, 05:57 AM #1Member
 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); // Receive the amount 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"); } }
Enter an amount in double, for example 11.56: 10.03
Your amount 10.03 consists of
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"); } }
Enter an amount in int, for example 1156: 1003
Your amount 3 consists of
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 helpLast edited by Fubarable; 08282010 at 06:01 AM. Reason: quote tags changed to code tags

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.

 08282010, 06:28 AM #4Member
 Join Date
 Aug 2010
 Posts
 6
 Rep Power
 0
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 cockeyed. 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;
Enter an amount in double, for example 11.56: 1003
Your amount 1003 consists of
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!
StitchLast edited by USMCstitch; 08282010 at 06:33 AM.

The key to understanding this is to understand what each line of code does. Take this line for instance:
Java Code:amount = amount % 25;
Now the key here is the left hand side of the code above:
Java Code:amount = ....
 08282010, 07:14 AM #6Member
 Join Date
 Aug 2010
 Posts
 6
 Rep Power
 0
Similar Threads

E:\IT 215 Java Programming\GUI1.java:125: class, interface, or enum expected
By tlouvierre in forum New To JavaReplies: 1Last Post: 05312009, 11:57 PM 
E:\IT 215 Java Programming\GUI1.java:125: class, interface, or enum expected
By tlouvierre in forum New To JavaReplies: 1Last Post: 05312009, 03:17 PM 
E:\IT 215 Java Programming\Inventory.java:36: class, interface, or enum expected
By tlouvierre in forum Advanced JavaReplies: 16Last Post: 05282009, 04:41 PM 
E:\IT 215 Java Programming\public class Inventory.java:39: class, interface, or enum
By tlouvierre in forum New To JavaReplies: 14Last Post: 05282009, 06:44 AM 
class.java to class.exe
By f_the_cook in forum New To JavaReplies: 11Last Post: 07162008, 03:24 PM
Bookmarks