Write a Java program that modifies Example 2.4, “ComputingChanges.” Display the
ID: 3617950 • Letter: W
Question
Write a Java program that modifies Example 2.4, “ComputingChanges.” Display the non-zero denominations only. Displaysingular words for single units like 1 dollar and 1 penny, anddisplay plural words for more than one unit like 2 dollars and 3pennies. (Use 23.67 to test your programs.) If the user enters zeroor a negative amount, your program should exit properly and displaya message stating that the amount entered by the user was zero ornegative.
If input is 0, your output should be:
Your amount is zero
If input is -2000.25, your output should be:
Your amount is negative
If input is 23.67, your output should be:
Your amount 23.67 consists of 23 dollars 2 quarters 1 dime 1nickel 2 pennies
Important Notes : The input and output must use JOptionPanedialog and display boxes.
Example 2.4
// ComputeChange.java: Break down an amount into smallerunits
import javax.swing.JOptionPane;
public class ComputeChange {
/** Main method */
public static void main(String[] args) {
double amount; // Amount entered from the keyboard
// Receive the amount entered from the keyboard
String amountString = JOptionPane.showInputDialog(null,
"Enter an amount in double, for example 11.56",
"Example 2.4 Input", JOptionPane.QUESTION_MESSAGE);
// Convert string to double
amount = Double.parseDouble(amountString);
int remainingAmount = (int)(amount * 100);
// Find the number of one dollars
int numOfOneDollars = remainingAmount / 100;
remainingAmount = remainingAmount % 100;
// Find the number of quarters in the remaining amount
int numOfQuarters = remainingAmount / 25;
remainingAmount = remainingAmount % 25;
// Find the number of dimes in the remaining amount
int numOfDimes = remainingAmount / 10;
remainingAmount = remainingAmount % 10;
// Find the number of nickels in the remaining amount
int numOfNickels = remainingAmount / 5;
remainingAmount = remainingAmount % 5;
// Find the number of pennies in the remaining amount
int numOfPennies = remainingAmount;
// Display results
String output = "Your amount " + amount + " consists of "+
numOfOneDollars + " dollars " +
numOfQuarters + " quarters " +
numOfDimes + " dimes " +
numOfNickels + " nickel " +
numOfPennies + " pennies";
JOptionPane.showMessageDialog(null, output,
"Example 2.4 Output", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
// Modified ComputeChange.java: Break down an amount intosmaller units
import javax.swing.JOptionPane;
public class ComputeChange {
/** Main method */
public static void main(String[] args) {
double amount; // Amount entered from the keyboard
// Receive the amount entered from the keyboard
String amountString = JOptionPane.showInputDialog(null,
"Enter an amount in double, for example 11.56",
"Example 2.4 Input", JOptionPane.QUESTION_MESSAGE);
// Convert string to double
amount = Double.parseDouble(amountString);
if(amount==0)
{
JOptionPane.showMessageDialog(null, "Your amount is zero",
"Example 2.4 Output", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
if(amount<0)
{
JOptionPane.showMessageDialog(null, "Your amount is negative",
"Example 2.4 Output", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
int remainingAmount = (int) (amount * 100);
// Find the number of one dollars
int numOfOneDollars = remainingAmount / 100;
remainingAmount = remainingAmount % 100;
// Find the number of quarters in the remaining amount
int numOfQuarters = remainingAmount / 25;
remainingAmount = remainingAmount % 25;
// Find the number of dimes in the remaining amount
int numOfDimes = remainingAmount / 10;
remainingAmount = remainingAmount % 10;
// Find the number of nickels in the remaining amount
int numOfNickels = remainingAmount / 5;
remainingAmount = remainingAmount % 5;
// Find the number of pennies in the remaining amount
int numOfPennies = remainingAmount;
// Display results
String output = "Your amount " + amount + " consists of "
+ numOfOneDollars + " dollars "
+ numOfQuarters + " quarters "
+ numOfDimes + " dimes "
+ numOfNickels + " nickel "
+ numOfPennies + " pennies";
JOptionPane.showMessageDialog(null, output,
"Example 2.4 Output", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
Explanation / Answer
please rate - thanks I'm getting the correct answer, however if you are coming out 1penny short, it's because not every floating point number can berepresented in the computer, and .67 is one of those. If you add .005 it will solve the problem and work for everynumber import javax.swing.JOptionPane; public class untitled{ /** Main method */ public static void main(String[] args) { double amount; // Amount entered from the keyboard // Receive the amount entered from the keyboard String amountString = JOptionPane.showInputDialog(null, "Enter an amount in double, for example 11.56", "Example 2.4 Input", JOptionPane.QUESTION_MESSAGE); // Convert string to double amount = Double.parseDouble(amountString); if(amount==0) { JOptionPane.showMessageDialog(null, "Your amount is zero", "Example 2.4 Output", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(amount0) if(numOfOneDollars==1) output=output+numOfOneDollars + " dollar "; else output=output+ numOfOneDollars+ " dollars "; if( numOfQuarters>0) if( numOfQuarters==1) output=output+ numOfQuarters + " quarter "; else output=output+ numOfQuarters + " quarters "; if( numOfDimes>0) if( numOfDimes==1) output=output+ numOfDimes + " dime "; else output=output+ numOfDimes + " dimes "; if( numOfNickels>0) if( numOfNickels==1) output=output+ numOfNickels+ " nickel "; else output=output+ numOfNickels + " nickels "; if( numOfPennies>0) if( numOfPennies==1) output=output+ numOfPennies+ " penny "; else output=output+ numOfPennies + " pennies "; JOptionPane.showMessageDialog(null, output, "Example 2.4 Output", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.