java
Payroll class Exceptions Programming Challenge 5 of Chapter 6 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 for the employee’s ID number. If you implemented this field as a string, then an empty string would be invalid. If you implemented this field as a numeric variable, then a negative number or zero would be invalid.
• An invalid number is given for the number of hours worked. This would be a negative number or a number greater than 84.
• An invalid number is given for the hourly pay rate. This would be a negative number or a number greater than 25.
Modify the Payroll class so that it throws the appropriate exception when any of these errors occurs. Demonstrate the exception classes in a program.
File name must be: PayRollExceptions.java
Test Case1:
Enter the employee's name: John
Enter employee number, (ex. 999-M): 9999
Enter the employee's hourly rate: 20
Enter the number of hours the employee has worked: 20
Error: Numericals in ID must be between 0-9 and letters must be
between A-M
Test Case2:
Enter the employee's name: John
Enter employee number, (ex. 999-M): 999-N
Enter the employee's hourly rate: 20
Enter the number of hours the employee has worked: -2
Error: Numericals in ID must be between 0-9 and letters must be
between A-M
Test Case3:
Enter the employee's name: John
Enter employee number, (ex. 999-M): 999-J
Enter the employee's hourly rate: 20
Enter the number of hours the employee has worked: -2
Error: Hours Cannot be negative or greater than 84
Test Case4:
Enter the employee's name: John
Enter employee number, (ex. 999-M): 999-H
Enter the employee's hourly rate: -2
Enter the number of hours the employee has worked: 20
Error: Hourly Rate Cannot be negative or greater than 25
Test Case5:
Enter the employee's name: John
Enter employee number, (ex. 999-M): 999-H
Enter the employee's hourly rate: 20
Enter the number of hours the employee has worked: 40
Employees name: John
ID: 999-H
Hourly Rate: $20.0
Hours: 40 hrs
Gross Pay: $800.0
/////////////////////////////////////////////////////////////////////////////////////////////
Code to copy
import java.util.Scanner;
class Payroll {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
String name;
String empNum;
double empHourRat;
int hour;
System.out.print("Enter the
employee's name:");
name = sc.nextLine();
// //name ="";
System.out.print("Enter employee
number, (ex. 999-M):");
empNum = sc.nextLine();
System.out.print("Enter the
employee's hourly rate:");
empHourRat = sc.nextInt();
System.out.print("Enter the
number of hours the employee has worked:");
hour = sc.nextInt();
try {
checkName(name);
checkEmpNum(empNum);
checkInvalidHourPayRate(empHourRat);
checkInvalidHour(hour);
System.out.println("Employees name:" + name);
System.out.println("ID:" + empNum);
System.out.println("Hourly Rate: $" + empHourRat);
System.out.println("Hours: " + hour + "hrs");
System.out.println("Gross Pay: $" + empHourRat * hour);
} catch (Exception m) {
System.out.println("\nError: " + m.getMessage());
}
}
public static void checkName(String s) throws
checkName {
if (s.length() == 0) {
throw new
checkName("empty string cannot be assigned the employe's
name.");
} else {
return;
}
}
public static void checkInvalidHour(int h) throws
InvalidHourNumber {
if (h < 0 || h > 84) {
throw new
InvalidHourNumber("Hours Cannot be negative or greater than
84");
}
}
public static void checkInvalidHourPayRate(double
h) throws InvalidHourPayRate {
if (h < 0 || h > 25) {
throw new
InvalidHourPayRate("Hourly Rate Cannot be negative or greater than
25");
}
}
public static void checkEmpNum(String s1) throws
checkEmpNum {
boolean flag = false;
if (s1.length() == 0) {
throw new
checkEmpNum("Numericals in ID must be between 0-9 and letters must
be between A-M");
} else if (s1.contains("-"))
{
String[] arr =
s1.split("-");
if (arr.length
== 1) {
throw new checkEmpNum("Numericals in ID must be
between 0-9 and letters must be between A-M");
}
if
((Integer.parseInt(arr[0]) < 0) || !(arr[1].equals("A") ||
arr[1].equals("B") || arr[1].equals("C")
|| arr[1].equals("D") ||
arr[1].equals("E") || arr[1].equals("F") ||
arr[1].equals("G")
|| arr[1].equals("H") ||
arr[1].equals("I") || arr[1].equals("J") ||
arr[1].equals("K")
|| arr[1].equals("L") ||
arr[1].equals("M"))) {
flag = true;
}
if (flag ==
true) {
throw new checkEmpNum("Numericals in ID must be
between 0-9 and letters must be between A-M");
}
} else {
throw new
checkEmpNum("Numericals in ID must be between 0-9 and letters must
be between A-M");
}
}
}
class InvalidHourNumber extends Exception {
InvalidHourNumber(String s) {
super(s);
}
}
class InvalidHourPayRate extends Exception {
InvalidHourPayRate(String s) {
super(s);
}
}
class checkName extends Exception {
checkName(String s) {
super(s);
}
}
class checkEmpNum extends Exception {
checkEmpNum(String s) {
super(s);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
EXPLANATION
import java.util.Scanner;
class Payroll {//creating Payroll class
public static void main(String[] args) {//creating
main method
Scanner sc = new
Scanner(System.in);//intializing Scanner object for taking
input
String name;//declaring name
variable
String empNum;//declaraing emoNum
variable
double empHourRat;//decalraing a
double variable
int hour;//declaring
System.out.print("Enter the
employee's name:");//printing user to enter name
name = sc.nextLine();//taking
input
// //name ="";
System.out.print("Enter employee
number, (ex. 999-M):");//printing user to emp number
empNum = sc.nextLine();//taking
input
System.out.print("Enter the
employee's hourly rate:");//printing user to enter emo Hour
Rate
empHourRat = sc.nextInt();//taking
input
System.out.print("Enter the
number of hours the employee has worked:");//printing user to enter
Hours
hour = sc.nextInt();//taking
input
try {
checkName(name);//checking the details
checkEmpNum(empNum);//checking the emp number format
checkInvalidHourPayRate(empHourRat);//checking the rate
checkInvalidHour(hour);//cheking the input of hours
System.out.println("Employees name:" + name);//printing name
System.out.println("ID:" + empNum);//printing emp NUmber
System.out.println("Hourly Rate: $" + empHourRat);//printing emp
hourly rate
System.out.println("Hours: " + hour + "hrs");//printing emp number
of hours
System.out.println("Gross Pay: $" + empHourRat * hour);//printing
total rate
} catch (Exception m) {
System.out.println("\nError: " + m.getMessage());//printing the
Exception
}
}
public static void checkName(String s) throws
checkName {
if (s.length() == 0) {//if the name
is empty
throw new
checkName("empty string cannot be assigned the employe's
name.");//throw the error
} else {
return;
}
}
public static void checkInvalidHour(int h) throws
InvalidHourNumber {
if (h < 0 || h > 84)
{//checking the hour are not more than 84 and non negative
number
throw new
InvalidHourNumber("Hours Cannot be negative or greater than
84");//else throw the error
}
}
public static void checkInvalidHourPayRate(double
h) throws InvalidHourPayRate {
if (h < 0 || h > 25)
{//checking if rate is not negative and not more than 25
throw new
InvalidHourPayRate("Hourly Rate Cannot be negative or greater than
25");//throw error
}
}
public static void checkEmpNum(String s1) throws
checkEmpNum {
boolean flag = false;//boolean
value to check
if (s1.length() == 0) {//if String
is empty
throw new
checkEmpNum("Numericals in ID must be between 0-9 and letters must
be between A-M");//throe error
} else if (s1.contains("-"))
{//checking that it has - in the String 999-M
String[] arr =
s1.split("-");//seperating the array arr[0] = 999 and arr[1] =
M
if (arr.length
== 1) {//if it doesnot have a Chacter throw error
ex:999-
throw new checkEmpNum("Numericals in ID must be
between 0-9 and letters must be between A-M");
}
if
((Integer.parseInt(arr[0]) < 0) || !(arr[1].equals("A") ||
arr[1].equals("B") || arr[1].equals("C")
|| arr[1].equals("D") ||
arr[1].equals("E") || arr[1].equals("F") ||
arr[1].equals("G")
|| arr[1].equals("H") ||
arr[1].equals("I") || arr[1].equals("J") ||
arr[1].equals("K")
|| arr[1].equals("L") ||
arr[1].equals("M"))) {
flag = true;//if it does not have character
between A-M put flag as true
}
if (flag ==
true) {//throw error if flag is true
throw new checkEmpNum("Numericals in ID must be
between 0-9 and letters must be between A-M");
}
} else {//if it does not contain -
in the String s1 throw error
throw new
checkEmpNum("Numericals in ID must be between 0-9 and letters must
be between A-M");
}
}
}
class InvalidHourNumber extends Exception {//creating a class
which extends Exception
InvalidHourNumber(String s) {//taking the error
message and sending to Exception class
super(s);
}
}
class InvalidHourPayRate extends Exception {//creating a class
which extends Exception
InvalidHourPayRate(String s) {//taking the error
message and sending to Exception class
super(s);
}
}
class checkName extends Exception {//creating a class which
extends Exception
checkName(String s) {//taking the error message and
sending to Exception class
super(s);
}
}
class checkEmpNum extends Exception {//creating a class which
extends Exception
checkEmpNum(String s) {//taking the error message and
sending to Exception class
super(s);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
IF YOU HAVE ANY PROBLEM REGARDING THE SOLUTION PLEASE COMMENT BELOW
java Payroll class Exceptions Programming Challenge 5 of Chapter 6 required you to write a Payroll...
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++ Program The Ward Bus Manufacturing Company has recently hired you to help them convert their manual payroll system to a computer-based system. Write a program to produce a 1-week payroll report for only one employee to serve as a prototype (model) for the administration to review. Input for the system will be the employee’s 4-digit ID number, the employee’s name, hours worked that week, and the employee’s hourly pay rate. Output should consist of the employee’s ID number, the...
IN JAVADesign a Payroll class with the following fields:• name: a String containing the employee's name• idNumber: an int representing the employee's ID number• rate: a double containing the employee's hourly pay rate• hours: an int representing the number of hours this employee has workedThe class should also have the following methods:• Constructor: takes the employee's name and ID number as arguments• Accessors: allow access to all of the fields of the Payroll class• Mutators: let the user assign values...
With Java Language: In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) .İd: String (5 pts) hours: int (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an...
It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming Challenge 2 Employee Class.pdf Program Template: // Chapter 13, Programming Challenge 2: Employee Class #include <iostream> #include <string> using namespace std; // Employee Class Declaration class Employee { private: string name; // Employee's name int idNumber; // ID number string department; // Department name string position; // Employee's position public: // TODO: Constructors // TODO: Accessors // TODO: Mutators }; // Constructor #1 Employee::Employee(string...
Write a Java application with a class name of Payroll, for the “Travel Agency”, that willCalculate the weekly paycheck for both Salaried and Hourly employees. Salaried employees will be paid 1/52 of their annual pay minus 18% withheld for federal and state taxes, as well as 4% for retirement pension. Salaried employees do not collect overtime pay. There are two types of Hourly employees; permanent employees and temporary weekly employees. •Permanent weekly employees will be paid their hourly rate minus...
Create a Java file named Ch6Asg.java that contains a public class named Ch6Asg containing a main() method. Within the same file, create another class named Employee, and do not declare it public. Create a field for each of the following pieces of information: employee name, employee number, hourly pay rate, and overtime rate. Create accessor and mutator methods for each field. The mutator method for the hourly pay rate should require the value to be greater than zero. If it...
In C++ Please please help.. Assignment 5 - Payroll Version 1.0 In this assignment you must create and use a struct to hold the general employee information for one employee. Ideally, you should use an array of structs to hold the employee information for all employees. If you choose to declare a separate struct for each employee, I will not deduct any points. However, I strongly recommend that you use an array of structs. Be sure to read through Chapter...
Design a PayRoll class that has data members for an employee’s first and last name, hourly pay rate, number of hours worked. The default constructor will set the first and last name to empty string, and hours worked and pay rate to zero. The class must have mutator functions to set each of the data members of the class and accessors to access them. The class should also have the following member functions: displayPayroll function that displays all data members...
My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...