Please answer the following question with Java
Modify the Employee and ProductionWorker classes provided below 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 thrown anexception 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 works.
Employee class
/** The Employee class stores data about an employee for the Employee and ProductionWorker Classes programming challenge. */
public class Employee
{ private String name; // Employee name
private String employeeNumber; // Employee number
private String hireDate; // Employee hire date
/**
This constructor initializes an object with a name, employee number, and hire date. @param n The employee's name. @param num The employee's number. @param date The employee's hire date.
*/
public Employee(String n, String num, String date)
{
name = n; setEmployeeNumber(num); hireDate = date;
}
/**
The no-arg constructor initializes an object with null strings for name, employee number, and hire date.
*/
public Employee()
{
name = "";
employeeNumber = "";
hireDate = "";
}
/**
The setName method sets the employee's name. @param n The employee's name.
*/
public void setName(String n)
{
name = n;
}
/**
The setEmployeeNumber method sets the employee's number. @param e The employee's number.
*/
public void setEmployeeNumber(String e)
{
if (isValidEmpNum(e)) employeeNumber = e; else employeeNumber = "";
}
/**
The setHireDate method sets the employee's hire date. @param h The employee's hire date.
*/
public void setHireDate(String h)
{
hireDate = h;
}
/**
The getName method returns the employee's name. @return The employee's name.
*/
public String getName()
{
return name;
}
/**
The getEmployeeNumber method returns the employee's number. @return The employee's number.
*/
public String getEmployeeNumber()
{ r
eturn employeeNumber;
}
/**
The getHireDate method returns the employee's hire date. @return The employee's hire date.
*/
public String getHireDate()
{ return hireDate;
}
/**
isValidEmpNum is a private method that determines whether a string is a valid employee number. @param e The string containing an employee number. @return true if e references a valid ID number, false otherwise.
*/
private boolean isValidEmpNum(String e)
{ boolean status = true;
if (e.length() != 5) status = false; else
{ if ((!Character.isDigit(e.charAt(0))) ||
(!Character.isDigit(e.charAt(1))) ||
(!Character.isDigit(e.charAt(2))) ||
(e.charAt(3) != '-') ||
(!Character.isLetter(e.charAt(4))))
status = false;
}
return status;
}
/**
toString method @return A reference to a String representation of the object.
*/
public String toString()
{
String str = "Name: " + name + "\nEmployee Number: ";
if (employeeNumber == "")
str += "INVALID EMPLOYEE NUMBER";
else
str += employeeNumber;
str += ("\nHire Date: " + hireDate); return str;
}
}
ProductionWorker class
/**
The ProductionWorker class stores data about an employee who is a production worker for the Employee and ProductionWorker Classes programming challenge.
*/
public class ProductionWorker extends Employee
{
// Constants for the day and night shifts. public static final int DAY_SHIFT = 1;
public static final int NIGHT_SHIFT = 2; private int shift; // The employee's shift
private double payRate; // The employee's pay rate
/**
This constructor initializes an object with a name, employee number, hire date, shift, and pay rate
@param n The employee's name.
@param num The employee's number.
@param date The employee's hire date.
@param sh The employee's shift.
@param rate The employee's pay rate.
*/
public ProductionWorker(String n, String num, String date, int sh, double rate)
{
super(n, num, date); shift = sh; payRate = rate;
}
/**
The no-arg constructor initializes an object with null strings for name, employee number, and hire date. The day shift is selected, and the pay rate is set to 0.0.
*/
public ProductionWorker()
{
super(); shift = DAY_SHIFT; payRate = 0.0;
}
/**
The setShift method sets the employee's shift. @param s The employee's shift.
*/
public void setShift(int s)
{
shift = s;
}
/**
The setPayRate method sets the employee's pay rate. @param p The employee's pay rate.
*/
public void setPayRate(double p)
{
payRate = p;
}
/** T
he getShift method returns the employee's shift. @return The employee's shift.
*/
public int getShift()
{
return shift;
}
/**
The getPayRate method returns the employee's pay rate. @return The employee's pay rate.
*/
public double getPayRate()
{
return payRate;
}
/**
toString method @return A reference to a String representation of the object.
*/
@Override
public String toString()
{
String str = super.toString();
str += "\nShift: ";
if (shift == DAY_SHIFT)
str += "Day";
else if (shift == NIGHT_SHIFT)
str += "Night";
else str += "INVALID SHIFT NUMBER";
str += String.format("\nHourly Pay Rate: $%,.2f", payRate);
return str;
}
}
Please rate the answer and do comment in case of any query. Thanks.
************************************************************* Code *************************************************************
InvalidEmployeeNumberException.java:
public class InvalidEmployeeNumberException extends
Exception {
public InvalidEmployeeNumberException(String s)
{
super(s);
}
}
InvalidShiftException.java:
public class InvalidShiftException extends Exception {
public InvalidShiftException(String s) {
super(s);
}
}
InvalidPayRateException.java:
public class InvalidPayRateException extends Exception
{
public InvalidPayRateException(String s) {
super(s);
}
}
Employee.java:
/** The Employee class stores data about an employee for the Employee and ProductionWorker Classes programming challenge. */
public class Employee {
private String name; // Employee name
private String employeeNumber; // Employee
number
private String hireDate; // Employee hire
date
/**
* This constructor initializes an object with a name,
employee number, and hire date.
* @param n The employee's name.
* @param num The employee's number.
* @param date The employee's hire date.
*/
public Employee(String n, String num, String date)
throws InvalidEmployeeNumberException {
name = n;
setEmployeeNumber(num);
hireDate = date;
}
/**
* The no-arg constructor initializes an object with
null strings for name, employee number, and hire date.
*/
public Employee() {
name = "";
employeeNumber = "";
hireDate = "";
}
/**
* The setName method sets the employee's name. @param
n The employee's name.
*/
public void setName(String n) {
name = n;
}
/**
* The setEmployeeNumber method sets the employee's
number.
* @param e The employee's number.
*/
public void setEmployeeNumber(String e) throws
InvalidEmployeeNumberException {
// Valid employee number will be in
the format "123-d"
// InvalidEmployeeNumberException
is thrown if the number part (123) is <0 or >9999
// Identifying the last index of
'-'
int index =
e.lastIndexOf("-");
// Taking the number part from the
employee number
String num = e;
if(index > 0)
num =
e.substring(0, index);
// Converting it from string to
integer along with exception handling for
NumberFormatException
int empNum = 0;
try { empNum =
Integer.parseInt(num); }
catch(Exception ex) {}
// Throwing
InvalidEmployeeNumberException if it is invalid
if(empNum < 0 || empNum >
9999)
throw new
InvalidEmployeeNumberException("Employee number shoould be >= 0
and <= 9999");
// If employee number is between 0
and 9999, check the format if it is valid or not
// NOTE: Exception is not thrown if
entered employee number is "1000", instead "INVALID NUMBER" is
displayed
if (isValidEmpNum(e))
employeeNumber = e;
else employeeNumber = "";
}
/**
* The setHireDate method sets the employee's hire
date.
* @param h The employee's hire date.
*/
public void setHireDate(String h) {
hireDate = h;
}
/**
* The getName method returns the employee's
name.
* @return The employee's name.
*/
public String getName() {
return name;
}
/**
* The getEmployeeNumber method returns the employee's
number.
* @return The employee's number.
*/
public String getEmployeeNumber() {
return employeeNumber;
}
/**
* The getHireDate method returns the employee's hire
date.
* @return The employee's hire date.
*/
public String getHireDate() {
return hireDate;
}
/**
* isValidEmpNum is a private method that determines
whether a string is a valid employee number.
* @param e The string containing an employee
number.
*
* @return true if e references a valid ID number,
false otherwise.
*/
private boolean isValidEmpNum(String e) {
boolean status = true;
if (e.length() != 5) status =
false;
else {
if
((!Character.isDigit(e.charAt(0))) ||
(!Character.isDigit(e.charAt(1))) ||
(!Character.isDigit(e.charAt(2))) ||
(e.charAt(3) != '-') ||
(!Character.isLetter(e.charAt(4))))
status = false;
}
return status;
}
/**
* toString method
* @return A reference to a String representation of
the object.
*/
public String toString() {
String str = "Name: " + name +
"\nEmployee Number: ";
if (employeeNumber == "")
str += "INVALID
EMPLOYEE NUMBER";
else
str +=
employeeNumber;
str += ("\nHire Date: " +
hireDate); return str;
}
}
ProductionWorker.java:
/**
* The ProductionWorker class stores data about an employee who is a
production worker for the Employee and ProductionWorker Classes
programming challenge.
*/
public class ProductionWorker extends Employee {
// Constants for the day and night shifts.
public static final int DAY_SHIFT = 1;
public static final int NIGHT_SHIFT = 2;
private int shift; // The employee's shift
private double payRate; // The employee's pay
rate
/**
* This constructor initializes an object with a name,
employee number, hire date, shift, and pay rate
* @param n The employee's name.
* @param num The employee's number.
* @param date The employee's hire date.
* @param sh The employee's shift.
* @param rate The employee's pay rate.
*/
public ProductionWorker(String n, String num, String
date, int sh, double rate)
throws
InvalidEmployeeNumberException, InvalidShiftException,
InvalidPayRateException {
super(n, num, date);
// Throw InvalidShiftException if
entered shift is neither 1 nor 2
if(sh != 1 && sh !=
2)
throw new
InvalidShiftException("Shift should be either 1 or 2");
shift = sh;
// Throw InvalidPayRateException if
entered payrate is negative
if(rate < 0)
throw new
InvalidPayRateException("Pay rate should not be negative");
payRate = rate;
}
/**
* The no-arg constructor initializes an object with
null strings
* for name, employee number, and hire date.
* The day shift is selected, and the pay rate is set
to 0.0.
*/
public ProductionWorker() {
super();
shift = DAY_SHIFT;
payRate = 0.0;
}
/**
* The setShift method sets the employee's shift.
* @param s The employee's shift.
*/
public void setShift(int s) {
shift = s;
}
/**
* The setPayRate method sets the employee's pay
rate.
* @param p The employee's pay rate.
*/
public void setPayRate(double p) {
payRate = p;
}
/**
* The getShift method returns the employee's
shift.
* @return The employee's shift.
*/
public int getShift() {
return shift;
}
/**
* The getPayRate method returns the employee's pay
rate.
* @return The employee's pay rate.
*/
public double getPayRate() {
return payRate;
}
/**
* toString method
* @return A reference to a String representation of
the object.
*/
@Override
public String toString() {
String str =
super.toString();
str += "\nShift: ";
if (shift == DAY_SHIFT)
str +=
"Day";
else if (shift ==
NIGHT_SHIFT)
str +=
"Night";
else str += "INVALID SHIFT
NUMBER";
str += String.format("\nHourly Pay
Rate: $%,.2f", payRate);
return str;
}
}
TestProgram.java:
import java.io.*;
public class TestProgram {
public static void main(String[] args) throws
IOException {
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
// Continuing execution until user
wants to end
while(true) {
try {
System.out.print("Enter employee name:
"); // Prompting user for Employee
Name
String empName = br.readLine();
System.out.print("Enter employee number:
"); // Prompting user for Employee Number
String empNum = br.readLine();
System.out.print("Enter hire date:
"); // Prompting
user for Hire date
String hireDate = br.readLine();
System.out.print("Enter Shift (as a number):
");// Prompting user for Shift
int shift =
Integer.parseInt(br.readLine());
System.out.print("Enter Payrate: ");
// Prompting user for
Payrate
double payRate =
Double.parseDouble(br.readLine());
Employee emp = new ProductionWorker(empName,
empNum, hireDate, shift, payRate);
System.out.println("=============================================================");
System.out.println(emp);
System.out.println("=============================================================");
}
catch(Exception
e) {
System.err.println(e);
}
System.out.print("Continue? (y/n): "); // Prompting
user whether to continue or not
String ctn =
br.readLine();
// If 'n' is given as input, execution ends. For
any other input, execution continues
if(ctn.equals("n"))
break;
}
}
}
************************************************ Screenshots of the code ************************************************
InvalidEmployeeNumberException.java:
InvalidShiftException.java:
InvalidPayRateException.java:
Employee.java:
ProductionWorker.java:
TestProgram.java:
************************************************ Screenshot of sample output ************************************************
Hope this helps you and would suffice your requirement.
Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so...
Given attached you will find a file from Programming Challenge 5 of chapter 6 with required you to write a Payroll class that calculates an employee’s payroll. Write exception classes for the following error conditions: An empty string is given for the employee’s name. An invalid value is given to the employee’s ID number. If you implemented this field as a string, then an empty string could be invalid. If you implemented this field as a numeric variable, then a...
C++ Lab 9B Inheritance Class Production Worker Create a project C2010Lab9b; add a source file Lab9b.cpp to the project. Copy and paste the code is listed below: Next, write a class named ProductionWorker that is derived from the Employee class. The ProductionWorker class should have member variables to hold the following information: Shift (an integer) Hourly pay rate (a double) // Specification file for the ProductionWorker Class #ifndef PRODUCTION_WORKER_H #define PRODUCTION_WORKER_H #include "Employee.h" #include <string> using namespace std; class ProductionWorker...
HospitalEmployee Inheritance help please. import java.text.NumberFormat; public class HospitalEmployee { private String empName; private int empNumber; private double hoursWorked; private double payRate; private static int hospitalEmployeeCount = 0; //----------------------------------------------------------------- // Sets up this hospital employee with default information. //----------------------------------------------------------------- public HospitalEmployee() { empName = "Chris Smith"; empNumber = 9999; hoursWorked = 0; payRate =0; hospitalEmployeeCount++; } //overloaded constructor. public HospitalEmployee(String eName,...
By editing the code below to include composition, enums, toString; must do the following: Prompt the user to enter their birth date and hire date (see Fig. 8.7, 8.8 and 8.9 examples) in addition to the previous user input Create a new class that validates the dates that are input (can copy date class from the book) Incorporate composition into your class with these dates Use enums to identify the employee status as fulltime (40 or more hours worked for...
I was wondering if I could get some help with a Java Program that I am currently working on for homework. When I run the program in Eclipse nothing shows up in the console can you help me out and tell me if I am missing something in my code or what's going on? My Code: public class Payroll { public static void main(String[] args) { } // TODO Auto-generated method stub private int[] employeeId = { 5658845, 4520125, 7895122,...
JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...
JAVA HELP Design a class named Employee. The class should keep the following information in fields: Employee name Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. Hire date then, Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that extends the Employee class. The ProductionWorker class should have fields to...
Employee and ProductionWorker Classes DONE IN C# PLEASE Create an Employee class that has properties for the following data: • Employee name • Employee number Next, create a class named ProductionWorker that is derived from the Employee class. The ProductionWorker class should have properties to hold the following data: • Shift number (an integer, such as 1, 2, or 3) • Hourly pay rate The workday is divided into two shifts: day and night. The Shift property will hold an...
The following is for java programming. the classes money date and array list are so I are are pre made to help with the coding so you can resuse them where applicable Question 3. (10 marks) Here are three incomplete Java classes that model students, staff, and faculty members at a university class Student [ private String lastName; private String firstName; private Address address; private String degreeProgram; private IDNumber studentNumber; // Constructors and methods omitted. class Staff private String lastName;...
Please fill in the remaining code necessary and follow the instructions below this is an abstract class; it represents an department in the company. This class cannot be instantiated (objects created), but it serves as the parent for other classes. Department – This class represents a department within the company. 1. Provide the implementation (the body) for the following methods: public int getDepartmentID()) public String getDepartmentName()) public Manager getManager()) 2. Fix the implementation for the following method– substitute “blah” with...