10. Exception Project This assignment assumes you have completed Programming Cha
ID: 3640880 • Letter: 1
Question
10. Exception Project
This assignment assumes you have completed Programming Challenge 1 of Chapter 11 (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 works.
Explanation / Answer
There are four 5 classes. Employee class, Production worker class a child class of employee , Identify exception class - process the inputs and throw exception accordingly.
It is assumed "Noon" is an invalid shift, since nothing told in particular about that.
InvalidEmployeeNumberException class - an exception class which has a constructor that calls super constructor.
InvalidPayRate class - - an exception class which has a constructor that calls super constructor.
Following are the Classes
package exception;
public class Employee {
private int employeeNumber;
public int getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(int employeeNumber) {
this.employeeNumber = employeeNumber;
}
}
package exception;
public class ProductionWorker extends Employee {
private String shift;
private double rate;
public String getShift() {
return shift;
}
public void setShift(String shift) {
this.shift = shift;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
}
package exception;
public class InvalidEmployeeNumberException extends Exception {
public InvalidEmployeeNumberException(String message) {
super(message);
}
}
package exception;
public class InvalidPayRate extends Exception {
public InvalidPayRate(String message) {
super(message);
}
}
package exception;
import java.util.Scanner;
public class IdentifyException {
public static void main(String[] args) {
try {
ProductionWorker e = new ProductionWorker();
Scanner sc = new Scanner(System.in);
System.out.println("Please enter Employee Number");
e.setEmployeeNumber(sc.nextInt());
if (e.getEmployeeNumber() == 9999) {
throw new InvalidEmployeeNumberException(
"Employee Number Entered is Invalid");
}
System.out.println("Please enter a Shift");
String shift = sc.next();
e.setShift(shift);
if (shift.equalsIgnoreCase("Noon")) {
throw new Exception("Invalid Shift entered");
}
System.out.println("Please enter a Pay rate");
e.setRate(sc.nextDouble());
if (e.getRate() < 0) {
throw new InvalidPayRate("Invalid Pay rate entered");
}
} catch (Exception e) {
System.out.println("Exception occurred " + e.getMessage());
}
}
}
Hope it helps, Please rate.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.