Write an Employee class that keeps data attributes for the following pieces of i
ID: 3890871 • Letter: W
Question
Write an Employee class that keeps data attributes for the following pieces of information:
Employee Name
Employee Number
Next, write a class named ProductionWorker that is a subclass of the Employee class.
The ProductionWorker class should keep data attributes for the following information:
Shift Number (an integer 1 or 2)
Hourly Pay Rate
The workday is divided into two shifts: day and night. The shift attribute will hold an integer value representing the shift that the employee works. The day shift is shift 1 and the night shift is shift 2. Write the appropriate accessor and mutator methods for each class.
Once you have written the classes, write a program that creates an object of the ProductionWorker class and prompts the user to enter data for each of the object’s data attributes. Store the data in the object and then use the object’s accessor methods to retrieve it and display it on the screen.
Add an __str__ method to the ProductionWorker class that will print the attributes from both classes in a readable format. In the main part of the program, create one more ProductionWorker object, assign values to the attributes and print both objects using the print statement.
Explanation / Answer
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,
double hourly_pay_rate, int shift) {
super(employee_name, employee_no);
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.
public void __str__() {
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");
}
}
___________________
Employee.java
public class Employee {
private String employee_name;
private String employee_no;
// Default Constructor
public Employee() {
}
// Parameterized Constructor
public Employee(String employee_name, String employee_no) {
this.employee_name = employee_name;
this.employee_no = employee_no;
}
// 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;
}
// 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);
return "";
}
}
__________________
Driver.java
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
//Declaring variables
String name, empNo;
int shift;
double payRate;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
System.out.print("Enter Employee No :");
empNo = sc.next();
System.out.print("Enter name :");
name = sc.next();
System.out.print("Enter Shift(1 (or) 2) :");
shift = sc.nextInt();
System.out.print("Enter Payrate :$");
payRate = sc.nextDouble();
Employee emp = new Employee(name, empNo);
//Creating the Production Worker Object by passing the parameters
ProductionWorker pw1 = new ProductionWorker(name, empNo, payRate, shift);
System.out.println(" ---Displaying the Production worker details---");
//Displaying the Production worker details
System.out.println("Employee Name :" + pw1.getEmployee_name());
System.out.println("Employee Number :" + pw1.getEmployee_no());
if (pw1.getShift() == 1)
System.out.println("Shift : Day");
else if (pw1.getShift() == 2)
System.out.println("Shift : Night");
System.out.println("Hourly Pay Rate :$" + pw1.getHourly_pay_rate());
System.out.println("---- Getting Production Worker details From user ---");
System.out.print("Enter Employee No :");
empNo = sc.next();
System.out.print("Enter name :");
name = sc.next();
System.out.print("Enter Shift(1 (or) 2) :");
shift = sc.nextInt();
System.out.print("Enter Payrate :$");
payRate = sc.nextDouble();
//Creating the Production Worker Object by passing the parameters
ProductionWorker pw2 = new ProductionWorker(name, empNo, payRate, shift);
System.out.println(" ---Displaying the Production worker details---");
System.out.println("===== Production Worker Details =====");
pw1.__str__();
}
}
_________________
Output:
Enter Employee No :123-AH
Enter name :Williams
Enter Shift(1 (or) 2) :1
Enter Payrate :$12
---Displaying the Production worker details---
Employee Name :Williams
Employee Number :123-AH
Shift : Day
Hourly Pay Rate :$12.0
---- Getting Production Worker details From user ---
Enter Employee No :234-BH
Enter name :Johnson
Enter Shift(1 (or) 2) :2
Enter Payrate :$15
---Displaying the Production worker details---
===== Production Worker Details =====
Employee Name =Williams
Employee No =123-AH
Hourly Pay Rate =$12.0
Shift = Day
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.