Design a class named Employee. The class should keep the following information i
ID: 3803606 • Letter: D
Question
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.
Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
############################# Employee.java #############
public class Employee
{
private String Empname;
private String Empnumber;
private String Hiredate;
public Employee()
{
Empname="";
Empnumber="";
Hiredate="";
}
public Employee (String Empname, String Empnumber,String Hiredate) throws InvalidEmployeeNumber
{
setName(Empname);
setNumber(Empnumber);
setHiredate(Hiredate);
}
public void setName(String n) throws InvalidEmployeeNumber
{
if( n == null || n.trim().equals(""))
throw new InvalidEmployeeNumber("Employee name can not be null or empty");
Empname = n;
}
public void setNumber(String num)
{
Empnumber = num;
}
public void setHireDate(String h)
{
Hiredate = h;
}
public String getName()
{
return Empname;
}
public String getNumber()
{
return Empnumber;
}
public String getHireDate()
{
return Hiredate;
}
private void setHiredate(String Hiredate) {
this.Hiredate = Hiredate;
}
}
############### ProductionWorker.java ################
public class ProductionWorker extends Employee
{
private int shift; // The employee's shift
private double hourpayrate; // The employee's pay rate
public ProductionWorker(String Empname, String Empnumber, String Hiredate,
int shift, double hourpayrate) throws InvalidEmployeeNumber, InvalidShift, Invalidpayrate
{
super(Empname, Empnumber, Hiredate);
setShift(shift);
setHourlyPayRate(hourpayrate);
}
public int getShift()
{
return shift;
}
public double getHourlyPayRate()
{
return hourpayrate;
}
public void setShift(int s) throws InvalidShift
{
if(s < 0){
throw new InvalidShift("Shift can not negative");
}
shift = s;
}
/**
The setPayRate method sets the employee's pay rate.
@param p The employee's pay rate.
*/
public void setHourlyPayRate(double r) throws Invalidpayrate
{
if(r < 0)
throw new Invalidpayrate("Pay rate can not be negative");
hourpayrate = r;
}
}
################ Exception classes #########
class Invalidpayrate extends Exception {
public Invalidpayrate(String message){
super(message);
}
}
class InvalidEmployeeNumber extends Exception{
public InvalidEmployeeNumber(String message) {
super(message);
}
}
class InvalidShift extends Exception{
public InvalidShift(String message) {
super(message);
}
}
################# ProductionWorkerDemo.java #################
import java.util.*;
public class ProductionWorkerDemo
{
public static void main(String[] args)
{
String name,id,date;
int shift;
double pay;
// Creates Scanner object
Scanner keyboard = new Scanner(System.in);
// Gets the user's name.
System.out.println("Enter employee name: ");
name = keyboard.nextLine();
// Gets the user's employee number.
System.out.println("Enter employee ID: ");
id = keyboard.nextLine();
// Gets the user's hire date.
System.out.println("Enter employee date ");
date = keyboard.nextLine();
System.out.println("1-day Shift/n2-Night shift");
System.out.println("Enter employee shift: ");
shift = keyboard.nextInt();
System.out.println("Enter hourly pay");
pay = keyboard.nextDouble();
try{
// Creates an Production worker object.
ProductionWorker pw = new ProductionWorker(name,id,date,shift,pay);
System.out.println();
System.out.println("Employee Name: " + pw.getName());
System.out.println("Employee ID: " + pw.getNumber());
System.out.println("Hire Date: " + pw.getHireDate());
System.out.println("Shift: " + pw.getShift());
System.out.println("Hourly Rate: " + pw.getHourlyPayRate());
}catch(InvalidEmployeeNumber e){
System.out.println(e.getMessage());
}catch(Invalidpayrate e){
System.out.println(e.getMessage());
}catch(InvalidShift e){
System.out.println(e.getMessage());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.