A company pays its employees on a weekly basis. The employees are of four types:
ID: 3547686 • Letter: A
Question
A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid a fixed weekly salary regardless of the number of hours worked, hourly employees are paid by the hour and receive overtime pay (1.5 * wage) for all hours worked in the excess of 40 hours, commission employees are paid a percentage of their sales and base-commission employees receive a base salary plus a commission based on their gross sales. The company wants to implement a java application that performs its payroll calculations polymorphically.
1. Write a Java class
Explanation / Answer
public class Employee
{
// instance variables
protected String fName;
protected String lName;
protected double ssn;
protected double earnings;
public Employee()
{
earnings = 0.0;
}
public Employee(String first, String last, double socialSec)
{
fName = first;
lName = last;
ssn = socialSec;
}
public void setFirstName(String f_Name)
{
fName = f_Name;
}
public String getFirstName()
{
return fName;
}
public void setLastName(String l_Name)
{
lName = l_Name;
}
public String getLastName()
{
return lName;
}
public void setSsn(double social)
{
ssn = social;
}
public double getSsn()
{
return ssn;
}
public double getEarnings()
{
return earnings;
}
public String toString()
{
return ("The employees name is " + getFirstName() + " " + getLastName() + ". The employees social security number is " + getSsn());
}
}
2.public class SalariedEmployee extends Employee
{
private double weeklySalary;
public SalariedEmployee()
{
super();
earnings = 0.0;
}
public SalariedEmployee(double weeklySal)
{
super();
if(weeklySal > 0)
weeklySalary = weeklySal;
else
weeklySalary = 0;
}
public void setWeeklySalary(double salary)
{
if(salary > 0)
weeklySalary = salary;
else
weeklySalary = 0;
}
public double getWeeklySalary()
{
return weeklySalary;
}
public double getEarnings(double numberWeeks)
{
if(numberWeeks>0)
earnings = weeklySalary*numberWeeks;
else
earnings = 0;
return earnings;
}
public String toString()
{
return super.toString() + ". The weekly salary is " + getWeeklySalary();
}
}
3.
public class HiredEmployee extends Employee
{
private double wage;
private double hours;
public HiredEmployee()
{
super();
wage = 0.0;
hours = 0;
}
public HiredEmployee(double w, double h)
{
super();
if(w > 0 && h > 0)
{
wage = w;
hours = h;
}
else
{
wage = 0;
hours = 0;
}
}
public void setWage(double nWage)
{
if(nWage > 0)
wage = nWage;
else
wage = 0;
}
public double getWage()
{
return wage;
}
public void setHours(double nHours)
{
if(nHours > 0)
hours = nHours;
else
hours = 0;
}
public double getHours()
{
return hours;
}
public double getEarnings()
{
earnings = wage * hours;
return earnings;
}
public String toString()
{
return super.toString() + ". The employees wage is " + getWage() + ". The employees hours are " + getHours();
}
}
4.
public class CommissionEmployee extends Employee
{
protected double grossSales;
protected double commissionRate;
public CommissionEmployee()
{
super();
grossSales = 0.0;
commissionRate = 0.0;
}
public CommissionEmployee(double gS, double cR)
{
super();
if(gS > 0 && cR > 0)
{
grossSales = gS;
commissionRate = cR;
}
else
{
grossSales = 0;
commissionRate = 0;
}
}
public void setGrossSales(double nGS)
{
if(nGS > 0)
grossSales = nGS;
else
grossSales = 0;
}
public double getGrossSales()
{
return grossSales;
}
public void setCommissionRate(double nCS)
{
if(nCS > 0)
commissionRate = nCS;
else
commissionRate = 0;
}
public double getCommissionRate()
{
return commissionRate;
}
public String toString()
{
return super.toString() + ". The employees gross sales are " + getGrossSales() + ". The employees commission rate is " + getCommissionRate();
}
public double getEarnings()
{
earnings = commissionRate * grossSales;
return earnings;
}
}
5.public class BasePlusCommissionEmployee extends CommissionEmployee
{
private double baseSalary;
public BasePlusCommissionEmployee()
{
super();
baseSalary = 0.0;
}
public BasePlusCommissionEmployee(double bS)
{
super();
if(bS > 0)
baseSalary = bS;
else
baseSalary = 0;
}
public void setBaseSalary(double base)
{
if(base>0)
baseSalary = base;
else
baseSalary = 0;
}
public double getBaseSalary()
{
return baseSalary;
}
public double getEarnings()
{
earnings = commissionRate * grossSales + baseSalary;
return earnings;
}
public String toString()
{
return super.toString() + ". The employees base salary is " + getBaseSalary();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.