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

This assignment assumes you have completed Programming Challenge 1 of Chapter 9

ID: 3797783 • Letter: T

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 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());
}
}

Explanation / Answer

import java.util.Date;

import java.io.*;

public class InvalidPayRate extends Exception {

public InvalidPayRate(){

super("Error: Invalid Pay Rate.");

}

}

void setHourlyPayRate(double rate) throws InvalidPayRate

{

if (rate < 0.0){

throw new InvalidPayRate();

}

this.hourlyPayRate = rate;

}

public class ProductionWorker extends Employee {

private int shift;

private double hourlyPay;

ProductionWorker() {

}

ProductionWorker(int shift, double hourlyPay, String name, String empNum, Date hireDate) throws InvalidPayRate, InvalidShift, InvalidEmployeeNumber {

// Throws exception if the hourly rate is below zero.

if (hourlyPay < 0) {

throw new InvalidPayRate();

// Throws an exception if the shift is not a 1 or a two.

} else if (shift < 1 || shift > 2) {

throw new InvalidShift();

} else {

this.setName(name);

this.setEmpNumber(empNum);

this.setHireDate(hireDate);

this.shift = shift;

this.hourlyPay = hourlyPay;

}

}

public int getShift() {

return shift;

}

public void setShift(int shift) throws InvalidShift {

if (shift < 1 || shift > 2) {

throw new InvalidShift();

} else {

this.shift = shift;

}

}

public double getHourlyPay() {

return hourlyPay;

}

public void setHourlyPay(double hourlyPay) throws InvalidPayRate {

if (hourlyPay < 0) {

throw new InvalidPayRate();

}else{

this.hourlyPay = hourlyPay;

}

}

}