Java Reference
In-Depth Information
Rate
. Lines 29-30 output the
String
representation of the updated
CommissionEmployee
.
When an object is output using the
%s
format specifier, the object's
toString
method is in-
voked
implicitly
to obtain the object's
String
representation. [
Note:
In this chapter, we do
not use the
earnings
method in each class, but it's used extensively in Chapter 10.]
1
// Fig. 9.5: CommissionEmployeeTest.java
2
// CommissionEmployee class test program.
3
4
public
class
CommissionEmployeeTest
5
{
6
public
static
void
main(String[] args)
7
{
8
// instantiate CommissionEmployee object
9
CommissionEmployee employee =
new
CommissionEmployee(
"Sue"
,
"Jones"
,
"222-22-2222"
,
10000
,
.06
);
10
11
12
// get commission employee data
13
System.out.println(
14
"Employee information obtained by get methods:"
);
15
System.out.printf(
"%n%s %s%n"
,
"First name is"
,
16
employee.getFirstName()
);
17
System.out.printf(
"%s %s%n"
,
"Last name is"
,
18
employee.getLastName()
);
19
System.out.printf(
"%s %s%n"
,
"Social security number is"
,
20
employee.getSocialSecurityNumber()
);
21
System.out.printf(
"%s %.2f%n"
,
"Gross sales is"
,
22
employee.getGrossSales()
);
23
System.out.printf(
"%s %.2f%n"
,
"Commission rate is"
,
24
employee.getCommissionRate()
);
25
26
employee.setGrossSales(
5000
);
employee.setCommissionRate(
.1
);
27
28
29
System.out.printf(
"%n%s:%n%n %n"
,
%s
30
"Updated employee information obtained by toString"
,
employee
);
31
}
// end main
32
}
// end class CommissionEmployeeTest
Employee information obtained by get methods:
First name is Sue
Last name is Jones
Social security number is 222-22-2222
Gross sales is 10000.00
Commission rate is 0.06
Updated employee information obtained by toString:
commission employee: Sue Jones
social security number: 222-22-2222
gross sales: 5000.00
commission rate: 0.10
Fig. 9.5
| CommissionEmployee
class test program.