Java Reference
In-Depth Information
63
64
// return gross sales amount
65
public
double
getGrossSales()
66
{
67
return
grossSales;
68
}
69
70
// set commission rate
71
public
void
setCommissionRate(
double
commissionRate)
72
{
73
if
(commissionRate <=
0.0
|| commissionRate >=
1.0
)
74
throw new
IllegalArgumentException(
75
"Commission rate must be > 0.0 and < 1.0")
;
76
77
this
.commissionRate = commissionRate;
78
}
79
80
// return commission rate
81
public
double
getCommissionRate()
82
{
83
return
commissionRate;
84
}
85
86
// calculate earnings
87
public
double
earnings()
88
{
89
return
getCommissionRate()
*
getGrossSales()
;
90
}
91
92
// return String representation of CommissionEmployee object
93
@Override
94
public
String toString()
95
{
96
return
String.format(
"%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f"
,
97
"commission employee"
,
getFirstName() getLastName()
getSocialSecurityNumber()
getGrossSales()
getCommissionRate()
,
,
98
"social security number"
,
,
99
"gross sales"
,
,
100
"commission rate"
,
);
101
}
102
}
// end class CommissionEmployee
Fig. 9.10
| CommissionEmployee
class uses methods to manipulate its
private
instance
variables. (Part 3 of 3.)
Class
BasePlusCommissionEmployee
Subclass
BasePlusCommissionEmployee
(Fig. 9.11) inherits
CommissionEmployee
's non-
private
methods and can access (in a controlled way) the
private
superclass members via
those methods. Class
BasePlusCommissionEmployee
has several changes that distinguish
it from Fig. 9.9. Methods
earnings
(lines 43-47) and
toString
(lines 50-55) each invoke
method
getBaseSalary
to obtain the base salary value, rather than accessing
baseSalary
directly. If we decide to rename instance variable
baseSalary
, only the bodies of method
setBaseSalary
and
getBaseSalary
will need to change.