In Chapter 9 of the textbook Java How To Program Deitel Ninth Ed., the class Bas
ID: 3569175 • Letter: I
Question
In Chapter 9 of the textbook Java How To Program Deitel Ninth Ed., the class BasePlusCommissionEmployee was incrementally developed as a subclass of CommissionEmployee. This design, however, can be made more general so that it could more easily incorporate modified or new classes that model different types of employees as part of a class hierarchy.
Not all types of employees are CommissionEmployees, so it would be useful to create a more general Employee superclass that factors out the attributes and behaviors common to all employees. Attributes and methods that are common to all employees include:
firstName,
lastName,
socialSecurityNumber,
getFirstName()/setFirstName(),
getLastName()/setLastName(),
getSocialSecurityNumber()/setSocialSecurityName(), and
A portion of toString() that includes the preceeding accessor methods.
To accomplish this refactoring of the class hierarchy the represents employees, first create a new Employee class that included that above fields and methods, and also include a constructor that initializes the instance variables with their associated mutator methods.
Part B
B1. Compile and run the supplied source test drivers CommissionEmployeeTest and BasePlusComissionEmployeeTest and validate that the output matches the output of suppled files CommisionEmployeeTest Ouput and BasePlusCommissionEmployeeTest Output.
B2. Rewrite the supplied CommissionEmployee class source so that it is a subclass of Employee. The revised CommissionEmployee class should only contain methods and instance variables that are not declared in the Employee superclass. The constructor for the CommissionEmployee class should invoke the Employee superclass constructor and its toString() method should use the toString() method of the Employee superclass.
B3. The behavior of the CommissionEmployee and BasePlusCommissionEmployee class should remain the same after the refactoring. To confirm that the behaviors have not changed, rerun the test drivers from step B1. The output should be identical to the output produced in step B1.
B4. Why did we do this refactoring? To address that issue, answer the following 4 questions. You can include your replies in a text file (Questions.txt) to be submitted to the dropbox.
Explanation / Answer
// Fig. 10.1: PolymorphismTest.java // Assigning superclass and subclass references to superclass and // subclass variables. public class PolymorphismTest { public static void main( String args[] ) { // assign superclass reference to superclass variable CommissionEmployee3 commissionEmployee = new CommissionEmployee3( "Sue", "Jones", "222-22-2222", 10000, .06 ); // assign subclass reference to subclass variable BasePlusCommissionEmployee4 basePlusCommissionEmployee = new BasePlusCommissionEmployee4( "Bob", "Lewis", "333-33-3333", 5000, .04, 300 ); // invoke toString on superclass object using superclass variable System.out.printf( "%s %s: %s ", "Call CommissionEmployee3's toString with superclass reference ", "to superclass object", commissionEmployee.toString() ); // invoke toString on subclass object using subclass variable System.out.printf( "%s %s: %s ", "Call BasePlusCommissionEmployee4's toString with subclass", "reference to subclass object", basePlusCommissionEmployee.toString() ); // invoke toString on subclass object using superclass variable CommissionEmployee3 commissionEmployee2 = basePlusCommissionEmployee; System.out.printf( "%s %s: %s ", "Call BasePlusCommissionEmployee4's toString with superclass", "reference to subclass object", commissionEmployee2.toString() ); } // end main } // end class PolymorphismTest