Java Reference
In-Depth Information
The following step enters code for the getCode() method.
To Enter Applet Code for the getCode() Method
1. Enter the code in lines 83 through 93 as shown in Figure 4-54.
TextPad displays the getCode() method in the coding window (Figure 4-55).
getCode()
method
FIGURE 4-55
In the application, the code uses a switch statement to evaluate the value
entered by the user and then transfers control to the appropriate case statement.
The switch statement, however, can accept only an integer value as an argument,
which means it cannot accept the boolean value of true or false required to test
the state of a Checkbox. Because of this, the applet uses nested if statements to
determine which Checkbox the user selected. You will learn other ways to check
the state of components in a later chapter.
Coding the getComm() Method for the Applet
The getComm() method used in the applet is identical to the getComm()
method used in the application. When the getComm() method is called from
the init() method, it sends the arguments, dollars and empCode, along to the
getComm() method itself. As shown in line 95 in Figure 4-56 on the next page,
the arguments, dollars and empCode, become the parameters, sales and code, in
the getComm() method. As with the getComm() method in the application, the
getComm() method in the applet receives the value of the argument, empCode,
and stores it as the parameter, code, which is local to the getComm() method.
The switch statement evaluates the value of code, and if the value matches the
value used in the case statement in line 100, the program assigns the commission
variable a commission rate of .10 multiplied by the value for sales. If the user
enters a 2, the program assigns a commission rate of .14 multiplied by the value
for sales. If a user enters a 3, the program assigns a commission rate of .18
multiplied by the value for sales.
Search WWH ::




Custom Search