Java Reference
In-Depth Information
So, we will move the gross salary calculation from EnterEmpInfo into Employee where it belongs. In addition, we
will create new methods to return the information as formatted strings.
1.
In Employee, add the following import statement:
import
java.text.NumberFormat;
2.
In Employee at the end of the class variable definitions, add the following statement to
create a currency formatter object called cf:
NumberFormat cf = NumberFormat.getCurrencyInstance();
3.
In Employee, after the fedTaxCalc method, create a new method called
fedTaxCalcFormatted
as follows:
public
String fedTaxCalcFormatted(
double
payRate,
int
exemptions) {
String taxAmt =
new
String();
taxAmt = cf.format(
this
.fedTaxCalc(payRate, exemptions));
return
taxAmt;
}
Notice that this method doesn't duplicate the fedTaxCalc function. Rather, it uses the fedTaxCalc method to
perform the tax calculation and then simply formats the result. We need to change EnterEmpInfo to call this new
method and use the returned string.
4.
In EnterEmpInfo, at the beginning of the actionPerformed method, enter the following
statement to create a string called stringTaxAmt that will hold the formatted tax amount:
String stringTaxAmt =
new
String();
5.
In EnterEmpInfo's actionPerformed method, change the following statement:
double
doubleTaxAmt =
emp.fedTaxCalc(doubleEmpPR, intExmp);
to:
stringTaxAmt =
emp.fedTaxCalcFormatted(doubleEmpPR, intExmp);
6.
In EnterEmpInfo's actionPerformed method, change the following statement:
resultLbl.setText("The Federal Tax amount is: $" +
doubleTaxAmt + "0");
to:
resultLbl.setText("The Federal Tax amount is: " +
stringTaxAmt);