In: Computer Science
Design a class named Employee. The class should keep the following information in fields:
· Employee name
· Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M.
· Hire date
Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields to hold the following information:
· Shift (an integer)
· Hourly pay rate (a double)
· Add hours and calculate pay
The workday is divided into two shifts: day and night. The shift field will be an integer value representing the shift that the employee works. The day shift is shift 1 and the night shift is shift 2. Write one or more constructors and the appropriate accessor and mutator methods for the class. Demonstrate the classes by writing a program that uses a ProductionWorker object.
Employee.java
public class Employee {
// Declaring instance variables
private String employee_name;
private String employee_no;
private String hiredate;
// Default Constructor
public Employee() {
}
// Parameterized Constructor
public Employee(String employee_name, String
employee_no, String hiredate) {
this.employee_name =
employee_name;
this.employee_no =
employee_no;
this.hiredate = hiredate;
}
// Setters and Getters
public String getEmployee_name() {
return employee_name;
}
public void setEmployee_name(String employee_name)
{
this.employee_name =
employee_name;
}
public String getEmployee_no() {
return employee_no;
}
public void setEmployee_no(String employee_no)
{
this.employee_no =
employee_no;
}
public String getHiredate() {
return hiredate;
}
public void setHiredate(String hiredate) {
this.hiredate = hiredate;
}
// toString() method which displays the contents of
an Object inside it.
@Override
public String toString() {
System.out.println("Employee Name
=" + employee_name);
System.out.println("Employee No ="
+ employee_no);
System.out.println("Hiredate =" +
hiredate);
return "";
}
}
___________________
ProductionWorker.java
public class ProductionWorker extends Employee {
// Declaring instance variables
private double hourly_pay_rate;
private int shift;
private int no_of_hours;
// Default Constructor
public ProductionWorker() {
super();
}
// Parameterized Constructor
public ProductionWorker(String employee_name, String
employee_no,
String hiredate,
double hourly_pay_rate, int shift) {
super(employee_name, employee_no,
hiredate);
this.hourly_pay_rate =
hourly_pay_rate;
this.shift = shift;
}
// Setters and Getters
public double getHourly_pay_rate() {
return hourly_pay_rate;
}
public void setHourly_pay_rate(double
hourly_pay_rate) {
this.hourly_pay_rate =
hourly_pay_rate;
}
public int getShift() {
return shift;
}
public void setShift(int shift) {
this.shift = shift;
}
public void addHours(int hours)
{
this.no_of_hours+=hours;
}
public double calculate_Pay()
{
return
no_of_hours*hourly_pay_rate;
}
// toString() method which displays the contents of
an Object inside it.
@Override
public String toString() {
super.toString();
System.out.println("Hourly Pay Rate
=" + hourly_pay_rate);
if(getShift()==1)
System.out.println("Shift =
Day");
else if(getShift()==2)
System.out.println("Shift =
Night");
return "";
}
}
___________________
Driver.java
public class Driver {
public static void main(String[] args) {
//Creating the Production Worker Object by passing the
parameters
ProductionWorker pw1=new
ProductionWorker("Williams","123-B","31-Dec-2014", 10, 1);
//Displaying the Production worker details
System.out.println("===== Production Worker Details =====");
pw1.toString();
pw1.addHours(39);
System.out.println("Payment for Production Worker
1:$"+pw1.calculate_Pay());
//Creating the Production Worker Object by passing the
parameters
ProductionWorker pw2=new
ProductionWorker("Pitersen","345-H","11-Feb-2006", 15, 2);
//Displaying the Production worker details
System.out.println("===== Production Worker Details =====");
pw2.toString();
pw2.addHours(35);
System.out.println("Payment for Production Worker 2:
$"+pw2.calculate_Pay());
}
}
_____________________
Output:
===== Production Worker Details =====
Employee Name =Williams
Employee No =123-B
Hiredate =31-Dec-2014
Hourly Pay Rate =10.0
Shift = Day
Payment for Production Worker 1:$390.0
===== Production Worker Details =====
Employee Name =Pitersen
Employee No =345-H
Hiredate =11-Feb-2006
Hourly Pay Rate =15.0
Shift = Night
Payment for Production Worker 2: $525.0
____________Thank You