The program below is the solution to Liang’s Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.26.
Question: Rewrite Listing 2.10, ComputeChange.java, using input and output dialog boxes.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 2.26 - Use input dialog<br /> *<br /> */ <br /><br />import javax.swing.JOptionPane;<br /><br />public class Ex02_26 {<br /><br /> public static void main(String[] args) {<br /> <br /> String sAmount = JOptionPane.showInputDialog(null, "Enter an amount, for"<br /> + " example, 1156 for $11.56: ","Input", JOptionPane.QUESTION_MESSAGE);<br /> int amount = Integer.parseInt(sAmount);<br /> int remainingAmount = amount;<br /> int numberOfOneDollars = remainingAmount / 100;<br /> remainingAmount = remainingAmount % 100;<br /> // Find the number of quarters in the remaining amount<br /> int numberOfQuarters = remainingAmount / 25;<br /> remainingAmount = remainingAmount % 25;<br /> // Find the number of dimes in the remaining amount<br /> int numberOfDimes = remainingAmount / 10;<br /> remainingAmount = remainingAmount % 10;<br /> // Find the number of nickels in the remaining amount<br /> int numberOfNickels = remainingAmount / 5;<br /> remainingAmount = remainingAmount % 5;<br /> // Find the number of pennies in the remaining amount<br /> int numberOfPennies = remainingAmount;<br /> // Display results<br /> String output = "Your amount " + amount + " consists of: n" <br /> + numberOfOneDollars + " dollarsn"<br /> + numberOfQuarters + " quartersn"<br /> + numberOfDimes + " dimesn"<br /> + numberOfNickels + " nickelsn"<br /> + numberOfPennies + " pennies";<br /> JOptionPane.showMessageDialog(null, output, "Output", 1);<br /> }<br />}<br /> |
Program output |
Click here to see other solutions to Introduction to Java Programming.