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

Topics: Object Type Casting, Upcasting, Downcasting, instanceof, Polymorphism, P

ID: 3855557 • Letter: T

Question

Topics: Object Type Casting, Upcasting, Downcasting, instanceof, Polymorphism, Polymorphism using inheritance

Description

Write a program to compute bonuses earned this year by employees in an organization. There are three types of employees: workers, managers and executives. 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.

You are to compute and display bonuses for all the employees in the organization using a single polymorphic loop. This will be done by creating an abstract class Employee and making the classes Worker, Manager and Executive as subclasses of the class Employee. This will allow for creating an array of Employee references and storing in it references of Worker, Manager and Executive objects. (This can be done because objects of classes, Worker, Manager and Executives are also of type Employee as Employee is their parent class and they inherit all its characteristics).

Implementation

Create the following classes:

class Employee

Create an abstract class Employee that includes the following:

Two fields, name and 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. (Do not provide a body for this method).

class Worker

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

A field pctBonus for specifying the 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 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 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).

· Find the total bonus amount paid to each employee.

· Do this by calling the function computeBonus( ) of each object

· using a polymorphic call within a single polymorphic loop as shown below.

Testing:

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

The first employee is John Adam and has a yearly salary of $60000.00 and is paid 5% yearly bonus.

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:

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

Explanation / Answer

import java.util.*;
abstract class Employee
{
   Scanner s=new Scanner(System.in);
String Ename;
double sal;
public Employee()
{
Ename="";
sal=0;
}
public String getEname()
{
   System.out.print("Enter Ename");
Ename=s.next();
return Ename;
}
public double getSal()
{
   System.out.print("Enter Sal");
sal=s.nextInt();
return sal;
}
public abstract double computeBonus();
}

class Worker extends Employee
{
double pctBonus,bonus;
public Worker()
{
pctBonus=0;
}
public double getPctBonus()
{
System.out.print("Enter PctBonus");
pctBonus=s.nextDouble();
   return pctBonus;

}
public double computeBonus()
{
bonus=sal*pctBonus;
return(bonus);
}
public void printdata()
{
   System.out.println(Ename+","+sal+","+pctBonus+","+bonus);
}
}

class Manager extends Employee
{
double pctBonus,travelExpense,bonus;
public Manager()
{
pctBonus=0;
travelExpense=0;
}
public double getPctBonus()
{
System.out.print("Enter PctBonus");
pctBonus=s.nextDouble();
return pctBonus;
}
public double gettravelExpense()
{
System.out.print("Enter travelExpense");
travelExpense=s.nextDouble();
return travelExpense;
}
public double computeBonus()
{
bonus=(sal*pctBonus)+500;
return(bonus);
}
public void printdata()
{
   System.out.println(Ename+","+sal+","+travelExpense+","+pctBonus+","+bonus);
}
}

class Executive extends Employee
{
double pctBonus,travelExpense,optionsCount,bonus;
public Executive()
{
pctBonus=0;
travelExpense=0;
}
public double getPctBonus()
{
System.out.print("Enter PctBonus");
pctBonus=s.nextDouble();
return pctBonus;
}
public double getoptionsCount()
{
System.out.print("Enter optionCount");
optionsCount=s.nextDouble();
return optionsCount;
}
public double gettravelExpense()
{
   System.out.print("Enter travelExpense");
travelExpense=s.nextDouble();
return travelExpense;
}
public double computeBonus()
{
bonus=(sal*pctBonus)+1000;
return(bonus);
}
public void printdata()
{
   System.out.println(Ename+","+sal+","+travelExpense+","+optionsCount+","+pctBonus+","+bonus);
}
}

class TestEmployee
{
public static void main(String ar[])
{
Scanner s=new Scanner(System.in);
int nw,nm,ne;
System.out.print("Enter the number of Workers");
nw=s.nextInt();
System.out.print("Enter the number of Managers");
nm=s.nextInt();
System.out.print("Enter the number of Executives");
ne=s.nextInt();
Worker w[]=new Worker[nw];
Manager m[]=new Manager[nm];
Executive e[]=new Executive[ne];
for(int i=0;i<nw;i++)
{
   w[i]=new Worker();
   System.out.println("enter "+(i+1)+" Worker Details");
   w[i].getEname();
   w[i].getSal();
   w[i].getPctBonus();
}
for(int i=0;i<nm;i++)
{
   m[i]=new Manager();
   System.out.println("enter "+(i+1)+" Manager Details");
   m[i].getEname();
   m[i].getSal();
   m[i].gettravelExpense();
   m[i].getPctBonus();
}
for(int i=0;i<ne;i++)
{
   e[i]=new Executive();
   System.out.println("enter "+(i+1)+" Executive Details");
   e[i].getEname();
   e[i].getSal();
   e[i].getPctBonus();
   e[i].getoptionsCount();
   e[i].gettravelExpense();
}
int n=nw+nm+ne;
for (int i=0; i <nw; i++)
{
        w[i].printdata();
}
for (int i=0; i <nm; i++)
{
        m[i].printdata();
}
for (int i=0; i <ne; i++)
{
        e[i].printdata();
}
}
}