194,861 views
3 votes
3 votes
Design two subclasses of Employee…SalariedEmployee and HourlyEmployee. A salaried employee has an annual salary attribute. An hourly employee has an hourly pay rate attribute, an hours worked attribute, and an earnings attribute. An hourly employee that works more than 40 hours gets paid at 1.5 times their hourly pay rate. You will decide how to implement constructors, getters, setters, and any other methods that might be necessary. 1. (20 points) Draw a UML diagram for the classes. 2. (80 points) Implement the classes, and write a test program that creates a salaried employee and two hourly employees. One of the hourly employees should have hours worked set to less than 40 and one should have hours worked set to more than 40. The test program should display all attributes for the three employees. To keep things simple, the employee classes don’t need to do any editing.

public class Employee
{
private int empID;
Address address;
Name name;
Date date;

public Employee()
{
}

public Employee(int empID)
{
this.empID = empID;
}

public int getEmpID()
{
return empID;
}

public void setEmpID(int empID)
{
this.empID = empID;
}

public Address getAddress()
{
return address;
}

public void setAddress(Address address)
{
this.address = address;
}

public Name getName()
{
return name;
}

public void setName(Name name)
{
this.name = name;
}

public Date getDate()
{
return date;
}

public void setDate(Date date)
{
this.date = date;
}

}

User Asaf Shveki
by
2.9k points

1 Answer

4 votes
4 votes

Answer:

Complete answer to above question is given below.

Step-by-step explanation:

Employee.java

import java.util.Date;

public class Employee

{

private int empID;

private Address address;

private Name name;

private Date date;

public Employee()

{

}

public Employee(int empID)

{

this.empID = empID;

}

public int getEmpID()

{

return empID;

}

public void setEmpID(int empID)

{

this.empID = empID;

}

public Address getAddress()

{

return address;

}

public void setAddress(Address address)

{

this.address = address;

}

public Name getName()

{

return name;

}

public void setName(Name name)

{

this.name = name;

}

public Date getDate()

{

return date;

}

public void setDate(Date date)

{

this.date = date;

}

}

Name.java

public class Name {

private String name;

public Name(String name) {

super();

this.name = name;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

Address.java

public class Address {

private String addr;

public Address(String addr) {

super();

this.addr = addr;

}

public String getAddr() {

return addr;

}

public void setAddr(String addr) {

this.addr = addr;

}

}

SalariedEmployee.java

public class SalariedEmployee extends Employee {

private double annualSalary;

public SalariedEmployee(double annualSalary) {

super();

this.annualSalary = annualSalary;

}

public double getAnnualSalary() {

return annualSalary;

}

public void setAnnualSalary(double annualSalary) {

this.annualSalary = annualSalary;

}

}

HourlyEmployee.java

public class HourlyEmployee extends Employee {

private double hourlyrate;

private double hours_worked;

private double earnings;

public HourlyEmployee(double hourlyrate, double hours_worked) {

super();

this.hourlyrate = hourlyrate;

this.hours_worked = hours_worked;

}

public double getEarnings()

{

if(hours_worked<=40)

{

earnings=hours_worked*hourlyrate;

}

else if(hours_worked>40)

{

earnings=40*hourlyrate+(hours_worked-40)*hourlyrate*1.5;

}

return earnings;

}

public double getHourlyrate() {

return hourlyrate;

}

public void setHourlyrate(double hourlyrate) {

this.hourlyrate = hourlyrate;

}

public double getHours_worked() {

return hours_worked;

}

public void setHours_worked(double hours_worked) {

this.hours_worked = hours_worked;

}

public void setEarnings(double earnings) {

this.earnings = earnings;

}

Test.java

import java.util.Date;

public class Test {

public static void main(String[] args) {

System.out.println("_____Salaried Employee_____");

SalariedEmployee se=new SalariedEmployee(50000);

se.setEmpID(1234);

se.setName(new Name("Williams"));

se.setAddress(new Address("4,Richmond Street"));

se.setDate(new Date("Oct-11-2014"));

System.out.println("Employee Id :"+se.getEmpID());

System.out.println("Employee Name :"+se.getName().getName());

System.out.println("Employee Address :"+se.getAddress().getAddr());

System.out.println("Joining Date :"+se.getDate());

System.out.println("The Annual Salary :"+se.getAnnualSalary());

System.out.println("\\_____Hourly Employee_____");

HourlyEmployee he1=new HourlyEmployee(8.5,39);

he1.setEmpID(4567);

he1.setName(new Name("Kane"));

he1.setAddress(new Address("5,lake View Road"));

he1.setDate(new Date("Nov-12-2015"));

System.out.println("Employee Id :"+he1.getEmpID());

System.out.println("Employee Name :"+he1.getName().getName());

System.out.println("Employee Address :"+he1.getAddress().getAddr());

System.out.println("Joining Date :"+he1.getDate());

System.out.println("The Earnings Of An HourlyEmployee :"+he1.getEarnings());

System.out.println("\\_____Hourly Employee_____");

HourlyEmployee he2=new HourlyEmployee(9.5,47);

he2.setEmpID(1111);

he2.setName(new Name("John"));

he2.setAddress(new Address("17,Villey Parley Street"));

he2.setDate(new Date("Dec-13-2011"));

System.out.println("Employee Id :"+he2.getEmpID());

System.out.println("Employee Name :"+he2.getName().getName());

System.out.println("Employee Address :"+he2.getAddress().getAddr());

System.out.println("Joining Date :"+he2.getDate());

System.out.println("The Earnings Of An HourlyEmployee :"+he2.getEarnings());

}

}

Output:

_____Salaried Employee_____

Employee Id :1234

Employee Name :Williams

Employee Address :4,Richmond Street

Joining Date :Sat Oct 11 00:00:00 IST 2014

The Annual Salary :50000.0

_____Hourly Employee_____

Employee Id :4567

Employee Name :Kane

Employee Address :5,lake View Road

Joining Date :Thu Nov 12 00:00:00 IST 2015

The Earnings Of An HourlyEmployee :331.5

_____Hourly Employee_____

Employee Id :1111

Employee Name :John

Employee Address :17,Villey Parley Street

Joining Date :Tue Dec 13 00:00:00 IST 2011

The Earnings Of An HourlyEmployee :479.75

User Param Yadav
by
4.1k points