Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 I have so far:

import java.util.Scanner;

public class Employee { private int employeeNumber;

public int getEmployeeNumber() { return employeeNumber; }

public void setEmployeeNumber(int employeeNumber) { this.employeeNumber = employeeNumber; } }

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

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

public class InvalidPayRate extends Exception {

public InvalidPayRate(String message) { super(message);

}

}

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

import java.util.Scanner; /** * * @author Michael */ public class EmployeeDemo { /** * @param args the command line arguments */ public static void main(String[] args) { String name, id, date; double pay; int shift; //Scanner Scanner keyboard = new Scanner(System.in); //User Input System.out.println("Please enter employee's name: "); name = keyboard.nextLine(); //input ID System.out.println("Please enter employee's ID: "); id = keyboard.nextLine(); //input date System.out.println("Please enter employee's hire date: "); date = keyboard.nextLine(); //input shift System.out.println("1-Day shift 2-Night shift"); System.out.println("Please enter employee's shift: "); shift = keyboard.nextInt(); //input pay System.out.println("Please enter employee's hourly pay: "); pay = keyboard.nextDouble(); //Call ProductionWorker class ProductionWorker pw = new ProductionWorker(name,id,date,shift,pay); //Output System.out.println("Employee Name: " + pw.getName()); System.out.println("Employee ID: " + pw.getNumber()); System.out.println("Hire Date: " + pw.getHireDate()); System.out.println("Shift: " + pw.getShift()); System.out.println("Hourly Rate: " + pw.getHourlyPayRate()); } }

class Employee { //fields private String Empname; private String Empnumber; private String Hiredate; //constructor (default) public Employee() { Empname = " "; Empnumber = " "; Hiredate = " "; } //constructor (parameterized) public Employee(String Empname, String Empnumber, String Hiredate) { setName(Empname); setNumber(Empnumber); setHireDate(Hiredate); } //Mutator (employee name) public void setName(String n) { Empname = n; } //Mutator (employee number) public void setNumber(String num) { Empnumber = num; } //Mutator (hire date) public void setHireDate(String h) { Hiredate = h; } //Accessor (employee name) public String getName() { return Empname; } //Accessor (employee number) public String getNumber() { return Empnumber; } //Accessor(hire date) public String getHireDate() { return Hiredate; } }

/** * * @author Michael */ class ProductionWorker extends Employee { //fields private int shift; private double hourpayrate; //constructor public ProductionWorker(String Empname, String Empnumber, String Hiredate, int shift, double hourpayrate) { super(Empname,Empnumber,Hiredate); setShift(shift); setHourlyPayRate(hourpayrate); } //accessor public int getShift() { return shift; } public double getHourlyPayRate() { return hourpayrate; } //mutator public void setShift(int s) { shift = s; } public void setHourlyPayRate(double r) { hourpayrate = r; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 11 The Ultimate Database For Cloud Analytics And Mobile

Authors: John Campbell, Chris Crone, Gareth Jones, Surekha Parekh, Jay Yothers

1st Edition

ISBN: 1583474013, 978-1583474013

More Books

Students also viewed these Databases questions