Suppose a company has two types of workers working for them - HourlyEmployee and
ID: 3736645 • Letter: S
Question
Suppose a company has two types of workers working for them - HourlyEmployee and FullTimeEmployee. The hourly employees are paid on hourly basis and they work 40 hours a week whereas, full time employees are paid on monthly basis. Given the below code implementations for HourlyEmployee and FullTimeEmployee class and a client program – EmployeeTest class that is a client of HourlyEmployee and FullTimeEmployee, evaluate code design to check whether the design follows Open-Close and Liskov Substitution Principle. Your argument should provide appropriate explanation for whether these design principles are followed or violated. If your evaluation states that the given code design violates either or both of the design principles, explain what needs to be done in terms of the changing the design. Provide a class diagram of the improved design.
public interface Employee
{
//this is an empty interface with no methods.
}
//SalariedEmployee represents a fulltime employee
public class FullTimeEmployee implements Employee
{
private int annualSalary;
public FullTimeEmployee(int sal)
{
annualSalary = sal;
}
public double salaryFullTimeEmployee()
{
return annualSalary/12;
}
}
public class HourlyWageEmployee implements Employee
{
//hourly wage rate
private int wageRate;
public HourlyWageEmployee(double wage)
{
wageRate = wage;
}
//salary( ) method returns the weekly salary
//
public int salaryHourlyWageEmployee()
{
return 40 * wageRate;
}
}
//client program using the Employee types
public class EmployeeTest
{
public double returnSalary(Employee e )
{
if(e instanceof FullTimeEmployee)
{
FullTimeEmployee s = (FullTimeEmployee) e;
return s.salaryFullTimeEmployee();
}
else
{
HourlyWageEmployee h = (HourlyWageEmployee) e;
return h.salaryHourlyWageEmployee();
}
}
}
Explanation / Answer
Open Closed Principal : FOLLOWED
Open : Its open for extending to other derived classes.
Closed : No need of modification to the existing code.
Liskov Substitution Principle:
functions that use pointers to base classes must be able to use objects of derived classes without knowing it.
NOT followed
because such function are not present in the class design.
To make it work declare some functions in base class and extends those in derived class which can be accessed via base class pointers without knowing the type of derived object
ex -
public class Employee {
public void work() {
System.out.println("Employee is working");
}
}
public class salaryFullTimeEmployee extends Employee {
@Override
public void work() {
throw new IllegalArgumentException("salaryFullTimeEmployee gets per month");
}
}
public class salaryHourlyWageEmployee extends Employee {
@Override
public void work() {
throw new IllegalArgumentException("salaryHourlyWageEmployee gets wages hourly");
}
}
Hope it helps
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.