Question: need help on java code it wont pass Write a program that prompts the user for a count of quarters, dimes, nickels and pennies. The
need help on java code it wont pass
Write a program that prompts the user for a count of quarters, dimes, nickels and pennies. The program must output the total amount in dollars and cents. For example: Enter number of quarters: 6 Enter number of dimes: 2 Enter number of nickels: 0 Enter number of pennies: 1 You have $1.71 in coins. Note: you must ensure that the output properly shows two decimal places (e.g. $1.50 vs. $1.5) and, if any of the counts are negative, provide an error message after inputting all counts (supplied as a constant).
package edu.wit.cs.comp1050; import java.util.Scanner; //TODO: document this class public class PA1b { /** * Error message to display for any non-negative counts */ public static final String ERR_MSG = " All coin counts must be non-negative!";
// TODO: document this method
// TODO: write your code here //Error message to display for any non-negative counts /*method to prompt the user for a count of quarters, dimes, nickels and pennie and convert and output the total amount in dollars and cents */ public static void main(String[] args) { /*declare double variables quarters,dimes,nickels,pennies and totalDollars and initialize totalDollars with 0 */ double quarters,dimes,nickels,pennies,totalDollars=0; //create an instance of Scanner class named input Scanner input=new Scanner(System.in); //prompt user to enter number of quarters System.out.print("Enter number of quarters:"); //read quarters quarters=input.nextDouble(); //prompt user to enter number of dimes System.out.print(" Enter number of dimes:"); //read dimes dimes=input.nextDouble(); //prompt user to enter number of nickels System.out.print(" Enter number of nickels:"); //read nickels nickels=input.nextDouble(); //prompt user to enter number of pennies System.out.print(" Enter number of pennies:"); //read pennies pennies=input.nextDouble(); //if any of the count is negative,then show the error message if(quarters<0||dimes<0||nickels<0||pennies<0) System.out.println(ERR_MSG); /*otherwise calculate totalDollars 1 dollar=4 quarters=10 dimes=20 nickels=100 pennies */ else { totalDollars=quarters*.25+dimes*.1+nickels*.05+pennies*.01; //display the result System.out.printf(" You have $%.2f in coins.",totalDollars); }
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
