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

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++
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....
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...
1. Chapter 5, Programming Challenge #8, Conversion Program (page 314). Program Name: FinalExamConversion. Write a program...
1. Chapter 5, Programming Challenge #8, Conversion Program (page 314). Program Name: FinalExamConversion. Write a program that asks the user to enter a distance in meters. The program will then present the following menu of selection: 1. Convert to Kilometers 2. Convert to Inches 3. Convert to Feet 4. Quit the Program The program will convert the distance to kilometers, inches or feet, depending on the user’s selection. Write the following methods: • getInput: This method prompts user to enter...
Assignment 3 - Enhanced Employee Hierarchy For this assignment, you are going to enhance the Employee...
Assignment 3 - Enhanced Employee Hierarchy For this assignment, you are going to enhance the Employee Hierarchy that you created in Java in Programming Assignment 2 by adding an interface called Compensation with the following two methods: earnings() - receives no parameters and returns a double. raise(double percent) - receives one parameter which is the percentage of the raise and returns a void. Create the abstract class CompensationModel which implements the Compensation interface. Create the following classes as subclasses of...
Programming assignment 9 Write a function sortWords(array) that does the following: 1. Takes as an input...
Programming assignment 9 Write a function sortWords(array) that does the following: 1. Takes as an input a cell array of strings consisting of letters only. (1 pts) 2. Returns a cell array with all the strings in alphabetical order. (5 pts) 3. The function should ignore whether letters are lower case or upper case. (2 pts) Test your function with the following: (2 pts) >> sorted=sortWords({’Hello’,’hell’,’abc’,’aa’,’aza’,’aab’,’AaBb’,’a’}) sorted = ’a’ ’aa’ ’aab’ ’AaBb’ ’abc’ ’aza’ ’hell’ ’Hello’ Note: Your function may...
1. Recall Ken Washington from the beginning of Chapter 35. Now that you have completed the...
1. Recall Ken Washington from the beginning of Chapter 35. Now that you have completed the chapter, answer the following questions regarding his case. 1. Is it significant that Ken had a urinary catheter in place for 6 days while he was in the hospital? 2. Dr. Buckwalter plans to send Ken home with a urinary catheter in place. What information can you give him to help him prevent infection? 3. You note on the chart that Dr. Buckwalter wants...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT