Java Reference
In-Depth Information
3.
Change the setPayRate method body to the following:
if (d < 6.5 || d > 35) {
throw new InvalidValue("The value for a pay rate " +
"must be at least $6.50 but no more than $35." +
" Please reenter the pay rate.");
} else {
this .payRate = d;
}
This statement checks if the value is outside the valid range. If so, an InvalidValue exception is created with a
descriptive message and then thrown. If not, the employee pay rate is set to the parameter value that was passed.
4.
Save the Employee source code.
The EnterEmpInfo source code should have errors in the setEmployeeProperties method because InvalidValue is
unhandled. We need to change setEmployeeProperties to catch the InvalidValue exception.
5.
In the EnterEmpInfo source code, add the following import statement.
import org.omg.CORBA.DynAnyPackage.InvalidValue;
The exception class must be imported even if the program is only catching the exception.
6.
In the setEmployeeProperties method, add the following catch after the already existing
catch and save the source code:
catch (InvalidValue e) {
empPRTF.setText("");
resultLbl.setForeground(Color.red);
resultLbl.setText(e.getMessage());
}
This solves one of the errors. However, the statement that sets empPayRate to -1 is still an error because -1 is
an invalid value for pay rate. We were using the -1 value as an error flag within the actionPerformed method. This is
really a misuse of the empPayRate variable. Instead, we will create a boolean variable called inputError and, if the pay
rate value is invalid, set inputError to true . We'll change actionPerformed to check inputError, not empPayRate. In
addition, we need to reset inputError to false each time actionPerformed is executed.
In the EnterEmpInfo, add the following statement to create a class boolean variable called
inputError.
7.
private boolean inputError = false ;
In the setEmployeeProperties method, replace emp.setPayRate(-1); with the following
statement:
8.
inputError = true ;
9.
In the InvalidValue catch block, add the following statement:
inputError = true ;
Search WWH ::




Custom Search