Java Reference
In-Depth Information
To Enter Code for the Numeric and Decimal Point Button Clicks
1. Enter lines 176 through 196 as shown in Figure 6-28 on page 406.
The TextPad window displays the code to test for button clicks of the
numeric buttons or the decimal point button (Figure 6-29). When executed,
the array will be searched, and the switch structure will look for numeric
buttons and the decimal point. When found, the labels from the buttons will
display in the lcd TextField, concatenated with previously displayed labels as
needed.
for loop to
search array until
key is found
flag set to false
if statement to find which
button was clicked
switch header
multiple case
statements
if flag is
set to true
sets button label to
display in lcd TextField
FIGURE 6-29
concatenates multiple
numbers if needed
The boolean flag, foundKey, allows the program to exit the loop if a match is
found in line 184, thereby saving time and making the program more efficient.
Searching for the First Operator Button Click
Figure 6-30 displays the code to search the array and add functionality to
the operator buttons within the actionPerformed() method. Line 198 displays
the case statements that correspond to the operator buttons in the keys. If any of
the cases occur, meaning that the user clicked one of the buttons between 10
through 14, execution of the code passes to the first non-case statement, which is
seen in line 199.
197
// operator buttons
198
case 10: case 11: case 12: case 13: case 14:
199
clearText = true ;
200
201
if ( first ) // first operand
202
{
203
if ( lcd.getText () .length () ==0 ) op1 = 0.0;
204
else op1 = Double .parseDouble ( lcd.getText ()) ;
205
206
first = false ;
207
clearText = true ;
208
lastOp = i; // save last operator
209
}
FIGURE 6-30
 
Search WWH ::




Custom Search