Java Reference
In-Depth Information
Figure 6-34 on the previous page displays the final code for the
actionPerformed() method. If the user clicks the equal button, the first flag
is set to true in preparation for the next calculation (line 230); otherwise, the
lastOp location (line 231) is saved.
Line 232 closes the else structure begun in line 211. Line 233 is the final
break for the case structure begun in line 198. Line 234 closes the switch
structure begun in line 185. Line 235 closes the if structure begun in line 182.
Line 236 ends the for structure begun in line 180, and line 237 closes the
actionPerformed() method entirely. The following step enters the code that
executes when a user clicks the equal button.
To Enter Code for the Equal Button Click
1. Enter lines 230 through 238 as shown in Figure 6-34.
The TextPad window displays the code to handle a click of the equal button,
represented by the subscript 14 in the array (Figure 6-35). Line comments
help you know to which structure each closing brace applies.
if statement to
determine if equal
operator button
was clicked
closing
braces
FIGURE 6-35
Matching Braces
One of the most common errors made by beginning Java
programmers is the mismatching of opening and closing braces
in control structures and class blocks. Some programmers insert
a short line comment for each ending brace specifying what
structure is closing. Your instructor may advise you to add other
line comments.
Coding the main() Method
for the Calculator Class
The main() method for the Calculator application constructs an instance of the
Calculator class and then sets three attributes, as shown in Figure 6-36.
 
Search WWH ::




Custom Search