Implement the following classes An abstract class Employee is the super class of
ID: 3548486 • Letter: I
Question
Implement the following classes
An abstract class Employee is the super class of the hierarchy. It has two instance variables Name and Department (of type String). It has a constructor that initializes the two instance variables to the specified values. In addition to the set and get methods, it has three methods: toString() that returns the name and department of the employee, an abstract method workingHoursPerMonth()and an abstract method calculateSalary(). Class Manager is derived from Employee. It has one instance variable NoEngineers (of type int). In addition to the constructor, it has three methods: A method workingHoursPerMonth() that implements the abstract method and returns the working hours of the manager per month. The working hours equals 140 if the manager works in the "IT" department and 120 if the manager works in the "Accounting" department. A method calculateSalary()that implements the abstract method and returns the salary of the manager. The salary is calculated as follows: salary = working Hours Per Month * 20.8 + No of Enigineers * 10.5 A method toString() that overrides the super class version and returns the name, department, No. of engineers supervised by the manager and the salary. Class Engineer is derived from Employee. It has one instance variable NoProjects (of type int). In addition to the constructor, it has three methods: A method workingHoursPerMonth() that implements the abstract method and returns the working hours of the engineer per month. The working hours equals 180 if the engineer works in the "IT" department and 160 if the manager works in the "Accounting" department. A method calculateSalary()that implements the abstract method and returns the salary of the engineer. The salary is calculated as follows: salary = working Hours Per Month * 12.1 + No of Projects * 50.9 A method toString() that overrides the super class version and returns the name, department, No. of projects assigned to the engineer and the salary. Write a test class named Test that has a main method to test your classes. In the main method, prompt the user to input the Employee information and then the type of employee. Display the information of the Employee as shown in the sample output.Explanation / Answer
import java.util.*;
abstract class Employee
{
private String name;
private String Department;
public Employee(String myname,String mydepartment)
{
name = myname;
Department = mydepartment;
}
public void setName(String myname)
{
name = myname;
}
public void setDepartment(String mydepartment)
{
Department = mydepartment;
}
public String getName()
{
return name;
}
public String getDepartment()
{
return name;
}
public String toString()
{
return name + " working in "+ Department;
}
public abstract int workingHoursPerMonth();
public abstract double calculateSalary();
}
class Manager extends Employee
{
private int NoEngineers;
public Manager(String name,String department,int engg_no)
{
super(name,department);
NoEngineers = engg_no;
}
public int workingHoursPerMonth()
{
if(getDepartment().equalsIgnoreCase("IT"))
return 120;
if(getDepartment().equalsIgnoreCase("Accounting"))
return 140;
return 0;
}
public double calculateSalary()
{
return workingHoursPerMonth() * 20.8 + NoEngineers * 10.5;
}
public String toString()
{
return super.toString() + " was supervising " + NoEngineers + " and salary is " + calculateSalary();
}
}
class Engineer extends Employee
{
private int NoProjects;
public Engineer(String name,String department, int proj_no)
{
super(name,department);
NoProjects = proj_no;
}
public int workingHoursPerMonth()
{
if(getDepartment().equalsIgnoreCase("IT"))
return 180;
if(getDepartment().equalsIgnoreCase("Accounting"))
return 160;
return 0;
}
public double calculateSalary()
{
return workingHoursPerMonth() * 12.1 + NoProjects * 50.9;
}
public String toString()
{
return super.toString() + " was assigned " + NoProjects + " projects and salary is " + calculateSalary();
}
}
public class Test
{
public static void main(String[] args)
{
String name,department;
int no;
char type;
Scanner in = new Scanner(System.in);
System.out.println("Enter name of employee :");
name = in.next();
System.out.println("Enter department of employee :");
department = in.next();
System.out.println("Enter type of employee (E for Engineer and M for Manager) :");
type = in.next().charAt(0);
if(type=='M')
{
System.out.println("Enter No of Engineers :");
no = in.nextInt();
Manager man = new Manager(name,department,no);
System.out.println(man);
}
else if(type == 'E')
{
System.out.println("Enter No of Projects :");
no = in.nextInt();
Engineer man = new Engineer(name,department,no);
System.out.println(man);
}
}
}