Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project Outcomes: Develop a Java program that uses: Inheritance constructs overr

ID: 672615 • Letter: P

Question

Project Outcomes:

Develop a Java program that uses:

Inheritance constructs

overriding superclass methods

invoking super class methods

accessing superclass instance variables

abstact classes and methods

Polymorphism

Container operations, access all elements

Prep Readings:

Absolute Java, Chapters 1 - 9.

Project Requirements:

1. We are going to develop a program that implements inheritance to process employees in an organization. The program will consist of the following classes: Employee, HourlyEmployee, SalariedEmployee, CommissionedEmployee and Company and CompanyTester.

2. The Employee Class

a. This is an Abstract class designed to represent an Employee in a company.

b. Instance Variables.

   i.) Employees Name

   ii.) Employee id number

c. Static Variable - int nextIDNum - starts at 1000 and is used to generate the next Employee's IDNum

d. Methods

i.) Constructor - sets all instance fields from parameters.

ii.) Constructor - default constructor, sets all instance field to a default value.

iii.) Access and mutator methods for all variables. NOTE Mutator method for IDNum should use the static variable nextIDNum.

iv.) computePay - Abstract method that computes employee pay.

v.) toString method - returns a neatly formatted string containing the s name and id number

3. The HourlyEmployee Class

a. Subclass of Employee Class

b. Instance Variable

i.) employees hourly rate of pay

ii.) hours worked during current week

c. Methods

i.) Constructor - sets all instance fields from parameters.

ii.) Default Constructor - sets all instance fields to a default value.

iii.) Access and mutator methods for instance variable.

iv.) Overwritten computePay method
               1. pay calculation
                     1. <= 40 hours - rate * hours
                     2. > 40 hours - rate * 40 + (hours above 40 * rate * 1.5).

v.) toSting - toString method - returns a neatly formatted string containing the employees name, id number (where have you see this before) and hourly rate of pay.

4. The SalariedEmployee Class

a. Subclass of Employee Class

b. Instance Variable - weekly salary

c. Methods

i.) Constructor - sets all instance fields from parameters.

ii.) Default Constructor - sets all instance fields to a default value.

iii.) Accessor and mutator methods for instance field.

iv.) Overwritten computePay method - Returns weekly salary

v.) toString method - returns a neatly formatted string containing the employees name, id number and weekly salary

5. The CommissionedEmployee Class

a. Subclass of SalariedEmployee

b. Instance Variable

i.) weeklysales

ii.) commission percentage

c. Methods

i.) Constructor - sets all instance fields from parameters.

ii.) Default Constructor - sets all instance fields to a default value.

iii.) Accessor and mutator methods for instance fields.

iv.) Overwritten computePay method  

[1] pay calculation

(a) add commission to weekly salary

(b) commission = weeklysales * commission percentage.

v.) toString method - returns a neatly formatted string containing the employees name, id number, weekly salary and commission

6. The Company Class

a. Holds and manages all the employee objects.

b. Instance fields

i.) Name of company

ii.) A container (ArrayList or Array) to hold the abstract classes employee objects. ArrayList are not covered until Chapter 14 but they are fairly simple to use if you’d like to use them in this project.

c. Methods

i.) Constructor - sets the instance field from parameter and creates the container.

ii. )Default Constructor - sets the instance field to a default value and creates the container

iii.) Add an employee to the container

iv.) Get an employee by employee Id

v.) Get the number of employees in the company

vi.) Get the total payroll for the company assuming all employees work a 40 hour work week.

vii.) toString method that returns a neatly formatted string the company name and each employees information (employees name, id number and weekly pay (assume 40 hour work week).

7. CompanyTester Class

a. Creates a Company Object

b. Adds several employees of each type to the Company class.

c. Tests all the methods in all the classes, either directly or indirectly.

8. Create 2 UML Class Diagram for this project.

a. Design Version - completed prior to coding the project to assist with coding.

b. Final Version - completed after the coding that accurately represents the final version of the code.

i.) All instance variables, including type.

ii.) All methods, including parameter list, return type and access specifier (+, -);

c. No need to include the CompanyTester in the UML diagrams. Refer to the UML Distilled pdf on the content page as a referene for creating class diagrams.

9. Note: Some design details such as instance variable types and method names and parameters have not been specified, this is intentional, use your experience and best judgment to fill in the details, just make sure you meet the requirements.

Explanation / Answer

         2       // Assigning superclass and subclass references to superclass and

         3       // subclass variables.

         4      

         5       public class PolymorphismTest

         6       {

         7          public static void main( String args[] )

         8          {

         9             // assign superclass reference to superclass variable

         10             CommissionEmployee3 commissionEmployee = new CommissionEmployee3(

         11                "Sue", "Jones", "222-22-2222", 10000, .06 );

         12      

         13             // assign subclass reference to subclass variable

         14             BasePlusCommissionEmployee4 basePlusCommissionEmployee =

         15                new BasePlusCommissionEmployee4(

         16                "Bob", "Lewis", "333-33-3333", 5000, .04, 300 );

         17      

         18             // invoke toString on superclass object using superclass variable

         19             System.out.printf( "%s %s: %s ",

         20             "Call CommissionEmployee3's toString with superclass reference ",

         21             "to superclass object", commissionEmployee.toString() );

         22      

         23             // invoke toString on subclass object using subclass variable  

         24             System.out.printf( "%s %s: %s ",

         25                "Call BasePlusCommissionEmployee4's toString with subclass",

         26                "reference to subclass object",

         27                basePlusCommissionEmployee.toString() );

         28      

         29             // invoke toString on subclass object using superclass variable

         30             CommissionEmployee3 commissionEmployee2 =

         31                basePlusCommissionEmployee;

         32             System.out.printf( "%s %s: %s ",

         33                "Call BasePlusCommissionEmployee4's toString with superclass",

         34                "reference to subclass object",

                           commissionEmployee2.toString() );

         35          } // end main

            36        } // end class PolymorphismTest

Figure 10.4: Employee.java

2   // Employee abstract superclass.

3

4   public abstract class Employee

5   {

6      private String firstName;

7      private String lastName;

8      private String socialSecurityNumber;

9

10     // three-argument constructor

11     public Employee( String first, String last, String ssn )

12     {

13        firstName = first;

14        lastName = last;

15        socialSecurityNumber = ssn;

16     } // end three-argument Employee constructor

17

18     // set first name

19     public void setFirstName( String first )

20     {

21        firstName = first;

22     } // end method setFirstName

23

24     // return first name

25     public String getFirstName()

26     {

27        return firstName;

28     } // end method getFirstName

29

30     // set last name

31     public void setLastName( String last )

32     {

33        lastName = last;

34     } // end method setLastName

35

36     // return last name

37     public String getLastName()

38     {

39        return lastName;

40     } // end method getLastName

41

42    // set social security number

43     public void setSocialSecurityNumber( String ssn )

44     {

45        socialSecurityNumber = ssn; // should validate

46     } // end method setSocialSecurityNumber

47

48     // return social security number

49     public String getSocialSecurityNumber()

50     {

51        return socialSecurityNumber;

52     } // end method getSocialSecurityNumber

53

54     // return String representation of Employee object

55     public String toString()

56     {

57        return String.format( "%s %s social security number: %s",

58           getFirstName(), getLastName(),

                       getSocialSecurityNumber() );

59     } // end method toString

60

61     // abstract method overridden by subclasses

62     public abstract double earnings(); // no implementation here

63 } // end abstract class Employee