Employee and ProductionWorker Classes Design a class named Employee. The class s
ID: 3861705 • Letter: E
Question
Employee and ProductionWorker Classes
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 on or moe 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 (double)
The worday is divided into two shifts: day and night. The shift field will be an interger 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.
Code superclass called Employee and subclass called ProductionWorker, and InheritanceApp as the driver class.
Use Java and please use comments to explain. Thank you!
Explanation / Answer
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;
// 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;
}
// 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) {
Employee emp=new Employee("Johnson", "123B","12-Nov-2011");
//Displaying the Employee details
System.out.println("===== Employee Details =====");
emp.toString();
//Creating the Production Worker Object by passing the parameters
ProductionWorker pw1=new ProductionWorker("Williams","111C","31-Dec-2014", 10, 1);
//Displaying the Production worker details
System.out.println("===== Production Worker Details =====");
pw1.toString();
//Creating the Production Worker Object by passing the parameters
ProductionWorker pw2=new ProductionWorker("Pitersen","345H","11-Feb-2006", 15, 2);
//Displaying the Production worker details
System.out.println("===== Production Worker Details =====");
pw2.toString();
}
}
_____________________
Output:
===== Employee Details =====
Employee Name =Johnson
Employee No =123B
Hiredate =12-Nov-2011
===== Production Worker Details =====
Employee Name =Williams
Employee No =111C
Hiredate =31-Dec-2014
Hourly Pay Rate =10.0
Shift = Day
===== Production Worker Details =====
Employee Name =Pitersen
Employee No =345H
Hiredate =11-Feb-2006
Hourly Pay Rate =15.0
Shift = Night
____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.