366 Chapter 9 Object-Oriented Programming: Inheritance
format specifier %.2f to format the gross sales, commission rate and base salary with two
digits of precision to the right of the decimal point (line 121).
Testing Class BasePlusCommissionEmployee
Figure 9.7 tests class BasePlusCommissionEmployee. Lines 9–11 create a BasePlusCom-
missionEmployee object and pass "Bob", "Lewis", "333-33-3333", 5000, .04 and 300 to
the constructor as the first name, last name, social security number, gross sales, commis-
sion rate and base salary, respectively. Lines 16–27 use BasePlusCommissionEmployee’s
get methods to retrieve the values of the object’s instance variables for output. Line 29 in-
vokes the object’s setBaseSalary method to change the base salary. Method setBaseSal-
ary (Fig. 9.6, lines 95–102) ensures that instance variable baseSalary is not assigned a
negative value. Line 33 of Fig. 9.7 invokes method toString explicitly to get the object’s
String representation.
1 // Fig. 9.7: BasePlusCommissionEmployeeTest.java
2 // BasePlusCommissionEmployee test program.
3
4 public class BasePlusCommissionEmployeeTest
5 {
6 public static void main(String[] args)
7 {
8 // instantiate BasePlusCommissionEmployee object
9 BasePlusCommissionEmployee employee =
10 new BasePlusCommissionEmployee(
11 "Bob", "Lewis", "333-33-3333", 5000, .04, 300);
12
13 // get base-salaried commission employee data
14 System.out.println(
15 "Employee information obtained by get methods:%n");
16 System.out.printf("%s %s%n", "First name is",
17 employee.getFirstName());
18 System.out.printf("%s %s%n", "Last name is",
19 employee.getLastName());
20 System.out.printf("%s %s%n", "Social security number is",
21 employee.getSocialSecurityNumber());
22 System.out.printf("%s %.2f%n", "Gross sales is",
23 employee.getGrossSales());
24 System.out.printf("%s %.2f%n", "Commission rate is",
25 employee.getCommissionRate());
26 System.out.printf("%s %.2f%n", "Base salary is",
27 employee.getBaseSalary());
28
29 employee.setBaseSalary(1000);
30
31 System.out.printf("%n%s:%n%n%s%n",
32 "Updated employee information obtained by toString",
33 employee.toString());
34 } // end main
35 } // end class BasePlusCommissionEmployeeTest
Fig. 9.7 | BasePlusCommissionEmployee test program. (Part 1 of 2.)