Programming Practice The files AdditionButton.java( http://websites.delta.edu/te
ID: 3701380 • 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
// Delta College - CST 183 - Klingler // This application demonstrates a basic Java event-driven application with // a user interface that allows three number 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"); // Initialize text field objects operand1 = new JTextField( 15 ); operand2 = new JTextField( 15 ); operand3 = new JTextField( 15 ); // Add commponents to container frame (left-to-right, top-to-bottom) frameContainer.add( operandLabel1 ); frameContainer.add( operand1 ); frameContainer.add( operandLabel2 ); frameContainer.add( operand2 ); 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 ) { // If user clicks button, grab text currently stored // in text field and display it in a dialog if ( event.getSource() == addButton ) { int number1 = 0; int number2 = 0; int number3 = 0; int sum; boolean errorDetected = false; // Assume no error String operand1Str = operand1.getText(); String operand2Str = operand2.getText(); String operand3Str = operand3.getText(); // Convert operands from text fields try{ number1 = Integer.parseInt(operand1Str); number2 = Integer.parseInt(operand2Str); number3 = Integer.parseInt(operand2Str); } // Catch any number format exception. Display error message // for invalid numbers. catch (NumberFormatException theException) { JOptionPane.showMessageDialog( null, "Invalid input format", "ALERT", JOptionPane.WARNING_MESSAGE ); errorDetected = true; // Mark an error detected } // Asserting valid integers, check to insure they are in range. if (number1 < 0 || number2 < 0 || number3 < 0) { JOptionPane.showMessageDialog( null, "Numbers must be positive", "ALERT", JOptionPane.WARNING_MESSAGE ); errorDetected = true; // Mark an error detected } // If no error detected, calculate and display the sum if ( ! errorDetected ) { sum = number1 + number2 + number3; JOptionPane.showMessageDialog( null, "Sum is: " + sum ); } } } // end method actionPerformed } // end ButtonHandler class // 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.