Topic

Object Type Casting.
Upcasting.
Downcasting.
instanceof
Polymorphism.
Polymorphism using inheritance.

Description

Assignment

Write a program that will compute bonuses earned this year by employees in an organization. There are three types of employees: workers, managers and executives. In addition to salary, all employees get a bonus as a percentage of their salary. This percentage is different for workers, managers and executives. Additionally, all managers get a fixed amount for travel expenses yearly and all executives get a fixed amount for travel expenses and a set number of stock options yearly.

Each type of employee is represented by a class. The classes are named Worker, Manager and Executive and are described below in the implementation section. Create three Worker, two Manager and 1 Executive objects. Put all the objects in a single array. Then using a single loop, call the method computeBonus provided in each object to compute bonuses.

Problem

Java is a strongly types language. In a Worker array, we can only put Worker objects and in a Manger array, we can put only Manager objects. So, we face the same problem that we encounter in the last assignment. How to put the objects of different type in one array. In the last assignment, we solve the problem by using an interface. In this assignment, we solve it by using inheritance.

Solution

We will create an abstract class Employee and make the classes Worker, Manager and Executive as the sub-classes of Employee. Since, we made classes Worker, Manager and Executive as sub-classes of Employee, their objects will additionally be of type Employee as they will inherit all the characteristics of class Employee. We will create an array of type Employee and store in it Worker, Manager, and Executive references. We will be able to do that because they are all of type Employee as well. Then, using a For loop, we will call the method computeBonus on each object and obtain its bonus.

Steps for Doing the Assignment

Below are the steps for doing the assignment (For implementation details see the Implementation section):

1. Create classes Worker, Manager and Executive

2. Create an abstract class Employee containing an abstract method computeBonus

3. Make the classes Worker, Manager and Executive as subclasses of the class Employee.

4. Create three Worker objects, two Manager objects and one Executive object

5. Create an array of type Employee of size 6.

6. Store in it references of Worker, Manager and Executive objects. (This is allowed because objects of type Worker, Manager and Executives are also of type Employee because Employee is their parent class and they inherit all its characteristics).

7. In a For loop, call the method computeBonus on different objects stored in array to compute their bonus as below.

for (int i=0; i<6; i++) {
double bonus=emp [i].computeBonus( ); //polymorphic call
}

Implementation

Create the following classes:

class Employee

Create an abstract class Employee that includes the following:

Two fields, String name and double salary, for holding employee name and salary.

A constructor for initializing name and salary.

Accessor methods, getName ( ) and getSalary ( ) for returning name and salary respectively.

An abstract method computeBonus ( ) for computing and returning the bonus such as shown below. (Do not provide a body for this method. The child classes will provide their individual implementation of this method.)

public abstract double computeBonus ( );

class Worker

Create a class Worker that extends Employee and provides the following:

A field pctBonus for specifying the salary percentage value for the bonus.

A constructor to initialize name, salary and pctBonus.

An accessor method getPctBonus for returning pctBonus value.

A method computeBonus for returning the bonus computed. (Provide a body for this method).

The method computeBonus will compute the bonus as follows:

bonus = salary * pctBonus

class Manager

Create a class Manager that extends Employee and provides the following:

A field pctBonus for specifying the salary percentage value for the bonus.

A field travelExpense for specifying travel expenses assigned.

A constructor to initialize name, salary, pctBonus, and travelExpense.

An accessor method getPctBonus for returning pctBonus value.

An accessor method getTravelExpense for returning travelExpense.

A method computeBonus for returning the bonus computed. (Provide a body for this method).

The method computeBonus will compute the bonus as follows:

bonus = (salary * pctBonus) + 500.00

class Executive

Create a class Executive that extends Employee and provides the following:

A field pctBonus for specifying the percentage value for the bonus.

A field travelExpense for specifying travel expenses available.

A field optionsCount for specifying the number of stock options awarded.

A constructor to initialize name, salary, pctBonus, travelExpense, and optionsCount.

An accessor method getPctBonus for returning pctBonus value.

An accessor method getTravelExpense for returning travelExpense.

An accessor method getOptionsCount for returning optionsCount.

A method computeBonus for returning the bonus computed. (Provide a body for this method).

The method computeBonus will compute the bonus as follows:

bonus = (salary * pctBonus) + 1000.00

class TestEmployee

Create a class TestEmployee containing the main method. The method main will do the following:

  • Prompt the user to enter the number of workers, say nw.
  • Prompt the user to enter the number of managers, say nm.
  • Prompt the user to enter the number of executives, say ne.
  • Compute the total number of employee (say n) as below: n = nw + nm + ne
  • Create an array of n Employee references. (The objects stored will be created in the subsequent steps).
  • Create nw Worker objects. Do this by setting up an nw count loop. In each pass through the loop, do the following:
  • Ask the user to enter data for one worker.
  • Create a Worker object and initialize it with data provided by the user.
  • Store the Worker object reference in the array of Employee references created earlier at the appropriate location in the Employee array.
  • Create nm Manager objects. Do this by setting up an nm count loop.
  • In each pass through the loop, do the following:
  • Ask the user to enter data for one manager.
  • Create a Manager object and initialize it with data provided by the user.
  • Store the Manager object reference in the array of Employee references created earlier.(Store the Manager references in the array elements following where Worker references were stored.)
  • Create ne Executive objects. Do this by setting up an ne count loop.
  • In each pass through the loop, do the following:
  • Ask the user to enter data for one executive.
  • Create an Executive object and initialize it with data provided by the user.
  • Store the Executive object reference in the array of Employee references created earlier. (Store the Executive references in the array elements following where Manager references were stored).
  • Using a For loop, call computeBonus and getter methods of the objects in the array to produce the required output. See Testing section for output. (See Sample Code section for details.)

Testing

Use the following test data below.

In the input data below, there are 3 workers, 2 managers and 1 executive.

The first worker listed is John Adam and has a yearly salary of $60000.00 and bonus is .05 (5%) of salary

The first manager listed is Ray Bartlett, has a salary of 80000, bonus is .10 (10%) of salary and travel expenses are 5000

The last one on the list is executive Andy Wong, has a salary of 80000, bonus is .15 (15%) of salary, travel expenses are 10000 and stock options are 500.

Input

Enter Number of Workers:
3
Enter Number of Managers:
2
Enter Number of Executives:
1
Enter a worker data:
John Adam, 60000, .05

Enter a worker data:
Rick Smith, 65000, .05

Enter a worker data:
Raymond Woo, 70000, .05

Enter a manager data:
Ray Bartlett, 80000, .10, 5000

Enter a manager data:
Mary Russell, 85000, .10, 5000

Enter an executive data:
Andy Wong, 100000, .15, 10000, 500

Output

(You do not need to have decimal point in exactly the same way)

Name: John Adam
Salary: 60000.00
PercentBonus: .05
Total Bonus: 3000.00

Name:: Rick Smith
Yearly Salary: 65000.00
PercentBonus: .05
Total Bonus: 3250.00

Name: Raymond Woo
Yearly Salary: 70000.00
PercentBonus: .05
Total Bonus: 3500.00

Name: Ray Bartlet
Yearly Salary: 80000.00
PercentBonus: .10
Total Bonus: 8500.00
Travel Expense: 5000.00

Name: Mary Russel
Yearly Salary: 85000.00
PercentBonus: .10
Total Bonus: 9000.00
Travel Expense: 5000.00


Name: Andy Wong
Yearly Salary: 100000.00
PercentBonus: .15
Total Bonus: 16000.00
Travel Expense: 10000.00
Options Count: 500

Sample Code

/*
In the code below, employee is an array of Employee references containing objects of Employee sub-classes Worker, Manager, Executive.
*/
String name;
double salary, pctBonus, bonus, travelExpense;
int optionsCount;

//outW will accumulate output for workers
//outM will accumulate output for manager
//outW will accumulate output for executive
//outAll will be the accumulated output for all

String outAll, outW=””, outM=””, outE=””;


for (int i=0; i < employee.length; i++){

//get name and salary
//To access target object methods that are defined in class Employee, we don’t need type casting.
name = employee[i].getName ( );
salary = employee[i].getSalary ( );

//call computeBonus using polymorphic call
//Since computeBonus is declared in Employee class, we don’t need type casting.
bonus = employee [i].computeBonus ( );

//To access target object methods that are not defined in class Employee, we use down casting.
//Downcasting (Widening) should be done within an if statement using instanceof
//Since getPctBonus method is not present in class Employee, it is accessed via down casting.
//Similarly getTravelExpense, and getOptionsCount are accessed via downcasting.
//The instanceof clause is used below to ensure a safe down casting (widening).

if (employee[i] instanceof Worker){
pctBonus = ( (Worker) employee[i] ).getPctBonus ( );
//accumulate Worker output in outW
outW = outW + “Name:” + name + “n”;
//add other values in outW


}
else if (employee[i] instanceof Manager) {
pctBonus = ( (Manager) employee[I] ).getPctBonus ( );
travelExpense = ( (Manager) employee[I] ).getTravelExpense ( );
//accumulate Manager output in outM
outM = outM + “Name:” + name + “n”;
//add other values in outM

}
else if (employee[i] instanceof Executive) {
pctBonus = ( (Executive) employee[I] ).getPctBonus ( );
travelExpense = ( (Executive) employee[I] ).getTravelExpense ( );
optionsCount = ( (Executive) employee[I] ).getOptionsCount ( );
//accumulate Executive output in outE
outE = outE + “Name:” + name + “n”;
//add other values in outE


}
}

//After getting out of the for loop
outAll = outW + outM + outE:
//display outAll
JOPtionPane.showMessageDialog(null, outAll);
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.