(Arithmetic Calculator Application) Write an application that allows users to enter two numbers that can then be added or multiplied (Fig. 9.25). Users should enter each num- ber in an input dialog, displayed when the Enter Operands JButton is clicked. Each number should be appended to the Operands: JTextArea. The Add and Multiply JButtons are ini- tially disabled, but they should be enabled after the two operands are input. Once a result is calculated using the Add JButton, this JButton should be disabled until two new numbers are added. Once a result is calculated using the Multiply JButton, this JButton should be disabled until two new numbers are added.



a) Copying the template to your working directory. Copy the directory C:Examples Tutorial09ExercisesArithmeticCalculator to your C:SimplyJava directory.

b) Opening the template file. Open the ArithmeticCalculator.java file in your text editor. Lines 25–26 in the template already declare two instance variables of type double—value1 and value2—that you will need in this application. Variable value1 will store the first value entered by the user, and value2 will store the second value entered by the user. Each variable is initialized to 0.

c) Displaying an input dialog. The template contains a do…while statement in the enterOperandsJButtonActionPerformed event handler, beginning in line 145. Add code in lines 147–148 (within the do…while statement) to display an input dialog that asks the user to enter an operand. Use two lines to increase readability. Store the value the user enters in the input dialog in the variable input (which is declared in line 139).

d) Retrievin

```
1 // Exercise 9.13: ArithmeticCalculator.java
2 // This application accepts two operands and allows
3 // the user to perform multiplication or addition.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7
8 public class ArithmeticCalculator extends JFrame
9 {
10 // JLabel and JTextArea for operands entered by user
11 private JLabel operandsJLabel;
12 private JTextArea operandsJTextArea;
13
14 // JLabel and JTextField displays result of calculation
15 private JLabel resultJLabel;
16 private JTextField resultJTextField;
17
18 // JButton initiates asking user for operands
19 private JButton enterOperandsJButton;
20
21 // JButtons initiate adding and/or multiplying operands
22 private JButton addJButton;
23 private JButton multiplyJButton;
24
25 private double value1 = 0; // holds the first value entered
26 private double value2 = 0; // holds the second value entered
27
28 // no-argument constructor
29 public ArithmeticCalculator()
30 {
31 createUserInterface();
32 }
33
34 // create and position GUI components; register event handlers
35 private void createUserInterface()
36 {
37 // get content pane for attaching GUI components
38 Container contentPane = getContentPane();
39
40 // enable explicit positioning of GUI components
41 contentPane.setLayout( null );
42
43 // set up operandsJLabel
44 operandsJLabel = new JLabel();
45 operandsJLabel.setBounds( 16, 8, 100, 16 );
46 operandsJLabel.setText( "Operands:" );
47 contentPane.add( operandsJLabel );
48
49 // set up operandsJTextArea
50 operandsJTextArea = new JTextArea();
51 operandsJTextArea.setBounds( 16, 27, 104, 32 );
52 operandsJTextArea.setEditable( false );
53 contentPane.add( operandsJTextArea );
54
55 // set up resultJLabel
56 resultJLabel = new JLabel();
57 resultJLabel.setBounds( 16, 64, 53, 23 );
58 resultJLabel.setText( "Result:" );
59 contentPane.add( resultJLabel );
60
61 // set up resultJTextField
62 resultJTextField = new JTextField();
63 resultJTextField.setBounds( 16, 91, 104, 26 );
64 resultJTextField.setEditable( false );
65 resultJTextField.setHorizontalAlignment( JTextField.CENTER );
66 contentPane.add( resultJTextField );
67
68 // set up enterOperandsJButton
69 enterOperandsJButton = new JButton();
70 enterOperandsJButton.setBounds( 140, 27, 125, 26 );
71 enterOperandsJButton.setText( "Enter Operands" );
72 contentPane.add( enterOperandsJButton );
73 enterOperandsJButton.addActionListener(
74
75 new ActionListener() // anonymous inner class
76 {
77 // event handler called when enterOperandsJButton clicked
78 public void actionPerformed( ActionEvent event )
79 {
80 enterOperandsJButtonActionPerformed( event );
81 }
82
83 } // end anonymous inner class
84
85 ); // end call to addActionListener
86
87 // set up addJButton
88 addJButton = new JButton();
89 addJButton.setBounds( 140, 59, 125, 26 );
90 addJButton.setText( "Add" );
91 addJButton.setEnabled( false );
92 contentPane.add( addJButton );
93 addJButton.addActionListener(
94
95 new ActionListener() // anonymous inner class
96 {
97 // event handler called when addJButton is pressed
98 public void actionPerformed( ActionEvent event )
99 {
100 addJButtonActionPerformed( event );
101 }
102
103 } // end anonymous inner class
104
105 ); // end call to addActionListener
106
107 // set up multiplyJButton
108 multiplyJButton = new JButton();
109 multiplyJButton.setBounds( 140, 91, 125, 26 );
110 multiplyJButton.setText( "Multiply" );
111 multiplyJButton.setEnabled( false );
112 contentPane.add( multiplyJButton );
113 multiplyJButton.addActionListener(
114
115 new ActionListener() // anonymous inner class
116 {
117 // event handler called when multiplyJButton is pressed
118 public void actionPerformed( ActionEvent event )
119 {
120 multiplyJButtonActionPerformed( event );
121 }
122
123 } // end anonymous inner class
124
125 ); // end call to addActionListener
126
127 // set properties of application’s window
128 setTitle( "Arithmetic Calculator" ); // set title bar text
129 setSize( 290, 159 ); // set window size
130 setVisible( true ); // display window
131
132 } // end method createUserInterface
133
134 // store and display input
135 private void enterOperandsJButtonActionPerformed(
136 ActionEvent event )
137 {
138 int counter = 1;
139 String input;
140
141 // remove previous data
142 operandsJTextArea.setText( "" );
143 resultJTextField.setText( "" );
144
145 do // loop twice, once for each operand
146 {
147 input = JOptionPane.showInputDialog(
148 null, "Enter Operand" );
149

150 // convert input
151 if ( counter == 1 )
152 {
153 value1 = Double.parseDouble( input );
154 }
155 else
156 {
157
158 }
159


value2 = Double.parseDouble( input );
160
161 }

counter++; // increment counter
162 while ( counter <= 2 );
163
164 // display operands
165 operandsJTextArea.setText( value1 + "\n" + value2 );
166
167 // allow user to click calculate JButtons
168 addJButton.setEnabled( true );
169 multiplyJButton.setEnabled( true );
170
171
172 // transfer focus to addJButton addJButton.requestFocusInWindow()
173 ;
174 } // end method enterOperandsJButtonActionPerformed
175
176 // add and display result
177 private void addJButtonActionPerformed( ActionEvent event )
178 {
179 double result = value1 + value2;
180 resultJTextField.setText( String.valueOf( result ) );
181
182
183 addJButton.setEnabled( false );
184
185 } // end method addJButtonActionPerformed
186
187 // multiply and display result
188 private void multiplyJButtonActionPerformed( ActionEvent event )
189 {
190 double result = value1 * value2;
191 resultJTextField.setText( String.valueOf( result ) );
192
193 // prevent user from multiplying until new values entered
194 multiplyJButton.setEnabled( false );
195
196 } // end method multiplyJButtonActionPerformed
197
198 // main method
199 public static void main( String[] args )
200 {
201 ArithmeticCalculator application = new ArithmeticCalculator();
202 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
203
204 } // end method main
205
206 } // end class ArithmeticCalculator
```

Computer Science & Information Technology

You might also like to view...

A(n) ________ is a database object that you create and use independently of other controls or objects

A) stand-alone macro B) procedure macro C) embedded macro D) command macro

Computer Science & Information Technology

Which of the following protocols would the network administrator use to protect login credentials when accessing a router terminal session?

A. SCP B. SNMPv3 C. SSL D. SSH

Computer Science & Information Technology