Please write in JAVA ( inheritance) Consider the Company class and the program o
ID: 3879904 • Letter: P
Question
Please write in JAVA (inheritance)
Consider the Company class and the program output given. Now, design a class hierarchy that implements company employee’s salary calculation. A company has two types of employees: hourly paid employees and monthly salary based employees. Every employee has a name, salary and tax rate.
In addition, hourly paid employee has two attributes: hours worked and the hourly rate. The monthly paid employee has one attribute, annual salary. Both classes have constructor methods that take all attributes and a method to calculate monthly salary.
Finally both classes should have the toString method that returns name, monthly salary, and the tax paid
ArrayList<Employee> empList= new ArrayList<Employee>(); monthlyPaidEmp mpE1 = new monthlyPaidEmp("John William" , 55675.00);
monthlyPaidEmp mpE2 = new monthlyPaidEmp("Nancy William" , 65675.00); hourlyPaidEmp hpE1 = new hourlyPaidEmp("Samira Monid", 7.50, 35);
}
}
} }
System.out.println(empList.get(i));
a) Draw the UML class diagram that represents the above class hierarchy b) Implement the classes in your UML diagram above
Explanation / Answer
Hi ,
Please see below the classes:
Employee.java
public class Employee {
private String name;
private double salary;
private double taxRate ;
//Constructor
public Employee(String name, double salary) {
super();
this.name = name;
this.salary = salary;
this.taxRate = 0.05;;
}
//To calculate Salary
public double calculateSalary(){
return getSalary();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getTaxRate() {
return taxRate;
}
public void setTaxRate(double taxRate) {
this.taxRate = taxRate;
}
}
MonthlyPaidEmp.java
public class MonthlyPaidEmp extends Employee {
private double annualSalary;
public MonthlyPaidEmp(String name, double salary) {
super(name, salary);
this.annualSalary = salary;
}
//To calculate Salary
public double calculateSalary(){
//monthly salary = annual salary/12
System.out.println("Calculating salary for Monthly Paid Emp..");
this.setSalary(this.getAnnualSalary()/12);
return getSalary();
}
public double getAnnualSalary() {
return annualSalary;
}
public void setAnnualSalary(double annualSalary) {
this.annualSalary = annualSalary;
}
public String toString() {
return "SalaryBasedEmployee [Name()=" + getName() + ", Salary()=" + getSalary()
+ ", TaxPaid()=" + getTaxRate()*getSalary() + "]";
}
}
HourlyPaidEmp.java
public class HourlyPaidEmp extends Employee {
private int hoursWorked;
private double hourlyRate;
public HourlyPaidEmp(String name, double hourlyRate, int hoursWorked) {
super(name, hourlyRate * hoursWorked);
this.hoursWorked = hoursWorked;
this.hourlyRate = hourlyRate;
}
//To calculate Salary
public double calculateSalary(){
//Monthly salary = hoursWorked * hourlyRate
System.out.println("Calculating salary for Hourly Paid Emp..");
this.setSalary(this.getHoursWorked()* this.getHourlyRate());
return getSalary();
}
public int getHoursWorked() {
return hoursWorked;
}
public void setHoursWorked(int hoursWorked) {
this.hoursWorked = hoursWorked;
}
public double getHourlyRate() {
return hourlyRate;
}
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
public String toString() {
return "HourlyPaidEmployee [Name()=" + getName()
+ ", Salary()=" + getSalary() + ", TaxPaid()=" + getTaxRate()*getSalary()+ "]";
}
}
Company.java
import java.util.ArrayList;
public class Company {
public static void main(String[] args)
{
ArrayList<Employee> empList= new ArrayList<Employee>();
MonthlyPaidEmp mpE1 = new MonthlyPaidEmp("John William" , 55675.00);
MonthlyPaidEmp mpE2 = new MonthlyPaidEmp("Nancy William" , 65675.00);
HourlyPaidEmp hpE1 = new HourlyPaidEmp("Samira Monid", 7.50, 35);
empList.add(mpE1);
empList.add(mpE2);
empList.add(hpE1);
for(int i=0; i<empList.size() ; i++)
{
Employee e= empList.get(i);
e.calculateSalary();
empList.set(i,e);
}
for(int i=0; i<empList.size() ; i++)
{
System.out.println(empList.get(i));
}
}}