Question

In: Computer Science

This assignment assumes you have completed Programming Challenge 1 of Chapter 9 ( Employee and ProductionWorker...

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

Solutions

Expert Solution

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;

}

}

}


Related Solutions

C++ Programming Chapter 7 Assignment: Assignment #4 – Student Ranking : In this assignment you are...
C++ Programming Chapter 7 Assignment: Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display...
/** * Chapter 6 * Programming Challenge 1: Area Class * This program demonstrates the Area...
/** * Chapter 6 * Programming Challenge 1: Area Class * This program demonstrates the Area class. */ public class AreaDemo { public static void main(String[] args) { // Get the area of a circle with a radius of 20.0. System.out.println("The area of a circle with a " + "radius of 20.0 is " + Area.getArea(20.0)); // Get the area of a rectangle with a length of 10 // and a width of 20. System.out.println("The area of a rectangle with...
In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts...
In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts a Celsius temperature to Fahrenheit. Modify that program so it uses a loop to display a table of the Celsius temperatures 0–20, and their Fahrenheit equivalents. Display table on screen and it a .txt file. c++
Assignment 2 - Employee Hierarchy In Chapter 9, we created the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model...
Assignment 2 - Employee Hierarchy In Chapter 9, we created the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model the relationship between two types of employees and how to calculate the earnings for each. Another way to look at the problem is that CommissionEmployees and BasePlusCommissionEmployees are each Employees and that each has a different CompensationModel object. A CompensationModel would provide an earnings method. Classes or subclasses of CompensationModel would contain the details of a particular Employee's compensation: CommissionCompensationModel - For Employees who...
Continuing Payroll Problem, 6B: Chapter 6 You have almost completed the Olney Company's Employee Payroll Register...
Continuing Payroll Problem, 6B: Chapter 6 You have almost completed the Olney Company's Employee Payroll Register for the pay period ending January 8, 20--. In this last task, the following steps will be completed. Requirements: Record the deduction for group insurance. Record the health insurance deduction. Record the check number assigned to each employee. Compute and record the net pay for each employee. Total the input columns on the Employee Payroll register. On Employer Register, enter the total gross earnings....
Continuing Payroll Problem, 6B, Chapter 6 You have almost completed the Kipley's Company Employee Payroll Register...
Continuing Payroll Problem, 6B, Chapter 6 You have almost completed the Kipley's Company Employee Payroll Register for the pay period ending January 8, 20XX. Requirements: Record the deduction for group insurance. Record the health insurance deduction. Record the check number assigned to each employee. Compute and record the net pay for each employee. Total the input columns on the Employee Payroll register. On Employer Register, enter total gross earnings. Prepare the journal entries as of January 12 to record the...
BEFORE YOU START: Before working on this assignment you should have completed Assignment 1. PROGRAM STATEMENT...
BEFORE YOU START: Before working on this assignment you should have completed Assignment 1. PROGRAM STATEMENT AND REQUIREMENTS: You will implement in Java the WIFI troubleshooting program you created in Assignment 1 with the following additional conditions: At the end of the program, you will ask if the client wants to purchase a regular router for $50 or a super high-speed router for $200, adding the cost to the total amount the client needs to pay. Once your program calculates...
Answer Critical Thinking Challenge #4 at the end of Chapter 9. Compare the program you create...
Answer Critical Thinking Challenge #4 at the end of Chapter 9. Compare the program you create to your current employer or a former employer’s management development program. If your program is better, tell me why you feel that way. If not, what ways can you improve yours?
You have completed the Cybersecure Contingency Planning Challenge. Do you have a contingency plan where you...
You have completed the Cybersecure Contingency Planning Challenge. Do you have a contingency plan where you are practicing? (Minimum 1 word, maximum 500 words. Once submitted, your response cannot be edited. This response will be posted on a public discussion wall for all your classmates to view.)
Chapter 5 Assignment 5-1 Below you will find descriptions of nine (9) transactions. On the excel...
Chapter 5 Assignment 5-1 Below you will find descriptions of nine (9) transactions. On the excel attachment, complete the journal entries by typing in your answers in the boxes provided for the following merchandising transactions for both the PERPETUAL and PERIODIC inventory systems. You are asked to forward you completed excel assignment answer back to me using C4. If that is not possible, print out the excel page and complete the answer by hand, and then either scan it back...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT