Question: This assignment assumes you have completed Programming Challenge 1 of Chapter 9 ( Employee and ProductionWorker Classes). Modify the Employee and ProductionWorker classes so they
This assignment assumes you have completed Programming Challenge 1 of Chapter 9 ( Employee and ProductionWorker Classes). Modify the Employee and ProductionWorker classes so they throw exceptions when the following errors occur: The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999. The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift. The ProductionWorker class should throw an exception named InvalidPayRate when it receives a negative number for the hourly pay rate. Write a test program that demonstrates how each of these exception conditions work.
Here is the code from the Chapter 9 project
class Employee {
private String Empname; private String Empnumber; private String Hiredate; //defulat constructor method public Employee() { Empname = "No name "; Empnumber = "No number "; Hiredate = "No date "; } //creates a constructor method public Employee(String Empname, String Empnumber, String Hiredate) { setName(Empname); setNumber(Empnumber); setHireDate(Hiredate); } //sets the employee name public void setName(String n) { Empname = n; } //sets the employee number public void setNumber(String num) { Empnumber = num; } //sets the hire date public void setHireDate(String h) { Hiredate = h; } //gets the employee name public String getName() { return Empname; } //gets the employee number public String getNumber() { return Empnumber; } //gets the hir date public String getHireDate() { return Hiredate; } }
class ProductionWorker extends Employee {
private int shift; private double hourpayrate; //creaets a constructor public ProductionWorker(String Empname, String Empnumber, String Hiredate, int shift, double hourpayrate) { super(Empname,Empnumber,Hiredate); setShift(shift); setHourlyPayRate(hourpayrate);
} //gets the shift public int getShift() { return shift; }
//gets the rate public double getHourlyPayRate() { return hourpayrate; }
//sets the shift public void setShift(int s) { shift = s; }
//sets the rate public void setHourlyPayRate(double r) { hourpayrate = r; } }
import java.util.Scanner;
public class ProductionWorker { public static void main(String[] args) { String name, id, date; double pay; int shift; //creates a scanner object named keyboard Scanner keyboard = new Scanner(System.in);
//asks the user for employee name System.out.println("Please enter employee's name: "); name = keyboard.nextLine();
//asks the user for the employee number System.out.println("Please enter employee's ID: "); id = keyboard.nextLine();
//asks the user for their hire date System.out.println("Please enter employee's hire date: "); date = keyboard.nextLine();
//asks the user what shift they work System.out.println("1-Day shift 2-Night shift"); System.out.println("Please enter employee's shift: "); shift = keyboard.nextInt();
//asks the user what their pay rate is System.out.println("Please enter employee's hourly pay: "); pay = keyboard.nextDouble(); ProductionWorker pw = new ProductionWorker(name,id,date,shift,pay); //tells the user the employee name they entered System.out.println("Employee Name: " + pw.getName());
//tells the user the employee number they entered System.out.println("Employee ID: " + pw.getNumber());
//tells the user the hire date they entered System.out.println("Hire Date: " + pw.getHireDate());
//tells the user the shift they entered System.out.println("Shift: " + pw.getShift());
//tells the user the employee's hourly rate they entered System.out.println("Hourly Rate: " + "$"+ pw.getHourlyPayRate()); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
