In: Computer Science
Java
Modify subclass HourlyEmployee11 and class HourlyExc (created to hold the exception) to add a "try and catch" statement to catch an exception if "empStatus == 1" hourly wage is not between $15.00/hr. and $25.00/hr. The keywords “throw” and “throws” MUST be used correctly and both keywords can be used either in a constructor or in a method. If an exception is thrown, the program code should prompt the user for the valid input and read it in.
*NOTE*- I have the code for the rest of the program all I need is what the instructions indicate. Thank you.
HourlyEmployee11 source code:
public class HourlyEmployee11 extends Employee11 implements
PartTimeEmployee11 {
//creating scanner object to read input
Scanner sc = new Scanner(System.in);
//declaring instance variables
private int hrsWorked, otHrs, empStatus;
private double hrlyRate, regularPay, otPay, wklyPaycheck;
//constructor to set instance variables
public HourlyEmployee11(String firstName, String lastName, int
hrsWorked,
int empStatus, int otHrs, double hrlyRate, double regularPay,
double otPay) {
//calling super class constructor
super (firstName, lastName);
if (empStatus == 1) {
System.out.println("Enter the employee's hourly rate of
pay:");
hrlyRate = sc.nextDouble();
System.out.println("Enter the number of hours worked by the
employee:");
hrsWorked = sc.nextInt();
if (hrsWorked > 40) {
otHrs = hrsWorked - 40;
otPay = ((hrlyRate * 1.5) * otHrs);
regularPay = hrlyRate * 40;
}
else {
otPay = 0;
regularPay = hrlyRate * hrsWorked;
}
}
else if (empStatus == 2) {
otPay = 0;
wklyPaycheck = HRLY_WKLYPAY;
}
this.hrsWorked = hrsWorked;
this.otHrs = otHrs;
this.empStatus = empStatus;
this.hrlyRate = hrlyRate;
this.regularPay = regularPay;
this.otPay = otPay;
WklyPaycheck();
counter++;
}
//method that calculates the weekly paycheck
public void WklyPaycheck() {
if (empStatus == 1) {
wklyPaycheck = regularPay + otPay;
}
}
//getter(method) that retrieves the "wklyPaycheck"
public double getWklyPaycheck() {
return wklyPaycheck;
}
public void payPrint() {
System.out.printf("%s The employee is an Hourly Employee and its
weekly "
+ "paycheck amount is $%.2f.\n", super.toString(),
wklyPaycheck);
}
}
HourlyExc source code:
package employeetest11;
import java.util.InputMismatchException;
public class HourlyExc extends Exception {
//modify
}
// HourlyExc.java
import java.util.InputMismatchException;
public class HourlyExc extends Exception {
public HourlyExc()
{
super();
}
public HourlyExc(String message)
{
super(message);
}
}
// end of HourlyExc.java
//HourlyEmployee11.java
public class HourlyEmployee11 extends Employee11 implements
PartTimeEmployee11 {
//creating scanner object to read input
Scanner sc = new Scanner(System.in);
//declaring instance variables
private int hrsWorked, otHrs, empStatus;
private double hrlyRate, regularPay, otPay,
wklyPaycheck;
//constructor to set instance variables
public HourlyEmployee11(String firstName, String
lastName, int hrsWorked,
int empStatus, int otHrs, double hrlyRate, double
regularPay, double otPay) {
//calling super class
constructor
super (firstName, lastName);
if (empStatus == 1) {
// loop that
continues until user enters a valid hourly wage
while(true)
{
System.out.println("Enter the employee's hourly
rate of pay:");
try{
// input the hourly
wage
hrlyRate =
sc.nextDouble();
// if hourly wage is invalid,
throw HourlyExc exception
if(hrlyRate < 15 ||
hrlyRate > 25)
throw new
HourlyExc("Invalid hourly wage");
else // valid hourly wage
exit the loop
break;
}catch(HourlyExc e)
{
sc.nextLine(); // discard the
entire line
System.out.println("Hourly
wage for empStatus 1 must be between $15.00/hr. and
$25.00/hr.");
}
}
System.out.println("Enter the number of hours worked by the
employee:");
hrsWorked =
sc.nextInt();
if (hrsWorked
> 40) {
otHrs = hrsWorked - 40;
otPay = ((hrlyRate * 1.5) * otHrs);
regularPay = hrlyRate * 40;
}
else {
otPay = 0;
regularPay = hrlyRate * hrsWorked;
}
}
else if (empStatus == 2) {
otPay = 0;
wklyPaycheck =
HRLY_WKLYPAY;
}
this.hrsWorked = hrsWorked;
this.otHrs = otHrs;
this.empStatus = empStatus;
this.hrlyRate = hrlyRate;
this.regularPay = regularPay;
this.otPay = otPay;
WklyPaycheck();
counter++;
}
//method that calculates the weekly paycheck
public void WklyPaycheck() {
if (empStatus == 1) {
wklyPaycheck =
regularPay + otPay;
}
}
//getter(method) that retrieves the
"wklyPaycheck"
public double getWklyPaycheck() {
return wklyPaycheck;
}
public void payPrint() {
System.out.printf("%s The employee
is an Hourly Employee and its weekly "
+ "paycheck amount is $%.2f.\n",
super.toString(), wklyPaycheck);
}
}
// end of HourlyEmployee11.java