Java Reference
In-Depth Information
Notice that in the code that follows the above (which is executed when the Display button is pressed)
an Employee object is created and passed information from the frame. This is no longer needed as
setEmployeeProperties now performs this function.
9.
Delete the statement that creates the Employee object.
The code should look like the following:
if (dispBtn == e.getSource()) {
EmployeeFrame ef = new EmployeeFrame(emp);
In the next set of code (executed when the Gross button is pressed), another method variable and Employee
object was created. Because we created a global Employee variable and object, we don't need this statement anymore.
10.
Delete the statement that creates the Employee object when the Gross button is pressed.
The code should look like the following:
if (grossBtn == e.getSource()) {
resultLbl.setText("Gross Salary is: " +
emp.grossSalaryCalcFormatted(doubleEmpPR));
The statement setting the result label should be changed because it tries to calculate the gross salary with
the doubleEmpPR variable. This local variable's value is no longer set because the Employee object's pay rate
property is used to hold the value (set in setEmployeeProperties). To calculate the gross salary, simply execute the
grossSalaryCalcFormatted method.
11.
Change the statement to the following:
resultLbl.setText("Gross Salary is: " +
emp.grossSalaryCalcFormatted());
Finally, the code that executes when the tax amount button is pressed tries to read the text and parse it. This is
redundant because setEmployeeProperties is performing this function.
12.
Delete the first four statements and change the fifth statement to the following:
resultLbl.setText("The Federal Tax amount is: " +
emp.fedTaxCalcFormatted());
13.
Save EnterEmpInfo and run as an application.
14.
Enter 10.3338 for the payRate and 4 for the number of exemptions.
15.
Click the Gross button.
The result will be the message: “Gross Salary is: $413.35.” Notice that the dollar value is formatted as currency
and rounded to two digits. At this point, a good programmer would run tests to ensure that the tax amount calculation
works and that the numeric value validity check is working for pay rate.
16.
Run tests to ensure that the tax amount is correct and only numeric values can be entered
for pay rate and then click the Exit button.
You should have discovered another soft error: the error message does not appear in red. We always
want to reset the color to black when we start actionPerformed. Remember that the error color is set to red in
setEmployeeProperties, and we called setEmployeeProperties before the color is reset to black.
Search WWH ::




Custom Search