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

Modify the Employee and ProductionWorker classes so they throw exceptions when t

ID: 3804387 • Letter: M

Question

Modify the Employee and ProductionWorker classes so they throw exceptions when the following errors occur: middot The Employee class should throw an exception named InvalidEmployeeNumber when it receives an invalid employee number middot The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid Shift middot The ProductionWorker class should throw an exception named invalidePayRate when it receives a negative number for the hourly pay rate. Write a test program that demonstrates how each of these exception conditions works. ________ The Employee and productionWorker classes from HW #2 _______ Employee and ProductionWorkder Classes Design a class named Employee. The class should keep the following information in fields: middot Employee name middot Employee number middot Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that extends the Employee class. The ProductionWorker class should have fields to hold the following formation: middot Shift (an integer) middot Hourly pay rate (a double) 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. Write one or more constructors and the appropriate accessor and mutator methods for the class.

Explanation / Answer

Employee.java:

public class Employee {
   private String name;
   private String employeeNumber;
   private String hireDate;

   public Employee(String n, String num, String date) throws InvalidEmployeeNumberException {
       name = n;
       setEmployeeNumber(num);
       hireDate = date;
   }

   public Employee() {
       name = "";
       employeeNumber = "";
       hireDate = "";
   }

   public void setName(String n) {
       name = n;
   }

   public void setEmployeeNumber(String e) throws InvalidEmployeeNumberException {
       if (isValidEmpNum(e))
           employeeNumber = e;
       else
           throw new InvalidEmployeeNumberException("Invalid Employee Number. Should be number of 5 digits");
   }

   public String getHireDate() {
       return hireDate;
   }

   public void setHireDate(String hireDate) {
       this.hireDate = hireDate;
   }

   // if a valid integer of 5 digits
   private boolean isValidEmpNum(String e) {      
       if (e.length() != 5)
           return false;
      
       try {
           Integer.parseInt(e);
       } catch (NumberFormatException e1) {
           return false;
       }
          
       return true;
   }

   public String toString() {
       String str = "Name: " + name + " Employee Number: " + employeeNumber;
       str += (" Hire Date: " + hireDate);
       return str;
   }
}


ProductionWorker.java:

import java.text.DecimalFormat;


public class ProductionWorker extends Employee {
   public static final int DAY_SHIFT = 1;
   public static final int NIGHT_SHIFT = 2;
   private int shift;
   private double payRate;

   public ProductionWorker(String n, String num, String date, int sh,
           double rate) throws InvalidEmployeeNumberException, InvalidShiftException, InvalidPayrateException {
       super(n, num, date);
       setShift(sh);
       setPayRate(rate);
   }

   public ProductionWorker() {
       super();
       shift = DAY_SHIFT;
       payRate = 0.0;
   }

   public void setShift(int s) throws InvalidShiftException {
       if(s != DAY_SHIFT && s != NIGHT_SHIFT)
           throw new InvalidShiftException("Invalid Shift. Should be 1=DayShift or 2=NightShift only");
       shift = s;
   }

   public void setPayRate(double p) throws InvalidPayrateException {
       if(p < 0)
           throw new InvalidPayrateException("invalid Payrate. Can't be negative.");
       payRate = p;
   }

   public int getShift() {
       return shift;
   }

   public double getPayRate() {
       return payRate;
   }

   public String toString() {
       String str = super.toString();
       DecimalFormat d = new DecimalFormat("###.00");
       str += " Shift: ";
       if (shift == DAY_SHIFT)
           str += "Day";
       else if (shift == NIGHT_SHIFT)
           str += "Night";
       str += " Pay rate: " + d.format(payRate);
       return str;
   }
}


InvalidEmployeeNumberException.java:
public class InvalidEmployeeNumberException extends Exception {
   InvalidEmployeeNumberException(String message) {
       super(message);
   }
}


InvalidShiftException.java:

public class InvalidShiftException extends Exception {
   InvalidShiftException(String message) {
       super(message);
   }
}



InvalidPayrateException.java:

public class InvalidPayrateException extends Exception {
   InvalidPayrateException(String message) {
       super(message);
   }
}


WorkerDemo.java:

public class WorkerDemo {
   public static void main(String[] args) {
       ProductionWorker pw = null;

       try {
           pw = new ProductionWorker("John Smith", "123", "11-15-2005",
                   ProductionWorker.DAY_SHIFT, 16.50);
       } catch (Exception e) {
           System.out.println(e.getMessage());
       }

       try {
           pw = new ProductionWorker("John Smith", "12334", "11-15-2005",
                   6, 16.50);
       } catch (Exception e) {
           System.out.println(e.getMessage());
       }
      
       try {
           pw = new ProductionWorker("John Smith", "13423", "11-15-2005",
                   ProductionWorker.DAY_SHIFT, -16.50);
       } catch (Exception e) {
           System.out.println(e.getMessage());
       }
      
       System.out.println(" Trying with valid Values");
       try {
           pw = new ProductionWorker("John Smith", "12133", "11-15-2005",
                   ProductionWorker.DAY_SHIFT, 16.50);
           System.out.println("Here's the first production worker. ");
           System.out.println(pw);
       } catch (Exception e) {
           e.getMessage();
       }
   }
}



Sample Output:

Invalid Employee Number. Should be number of 5 digits
Invalid Shift. Should be 1=DayShift or 2=NightShift only
invalid Payrate. Can't be negative.

Trying with valid Values
Here's the first production worker.
Name: John Smith
Employee Number: 12133
Hire Date: 11-15-2005
Shift: Day
Pay rate: 16.50

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote