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

A Basic Inheritance Hierarchy with Polymorphism. Define an Employee class. An Em

ID: 3843347 • Letter: A

Question

A Basic Inheritance Hierarchy with Polymorphism. Define an Employee class. An Employee has a name, an ID number, an age, a salary, a title, and a department name. The methods of Employee should: a. Print an employee record that includes all of the above information b. Change a salary, changeSalary(), and c. return the salary, getSalary(). The method changeSalary() accepts a parameter, increase, of type int or double. If increase is an int, then the salary should be increased by that amount. If increase is double, then the new salary should be (increase + 1) times the salary. For example, if the increase is 0.10, the salary is multiplied by 1.10, yielding an increase of 10%. The value of the (double) increase should be between 0.0 and 1.0. Define a class Manager that Extends Employee. A manager is an employee who supervises other employees. A Manager object should include all data of the Employee object plus the list of the employee ID numbers of those employees under his/her supervision as well as all the other relevant data. Define a class Executive that extends Manager. An Executive is a Manager who receives a bonus a the end of each year equal to percentage of his/her regular salary. Each Executive has his/her own bonus rate. You will need to redefine getSalary() to include the bonus. You will also need to add a setter (mutator) method, setBonus(), to set the percentage of the executive’s bonus. The default bonus rate should be 10%. Implement a test class that demonstrates the facilities of the Employee, Manager, and Executive classes. Your test class should accept employee information for an arbitrary number of employees. Your program should ask whether or not the employee is a manager or an executive, and prompt for all relevant information. After all data are entered, print an error message if there are any inconsistencies. In particular, a manager cannot manage a nonexistent employee. Also, every employee who is not an executive is supervised by some manager or executive. Your program should provide the user with the following options: • Change the salary of an employee • Adjust the bonus of an executive • Add or delete an employee from a manager’s list of employees • Print an employee’s data

For this assignment in particular, please focus on creating the Employee, Manager and Executive classes to support the driver program displayed below:

public class useEmployee
{
public static void main(String[] args)
{
Employee[] employees = new Employee[100];
employees[0] = new Employee(5950, "Fred Yates", 37, 360000.00, "MTS 3", "Scientists");
employees[1] = new Manager(1215, "Amanda Deleon", 53, 225000.00, "Manager Level 3", "Research and Development");
employees[2] = new Employee(6120, "Susan B. Anthony", 37, 360000.00, "MTS 3", "Engineers");
employees[3] = new Employee(8978, "Tyron Rothschild", 23, 42000.00, "MTS 1", "Engineers");
employees[4] = new Executive(2, "Baron Rothschild", 79, 500000.00, "Chairman", "Engineers", 0.15);

((Manager)(employees[1])).addEmployee(employees[0]);
((Manager)(employees[1])).addEmployee(employees[2]);
((Manager)(employees[1])).addEmployee(employees[3]);

System.out.println("Amanda's direct reports: ");

((Manager)(employees[1])).printEmployeeList();        // why not use toString() here?
System.out.println(" --- All Employees --- ");

int i = 0;
                                                       // employees[0].changeSalary(employees, 8978, 25000); ** OLD STYLE ** Why is this not optimal?
employees[3].changeSalary(25000);
while (employees[i] != null)
System.out.println(employees[i++]);
}
}

Explanation / Answer

package employee;

public class Employee {

   private String name;
   private int id;
   private int age;
   private double salary;
   private String title;
   private String departmentName;
  
   public Employee(int id,String name,int age,double salary, String title, String departmentName) {
       this.id = id;
       this.name = name;
       this.age = age;
       this.salary = salary;
       this.title = title;
       this.departmentName = departmentName;
   }
  
   public void printEmployee(){
       System.out.println("Emplyee Details Name: "+this.name);
       System.out.println("Id: "+this.id);
       System.out.println("Age: "+this.age);
       System.out.println("Salary: "+this.salary);
       System.out.println("Title: "+this.title);
       System.out.println("Department Name: "+this.departmentName);
       System.out.println();
   }
  
   public void changeSalary(int increment){
       this.salary = salary+increment;
   }
  
   public void changeSalary(double increment) throws Exception{
       if(increment>=0 && increment <1.0){
           this.salary = (increment+1)*salary;
       }
       else{
           throw new Exception("Increment should be in the range 0.0 to 1.0");
       }
   }
  
   public double getSalary(){
       return this.salary;
   }
  
  
}

package employee;

import java.util.ArrayList;

public class Manager extends Employee{

   ArrayList<Employee> employees;
  
   public Manager(int id,String name,int age,double salary, String title, String departmentName) {
       super(id,name,age,salary,title,departmentName);
       employees = new ArrayList<Employee>();
   }
  
   public void addEmployee(Employee e){
       employees.add(e);
   }

   public void printEmployeeList(){
       for(int i=0;i<employees.size();i++){
           employees.get(i).printEmployee();
       }
   }
}

package employee;

public class Executive extends Manager{

   private double bonusRate;
  
   public Executive(int id,String name,int age,double salary, String title, String departmentName, double bonusRate) {
       super(id,name,age,salary,title,departmentName);
       this.bonusRate = bonusRate;
   }
  
   public void setBonus(double bonusRate){
       if(bonusRate>=0 && bonusRate<1.0){
           this.bonusRate = bonusRate;
       }
       else{
           System.out.println("Invalid bonus rate");
       }
   }
  
   @Override
   public double getSalary(){
      
       return this.getSalary()+this.getSalary()*(1+bonusRate);
   }
}

Output:

Amanda's direct reports:
Emplyee Details
Name: Fred Yates
Id: 5950
Age: 37
Salary: 360000.0
Title: MTS 3
Department Name: Scientists

Emplyee Details
Name: Susan B. Anthony
Id: 6120
Age: 37
Salary: 360000.0
Title: MTS 3
Department Name: Engineers

Emplyee Details
Name: Tyron Rothschild
Id: 8978
Age: 23
Salary: 42000.0
Title: MTS 1
Department Name: Engineers


--- All Employees ---

employee.Employee@2a139a55
employee.Manager@15db9742
employee.Employee@6d06d69c
employee.Employee@7852e922
employee.Executive@4e25154f

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote