Answered step by step
Verified Expert Solution
Link Copied!

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 jp1, jp2, 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 JFrame("Calculate Change from Balance");
// Creates a panels
jp1= new JPanel();
jp2= new JPanel();
mainP = new JPanel();
// Creates a label
balanceL = new JLabel("Balance to Calculate: ");
// Creates a text field
balanceT = new JTextField(10);
// Creates a text area with 12 rows and 33 columns
textAreaTA = new JTextArea(12,33);
// Creates a button
calculateB = new JButton("Calculate");
// Creates a title border
titleBorder = BorderFactory.createTitledBorder("Change Amounts");
// Adds label, text field and buttons to panel 1
jp1.add(balanceL);
jp1.add(balanceT);
jp1.add(calculateB);
// Adds text area to panel 2
jp2.add(textAreaTA);
// Sets the title border to panel 2
jp2.setBorder(titleBorder);
// Sets the main panel layout to BorderLayout with 2 rows and 1 column
mainP.setLayout(new BorderLayout(2,1));
// Adds panel 1 to main panel at north side of main panel
mainP.add(jp1, BorderLayout.NORTH);
// Adds panel 2 to main panel at center of main panel
mainP.add(jp2, BorderLayout.CENTER);
// Adds main panel to frame
jf.add(mainP);
// Sets the frame other properties
jf.setVisible(true);
jf.setSize(400,300);
jf.setLocationRelativeTo(null);
// Registers action listener to calculate Button using anonymous class
calculateB.addActionListener(new ActionListener()
{
// Overrides the actionPerformed() method
public void actionPerformed(ActionEvent ae)
{
// Checks if balance text field is empty then display error message
if(balanceT.getText().length()==0)
JOptionPane.showMessageDialog(jf, "ERROR: Please enter amount.");
// Otherwise not empty
else
{
// try block begins
try
{
// Converts the amount entered in text field to double
double amount = Double.parseDouble(balanceT.getText());
// Checks if amount is negative then display error message
if(amount <=0.0)
JOptionPane.showMessageDialog(jf,
"ERROR: Amount cannot be negative.");
// Otherwise positive
else
{
// Calls the method to get the changes
int []result = calculate(amount);
// Concatenates the changes to string
String resultText = "The number of 100 dollar bills are: "
+ result[0];
resultText +="
The number of 10 dollar bills are: "+ result[1];
resultText +="
The number of 5 dollar bills are: "+ result[2];
resultText +="
The number of quarters are: "+ result[3];
resultText +="
The number of dimes are: "+ result[4];
// Sets the string result to text area
textAreaTA.setText(resultText);
}
}
// Catch block to handle NumberFormatException
catch(NumberFormatException ne)
{
JOptionPane.showMessageDialog(jf, "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 100,10,5 dollars, quarters and dimes
*/
int [] calculate(double amount)
{
// Creates array with size 5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions

Question

What resources will these tactics require?

Answered: 1 week ago

Question

What level of impact will this tactic make on the key public?

Answered: 1 week ago

Question

Have you used powerful language in your message?

Answered: 1 week ago