Answered step by step
Verified Expert Solution
Question
1 Approved Answer
package gui; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing. * ; import javax.swing.border.Border; / / Defines class CalculateChange public class CalculateChange { / /
package gui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.;
import javax.swing.border.Border;
Defines class CalculateChange
public class CalculateChange
Declares an object of class JFrame
JFrame jf;
Declares objects of class JPanel
JPanel jp jp mainP;
Declares an object of class JLabel
JLabel balanceL;
Declares an object of class JTextField
JTextField balanceT;
Declares an object of class JTextArea
JTextArea textAreaTA;
Declares an object of class JButton
JButton calculateB;
Declares an object of class JBorder
Border titleBorder;
Default constructor definition
CalculateChange
Creates a frame
jf new JFrameCalculate Change from Balance";
Creates a panels
jp new JPanel;
jp new JPanel;
mainP new JPanel;
Creates a label
balanceL new JLabelBalance to Calculate: ;
Creates a text field
balanceT new JTextField;
Creates a text area with rows and columns
textAreaTA new JTextArea;
Creates a button
calculateB new JButtonCalculate;
Creates a title border
titleBorder BorderFactory.createTitledBorderChange Amounts";
Adds label, text field and buttons to panel
jpaddbalanceL;
jpaddbalanceT;
jpaddcalculateB;
Adds text area to panel
jpaddtextAreaTA;
Sets the title border to panel
jpsetBordertitleBorder;
Sets the main panel layout to BorderLayout with rows and column
mainP.setLayoutnew BorderLayout;
Adds panel to main panel at north side of main panel
mainP.addjp BorderLayout.NORTH;
Adds panel to main panel at center of main panel
mainP.addjp BorderLayout.CENTER;
Adds main panel to frame
jfaddmainP;
Sets the frame other properties
jfsetVisibletrue;
jfsetSize;
jfsetLocationRelativeTonull;
Registers action listener to calculate Button using anonymous class
calculateB.addActionListenernew ActionListener
Overrides the actionPerformed method
public void actionPerformedActionEvent ae
Checks if balance text field is empty then display error message
ifbalanceTgetTextlength
JOptionPane.showMessageDialogjf "ERROR: Please enter amount.";
Otherwise not empty
else
try block begins
try
Converts the amount entered in text field to double
double amount Double.parseDoublebalanceTgetText;
Checks if amount is negative then display error message
ifamount
JOptionPane.showMessageDialogjf
"ERROR: Amount cannot be negative.";
Otherwise positive
else
Calls the method to get the changes
int result calculateamount;
Concatenates the changes to string
String resultText "The number of dollar bills are:
result;
resultText
The number of dollar bills are: result;
resultText
The number of dollar bills are: result;
resultText
The number of quarters are: result;
resultText
The number of dimes are: result;
Sets the string result to text area
textAreaTA.setTextresultText;
Catch block to handle NumberFormatException
catchNumberFormatException ne
JOptionPane.showMessageDialogjf "ERROR: Invalid amount.";
End of method
; End of anonymous class
Method to calculate change from the parameter amount
@param amount Amount whose change need to calculate
@return The array contains change for dollars, quarters and dimes
int calculatedouble amount
Creates array with size
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started