Java Reference
In-Depth Information
Line 199 sets the clearText field to true. Recall that the identifier clearText
was set to false in line 192 after the first operand was entered. Now that an oper-
ator is entered, setting clearText back to true will clear the display when the user
clicks the next numeric button.
The if statement in lines 201 through 209 executes only when the first
operand is clicked. If no values are in the lcd TextField, clicking an operator will
store 0.0 in the variable named op1 (line 203). Otherwise, if a number is stored
in the lcd TextField, that value is stored in op1 (line 204). The first flag will be set
to false (line 206), and the location index of the operator will be stored for later
use in the lastOp variable (line 208).
The following step enters the code that executes when a user clicks the first
operator button in any calculation.
To Enter Code for the First Operator Button Click
1. Enter lines 197 through 209 as shown in Figure 6-30.
The TextPad window displays the code to handle the first operator button
click in a calculation (Figure 6-31). When executed, the array will be
searched, and the switch structure will look for an operator button.
multiple case
statements for
operator buttons
first operand
flag tested
number displayed in
lcd TextField stored
in op1 variable
flags set
FIGURE 6-31
Period (.) delimiters link, or concatenate, method calls, such as the getText()
method and the length() method in line 203. The name of the TextField, lcd, is
followed by the getText() method and then by the length() method. Once Java
determines the text stored in the lcd TextField using the getText() method, it
then can count the characters to return the length.
 
Search WWH ::




Custom Search