Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Employer and production worker. Design a class named Employer. The class should

ID: 3673764 • Letter: E

Question

Employer and production worker.

Design a class named Employer. 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 withim the range A-M

.Hire date

write one or more constructor and the appropriate accessor and mutator methodes for the class.

Next, write a class name ProductionWorker that inherits from the employer class. The production Worker class should have field to hold the following informations:

.Shift (an integer)

.Hourly pay rate (a double)

The work day is dividedeinto two shifts: day and night. The shift field will be an integer value representing the shift that the employer works. The day shift is shift1 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.

Explanation / Answer

ProductionWorkerMain.java


import java.text.DecimalFormat;
import java.util.Scanner;

class Emp {

public String nm;//empname
public String num;//empno
public String hireDt;//hiredt
private boolean validEmpNumber = false;
//constructor method

Emp(String n, String num, String hd) {
nm = n;
num = num;
hireDt = hd;
}

public String getName() {
return nm;
}

public String getNumber() {
while (validEmpNumber = false) {
if (num.length() != 5) {
System.out.println("Invalid employee number, please try again.");
} else if (((!Character.isDigit(num.charAt(0))) || ((!Character.isDigit(num.charAt(1))))
|| ((!Character.isDigit(num.charAt(2)))) || (num.charAt(3) != '-')
|| ((!Character.isDigit(num.charAt(4))))
|| (!(num.charAt(4) >= 'A' && num.charAt(4) <= 'M')))) {
System.out.println("Invalid employee number, please try again.");
System.out.println("Enter your employee number: ");
} else {
validEmpNumber = true;
}
}
return num;
}

public String getHireDate() {
return hireDt;
}

// * Checking validity of employee number
// *
public boolean isValidEmployeeNumber(String number) {
boolean status = true;
if (number.length() != 5) {
System.out.println("Invalid employee number.");
} else {
if (((!Character.isDigit(number.charAt(0))) || ((!Character.isDigit(number.charAt(1))))
|| ((!Character.isDigit(number.charAt(2)))) || (number.charAt(3) != '-')
|| ((!Character.isDigit(number.charAt(4))))
|| (!(number.charAt(4) >= 'A' && number.charAt(4) <= 'M')))) {
status = false;
}
}
return status;
}

public String toString() {
String str = "Name: " + nm + " Employee Number: ";
if (num == "") {
str += "Invalid Employee Number";
} else {
str += num;
}
str += (" Hire Date: " + hireDt);
return str;
}
}

class ProductionWorker extends Emp //productioworker is the subclass of emp
{

public static int dayShift = 1;
public static int nightShift = 2;
private int shift;
private double payRate;
private boolean rightShift = false;

public String getShift() //EDIT#2: FIXED (this will be good enough for me!)
{
String shiftTime;

if (shift == 1) {
shiftTime = "Day";
} else if (shift == 2) {
shiftTime = "Night";
} else if (shift == 3) {
shiftTime = "Alternating";
} else {
shiftTime = "On Call";
}

return shiftTime;
}

public void setShift(int s) {
shift = s;
}

public double getPayRate() {
return payRate;
}

public void setPayRate(double rate) {
payRate = rate;
}

/**
* Constructor initializes a name, number and hire date
*/
public ProductionWorker(//String name, String number, String hireDate,
String n, String num, String hd, int sh, double rate) {

super(n, num, hd);
shift = sh;
payRate = rate;
}

public ProductionWorker(String n, String num, String hd) {
super(n, num, hd);
shift = dayShift;
payRate = 0.0;
}

public String toString() {
DecimalFormat dollar = new DecimalFormat("#,##0.00");

String str = super.toString();

str += " Shift: ";
if (shift == dayShift) {
str += "Day";
} else if (shift == nightShift) {
str += "Nights";
} else {
str += "Invalid Shift Number";
}

str += (" Hourly Pay Rate: $"
+ dollar.format(payRate));

return str;
}
}

public class ProductionWorkerMain {

public static void main(String[] args) {//driver program
String nm;
String n;
String numb;
String num;
String hireDate;
String hd;
int shift;
double payRate;
double rate;
Scanner s = new Scanner(System.in);
System.out.println("Enter your name: ");
nm = s.nextLine();
System.out.println("Enter your employee number: ");
System.out.println("(Format: NNN-L)");
numb = s.nextLine();
System.out.println("Enter your hire date: ");
hireDate = s.nextLine();
System.out.println("Enter your payrate: ");
payRate = s.nextDouble();
System.out.println("Enter your shift (day=1, night=2): ");
shift = s.nextInt();
//Production worker object
ProductionWorker ProductionWorker = new ProductionWorker(nm, numb,
hireDate, shift, payRate);

System.out.println("---------- Employee Information -----------");
System.out.println("Name: " + ProductionWorker.getName());
System.out.println("Employee Number: " + ProductionWorker.getNumber());
System.out.println("Hire Date: " + ProductionWorker.getHireDate());
System.out.println("Pay Rate: " + ProductionWorker.getPayRate());
System.out.println("Shift: " + ProductionWorker.getShift());
}
}

output

---------- Employee Information -----------
Name: mike
Employee Number: null
Hire Date: 11-may-2010
Pay Rate: 67000.0
Shift: Night

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote