Define an Employee class. An Employee has a name, an ID number, an age, a salary
ID: 3591748 • Letter: D
Question
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 If any change causes an inconsistency in the data, your program should print an error and not allow the change.
Your program should access an employee via the employee ID number. Use a binary search to find an employee’s record.
Explanation / Answer
Hi, Please find my implementation.
########
public class Employee {
private String name;
private int id;
private int age;
private String title;
private int deptNo;
private double salary;
public void displayInfo() {
System.out.println("Name: "+name);
System.out.println("Employee ID: "+id);
System.out.println("Age: "+age);
System.out.println("Title: "+title);
System.out.println("Department Number: "+deptNo);
System.out.println("Salary: "+salary);
}
public void changeSalary(double increase) {
salary = salary*(increase+1);
}
public void changeSalary(int increase) {
salary = salary + increase;
}
public double getSalary() {
return salary;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public int getAge() {
return age;
}
public String getTitle() {
return title;
}
public int getDeptNo() {
return deptNo;
}
public void setName(String name) {
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public void setAge(int age) {
this.age = age;
}
public void setTitle(String title) {
this.title = title;
}
public void setDeptNo(int deptNo) {
this.deptNo = deptNo;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
##########
import java.util.ArrayList;
public class Manager extends Employee{
private ArrayList<Integer> employees;
public Manager() {
employees = new ArrayList<>();
}
public ArrayList<Integer> getEmployees() {
return employees;
}
public void setEmployees(ArrayList<Integer> employees) {
this.employees = employees;
}
}
###########
public class Executive extends Manager{
private double bonusRate = 0.1;
public double getBonusRate() {
return bonusRate;
}
public void setBonusRate(double bonusRate) {
this.bonusRate = bonusRate;
}
// redefining
public double getSalary() {
return super.getSalary() + super.getSalary()*bonusRate;
}
}
##########
public class TestEmployee {
public static void main(String[] args) {
Employee emp1 = new Employee();
emp1.setAge(25);
emp1.setDeptNo(3);
emp1.setId(1);
emp1.setName("Nupur");
emp1.setTitle("Ms");
emp1.setSalary(345);
emp1.displayInfo();
System.out.println(" Changing salary: ");
emp1.changeSalary(0.2);
emp1.displayInfo();
System.out.println(" Creating Executive");
Executive ex = new Executive();
ex.setAge(43);
ex.setDeptNo(2);
ex.setId(1);
ex.setName("RAhul R");
ex.setTitle("Mr");
ex.setSalary(3435);
ex.displayInfo();
System.out.println(" Changing salary: ");
ex.changeSalary(321);
ex.displayInfo();
ex.setBonusRate(0.3);
System.out.println("Salary after bonus change: "+ex.getSalary());
}
}
/*
Sample run:
Name: Nupur
Employee ID: 1
Age: 25
Title: Ms
Department Number: 3
Salary: 345.0
Changing salary:
Name: Nupur
Employee ID: 1
Age: 25
Title: Ms
Department Number: 3
Salary: 414.0
Creating Executive
Name: RAhul R
Employee ID: 1
Age: 43
Title: Mr
Department Number: 2
Salary: 3435.0
Changing salary:
Name: RAhul R
Employee ID: 1
Age: 43
Title: Mr
Department Number: 2
Salary: 3756.0
Salary after bonus change: 4882.8
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.