Java Reference
In-Depth Information
After parsing (and the try / catch blocks), we only want to keep processing if a valid value for pay rate has been
entered. So, we need to check if the pay rate is still -1. If pay rate is still -1 (meaning invalid data was entered), we want
the JVM to redisplay the frame. In other words, we don't want the remaining statements in the method to be executed.
To stop the statements from being executed, we will surround the statements in the following if statement:
if (doubleEmpPR != -1) {
remaining method statements
}
6. Select all the remaining statements in the method (but not the last closing brace) and click
Source , Surround With , then 3 if ( if statement ).
RAD surrounds the statements with the if statement but doesn't know what condition should be coded. So, the
placeholder text “condition” is put in the parenthesis as follows:
if (condition) {
remaining method statements
}
7.
Replace the text “condition” with:
doubleEmpPR != -1
Now, when the pay rate value is invalid (i.e., doubleEmpPR's value was not changed from -1), this if prevents the
remaining statements from executing. We have coded a little, so it's time to test a little.
8.
Save and run the application.
9.
Try to calculate tax amounts for the following pay rate and exemptions values and notice
all the errors.
Pay Rate
Exemptions
Ten
4
10
four
10
4
10.33
4
You should have noted the following “problems”:
A.
The error message is not always displayed
B.
The tax amount is not always formatted correctly. For example:
a.
it is displayed in red
b.
it has too many decimal places
C.
The message does not always fit in the label
D.
Entering an invalid exemptions value results in an exception being thrown that is not
being caught. In addition, the previous message remains on the frame thereby indicating
there is no problem or incorrectly identifying the problem.
Search WWH ::




Custom Search