JAVA from control structures through objects 6th edition tony gaddis java progra
ID: 3836708 • Letter: J
Question
JAVA from control structures through objects 6th edition tony gaddis java programming Question exception project this assighment assumes you have completed programming challenges 1 of chapter 10 (emplyee 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 InvalidEmployyNumber when it recieves an invalid employee number
****the ProductionWorker class should throw an exception named InvalidShift when it recieves an invalid shift
****The productionWorker class should throw an exception named InvalidPayRate when it recieves a negative number for the hourly pay rate.
Part 2 Section D (this part is worth 60 points) 12/13/16 Page 1 Review the code below, which is a programming challenge from one of the chapters (Employee and Production Worker Classes). Modify the Employee and errors occur: ProductionWorker classes so they throw exceptions when the following The Employee class should throw an exception named InvalidEmployeeNumber when it receives an invalid employee number. The class should throw an exception named Invalid Shift when receives an invalid shift. The Production Worker class should throw an exception named InvalidPayRate when it receives a negative number for the hourly pay rate. You do not have to compile the code, but it should be syntactically correct. (Note: I do not want pseudo-code, but a Java implementation.) Please put your hand-est You (clean sheet(sh with and include the exam isroem your answer is. If there will need to label this so Lknow exactly code below direstlya on the exam, you can also include your answer in the import java text. Decimal Format ProductionWorker cians public class production Worker extends Employee Constant for the day and night shifts final int DAY SHIFT 2 public int NIGHT SHIFT static The employee' pay rate int shift Rate name private ith a constructor initializes an obs eet pay a employee The employee' s name s number date num The employee s The aramExplanation / Answer
Please find my implementation.
######### Exception Classes #########
class Invalidpayrate extends Exception {
public Invalidpayrate(String message){
super(message);
}
}
class InvalidEmployeeNumber extends Exception{
public InvalidEmployeeNumber(String message) {
super(message);
}
}
class InvalidShift extends Exception{
public InvalidShift(String message) {
super(message);
}
}
########### Employee.java #########
public class Employee
{
private String Empname;
private String Empnumber;
private String Hiredate;
public Employee()
{
Empname="";
Empnumber="";
Hiredate="";
}
public Employee (String Empname, String Empnumber,String Hiredate) throws InvalidEmployeeNumber
{
setName(Empname);
setNumber(Empnumber);
setHiredate(Hiredate);
}
public void setName(String n) throws InvalidEmployeeNumber
{
if( n == null || n.trim().equals(""))
throw new InvalidEmployeeNumber("Employee name can not be null or empty");
Empname = n;
}
public void setNumber(String num)
{
Empnumber = num;
}
public void setHireDate(String h)
{
Hiredate = h;
}
public String getName()
{
return Empname;
}
public String getNumber()
{
return Empnumber;
}
public String getHireDate()
{
return Hiredate;
}
private void setHiredate(String Hiredate) {
this.Hiredate = Hiredate;
}
}
############ ProductionWorker.java ###########
public class ProductionWorker extends Employee
{
private int shift; // The employee's shift
private double hourpayrate; // The employee's pay rate
public ProductionWorker(String Empname, String Empnumber, String Hiredate,
int shift, double hourpayrate) throws InvalidEmployeeNumber, InvalidShift, Invalidpayrate
{
super(Empname, Empnumber, Hiredate);
setShift(shift);
setHourlyPayRate(hourpayrate);
}
public int getShift()
{
return shift;
}
public double getHourlyPayRate()
{
return hourpayrate;
}
public void setShift(int s) throws InvalidShift
{
if(s < 0){
throw new InvalidShift("Shift can not negative");
}
shift = s;
}
/**
The setPayRate method sets the employee's pay rate.
@param p The employee's pay rate.
*/
public void setHourlyPayRate(double r) throws Invalidpayrate
{
if(r < 0)
throw new Invalidpayrate("Pay rate can not be negative");
hourpayrate = r;
}
}
################ ProductionWorkerDemo.java ########
import java.util.*;
public class ProductionWorkerDemo
{
public static void main(String[] args)
{
String name,id,date;
int shift;
double pay;
// Creates Scanner object
Scanner keyboard = new Scanner(System.in);
// Gets the user's name.
System.out.println("Enter employee name: ");
name = keyboard.nextLine();
// Gets the user's employee number.
System.out.println("Enter employee ID: ");
id = keyboard.nextLine();
// Gets the user's hire date.
System.out.println("Enter employee date ");
date = keyboard.nextLine();
System.out.println("1-day Shift/n2-Night shift");
System.out.println("Enter employee shift: ");
shift = keyboard.nextInt();
System.out.println("Enter hourly pay");
pay = keyboard.nextDouble();
try{
// Creates an Production worker object.
ProductionWorker pw = new ProductionWorker(name,id,date,shift,pay);
System.out.println();
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());
}catch(InvalidEmployeeNumber e){
System.out.println(e.getMessage());
}catch(Invalidpayrate e){
System.out.println(e.getMessage());
}catch(InvalidShift e){
System.out.println(e.getMessage());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.