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:
Output: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:
Here is the output: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 help

