Java Reference
In-Depth Information
Testing the getCode() Method
The following steps compile and test the getCode() method.
To Compile and Test the getCode() method
1. With the Data Disk in drive A, compile the program by clicking Compile
Java on the Tools menu. If TextPad notifies you of errors, click
Commission.java in the Selector window, fix the errors, and then
compile again. When the program compiles with no errors, click Run
Java Application on the Tools menu.
2. When the program displays the first Input dialog box, type 20000 as the
sales amount and then click the OK button. When the program displays
the second Input dialog box, type 5 and then click the OK button.
3. When the program displays the Error dialog box, click the OK button.
4. When the program again displays the Input dialog box, type 2 and then
click the OK button. Click the Close button in the command prompt
window title bar.
The program displays an error message when a number less than one or
greater than three is entered for the commission code. The program accepts
any number between one and three as valid input data.
Run the program several more times and test valid and invalid entries for
commission code, such as negative numbers, zero, or alphanumeric data.
The Case Structure
Sending a value to a method where it will be tested is a convenient way to make
the program easy to read and to test its components. In the case of a menu, for
example, there might be many possible, valid choices for the user to input. When
there are more than two possible, valid choices, the logical operators become
cumbersome and hard to understand, even when the logical AND and OR
operators are used.
Most programming languages, including Java, thus contain a variation of the
selection structure called a case structure. A case structure is a type of selection
structure that allows for more than two choices when the condition is evaluated.
For example, if a user selects from several choices on a menu, the code evaluates
the choice. If a match is found, then the appropriate action is performed. For
example, if the user selected Option 1 on the menu, the logic of the code might
execute one section of code; if the user selected Option 4 on the menu, an
entirely different section of code might execute. Alternatively, if no match is
found, the case structure can provide feedback to the user or store the no match
result for later use in the program. The case structure is described in detail in
Appendix A on page APP 8.
Search WWH ::




Custom Search