Programming Practice The files AdditionButton.java( http://websites.delta.edu/te
ID: 3701717 • Letter: P
Question
Programming Practice
The files AdditionButton.java(http://websites.delta.edu/teklingl/cs1/resources/w12/examples/AdditionButton.java.txt) and AdditionException.java(http://websites.delta.edu/teklingl/cs1/resources/w12/examples/AdditionException.java.txt) from the provided in-class examples.
Enhance the AdditionButton class in the following ways:
Integrate exception-handling into the actionPerformed method. Verify that the user’s input does not trigger NumberFormatException. Provide an error message if it does and be sure no further processing takes place until they enter correct input.
Add a third text field to expand the application to offer the ability to add three numbers instead of the original two.
Add traditional error checking as well so that the addition is only performed if all three numbers entered are non-negative.
Explanation / Answer
/* This application demonstrates a basic Java event-driven application with
a user interface that allows three numbers to be entered. When the button
is pressed, the numbers are added and the sum displayed via an output dialog box */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AdditionButton extends JFrame
{
// Class wide component declarations
private JTextField operand1;
private JTextField operand2;
private JTextField operand3;
private JLabel operandLabel1;
private JLabel operandLabel2;
private JLabel operandLabel3;
private JButton addButton;
// set up GUI
public AdditionButton()
{
// Get access to JFrame container - required to add components
// Set layout to flow layout; allow components to be displayed
// left-to-right and top-to-bottom
Container frameContainer = getContentPane();
frameContainer.setLayout( new FlowLayout() );
// Initialize label objects
operandLabel1 = new JLabel("First Number");
operandLabel2 = new JLabel("Second Number");
operandLabel3 = new JLabel("Third Number"); //Added label objects for third number
// Initialize text field objects
operand1 = new JTextField( 15 );
operand2 = new JTextField( 15 );
operand3 = new JTextField( 15 ); //Initialize text field object for third number
// Add commponents to container frame (left-to-right, top-to-bottom)
frameContainer.add( operandLabel1 );
frameContainer.add( operand1 );
frameContainer.add( operandLabel2 );
frameContainer.add( operand2 );
//Added commponentsto container frame for third number
frameContainer.add( operandLabel3 );
frameContainer.add( operand3 );
// Initialize button and its label
addButton = new JButton("Add");
frameContainer.add( addButton );
// Register event handler for button
ButtonHandler handler = new ButtonHandler();
addButton.addActionListener( handler );
// Set application window attributes
setTitle( "Adding Machine" );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize( 350, 200 );
setVisible( true );
} // end constructor ButtonTest
// Private inner class for event handling
private class ButtonHandler implements ActionListener
{
// Process button click
public void actionPerformed( ActionEvent event )
{
boolean errorDetected = false; //Added a flag to detect any error in number convertion
// If user clicks button, grab text currently stored
// in text field and display it in a dialog
if ( event.getSource() == addButton )
{
//Extrating the string values from JtextFields
String operand1Str = operand1.getText();
String operand2Str = operand2.getText();
String operand3Str = operand3.getText();
// Convert and add operands from text fields
try{
int sum=0;
int num1 = Integer.parseInt(operand1Str);
int num2 = Integer.parseInt(operand2Str);
int num3 = Integer.parseInt(operand3Str);
if(num1>0&&num2>0&&num3>0){
sum = num1+num2+num3;
}else{
JOptionPane.showMessageDialog( null,"Please enter only non negative numbers" );
return;
}
// Display via dialog vbox
JOptionPane.showMessageDialog( null, "Sum is: " + sum );
}
catch(NumberFormatException ex){
JOptionPane.showMessageDialog(
null, "Invalid input format", "ALERT",
JOptionPane.WARNING_MESSAGE );
errorDetected = true;
}
}
} // end method actionPerformed
} // end ButtonHandler class
}
public class Main{
// Main method - to launch application
public static void main( String args[] )
{
AdditionButton application = new AdditionButton();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.