Write a complete Java program that prompts for and reads a double value represen
ID: 3620753 • Letter: W
Question
Write a complete Java program that prompts for and reads a double value representing a monetary amount.Then determine the fewest number of each bill and coin needed to represent that amount, starting with the highest assuming that a ten dollar bill is the maximum size needed.I'm completely new to Java, so I don't really have that much experience with it. So far this is what I have compiled in my java program, but I'm having problems with converting double to integer which keeps reading error messages. Any help or suggestions would be greatly appreciated!
// This program displays money in monetary units
import java.util.Scanner; // to be able to read from the keyboard
public class Money
{
public static void main(String [] args)
{
double amount; //This displays user's amount
int one, five, ten;
int penny, nickel, dime, quarter;
// Create a Scanner object to read input.
Scanner kb = new Scanner(System.in);
// Get the user's amount
System.out.print("What is the amount? ");
amount = kb.nextDouble();
// Calculations
ten = amount/10;
amount = amount%10;
five = amount/5;
amount = amount%5;
> amount = amount%1;
quarter = amount/.25;
amount = amount%.25;
dime = amount/.10;
amount = amount%.10;
nickel = amount/.05;
amount = amount%.05;
penny = amount/.01;
amount = amount%.01;
// Display the results
System.out.println(ten + "tens. ");
System.out.println(five + "fives. ");
System.out.println(one + "ones. ");
System.out.println(quarter + "quarters. ");
System.out.println(dime + "dimes. ");
System.out.println(nickel + "nickels. ");
System.out.println(penny + "pennies. ");
}
}
Explanation / Answer
Dear, Small modification needed which are highlighted with red font import java.util.Scanner; // to be able to read from the keyboard public class money { public static void main(String [] args) { double amount; //This displays user's amount int one, five, ten; int penny, nickel, dime, quarter; // Create a Scanner object to read input. Scanner kb = new Scanner(System.in); // Get the user's amount System.out.print("What is the amount? "); amount = kb.nextDouble(); // Calculations ten = (int) amount/10; amount = amount%10; five = (int) amount/5; amount = amount%5; one = (int) amount/1; amount = amount%1; quarter = (int) (amount/0.25); amount = amount%25; dime = (int) (amount/0.10); amount = amount%10; nickel = (int) (amount/0.5); amount = amount%5; penny = (int) (amount/0.01); amount = amount%1; // Display the results System.out.println(ten + "tens. "); System.out.println(five + "fives. "); System.out.println(one + "ones. "); System.out.println(quarter + "quarters. "); System.out.println(dime + "dimes. "); System.out.println(nickel + "nickels. "); System.out.println(penny + "pennies. "); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.