Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment assumes you have completed Programming Challenge 1 of Chapter 10

ID: 3547108 • Letter: T

Question

This assignment assumes you have completed Programming Challenge 1 of Chapter 10. 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 invalid employee number. User can ommit the dash "-" between the numbers and letters, you must account for both accepted formats.
* 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 or if the data entered is not numeric.

Write a test program that demonstrates how each of these exception conditions works.


I have my classes and program, just need the exceptions to add to the classes somehow. Im confused on where and how to add them. thanks

Explanation / Answer

Start NetBeans and create a Java Application named bcr3 Change the file as follows: package bcr3; import java.util.*; public class Main { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); long employeeNumber = 0; String employeeName; double hourlySalary = 0.00; double weeklyTime = 0.00; double regularTime, overtime; double regularPay, overtimePay, netPay; try { System.out.print("Enter Employee Number (00000): "); employeeNumber = scnr.nextLong(); } catch(Exception eim) { System.out.println(eim.getMessage()); } if( employeeNumber == 82500 ) employeeName = "Helene Mukoko"; else if( employeeNumber == 92746 ) employeeName = "Raymond Kouma"; else if( employeeNumber == 54080 ) employeeName = "Henry Larson"; else if( employeeNumber == 86285 ) employeeName = "Gertrude Monay"; else employeeName = "Unknown"; System.out.print("Enter Hourly Salary: "); hourlySalary = scnr.nextDouble(); System.out.print("Enter Weekly Time: "); weeklyTime = scnr.nextDouble(); if(weeklyTime < 40 ) { regularTime = weeklyTime; overtime = 0; regularPay = hourlySalary * regularTime; overtimePay = 0; netPay = regularPay; } else { regularTime = 40; overtime = weeklyTime - 40; regularPay = hourlySalary * 40; overtimePay = hourlySalary * overtime; netPay = regularPay + overtimePay; } System.out.println("======================"); System.out.println("=//= BETHESDA CAR RENTAL =//="); System.out.println("==-=-= Employee Payroll =-=-=="); System.out.println("-------------------------------------------"); System.out.printf("Employee #: %d ", employeeNumber); System.out.printf("Employee Name: %s ", employeeName); System.out.printf("Hourly Salary: %.2f ", hourlySalary); System.out.printf("Weekly Time: %.2f ", weeklyTime); System.out.printf("Regular Pay: %.2f ", regularPay); System.out.printf("Overtime Pay: %.2f ", overtimePay); System.out.printf("Total Pay: %.2f ", netPay); System.out.println("======================"); } } To execute the exercise, press F6 Enter the Employee number as 82500, the hourly salary as 22.35, and the weekly hours as 38.50, then press Enter Enter Employee Number (00000): 82500 Enter Hourly Salary: 22.35 Enter Weekly Time: 38.50 ============================== =//= BETHESDA CAR RENTAL =//= ==-=-= Employee Payroll =-=-== ------------------------------ Employee #: 82500 Employee Name: Helene Mukoko Hourly Salary: 22.35 Weekly Time: 38.50 Regular Pay: 860.48 Overtime Pay: 0.00 Total Pay: 860.48 ======================Throwing an Exception To throw an exception, you use the throw keyword. The primary formula of using this keyword is: try { Normal flow When to throw the exception throw What? } catch(Exception e) { // Deal with the exception here } The new keyword in this formula is throw. On the right side of throw, indicate to the compiler what to do. This means that the throw keyword is followed by an expression. We will see various examples. In our introduction to exceptions, we got acquainted with the Exception class. Besides the default constructor, the Exception class is equipped with another constructor that takes a string as argument. When using the throw keyword, you can use this constructor to specify a message to display to display in case of an error. To do this, use the new operator to call the constructor and pass a message to it. Here is an example: import java.util.Scanner; public class Exercise { public static void main(String[] args) { int studentAge = 0; Scanner scnr = new Scanner(System.in); try { System.out.print("Student Age: "); studentAge = scnr.nextInt(); if( studentAge < 0 ) throw new Exception("Positive Number Required"); System.out.println("Student Age: " + studentAge); } catch(Exception exc) { System.out.println("Error: " + exc.getMessage()); } } } This program starts with the try block that asks the user to enter a positive number. If the user enters an invalid value, the program examines the throw keyword. This throw appears to display a string. The compiler registers this string and since there was an exception, the program exits the try block (it gets out of the try block even if the rest of the try block is fine) and looks for the first catch block it can find. If it finds a catch that does not take an argument, it would still use the catch. Otherwise, you can use the catch block to display the error string that was sent by the throw keyword. Practical Learning: Throwing an Exception To throw an exception, change the Main.java file as follows: public class Main { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); long employeeNumber = 0; String employeeName; double hourlySalary = 0.00; double weeklyTime = 0.00; double regularTime, overtime; double regularPay, overtimePay, netPay; try { System.out.print("Enter Employee Number (00000): "); employeeNumber = scnr.nextLong(); if( employeeNumber < 0 ) throw new Exception("The employee number must be positive"); } catch(Exception exc) { System.out.println(exc.getMessage()); } . . . No Change } } Execute the applicationWhen asked to enter the employee number, enter -22446688 and press EnterEnter the hourly salary as 22.35, and the weekly hours as 42.50, then press Enter Enter Employee Number (00000): -22446688 Enter Hourly Salary: 22.35 Enter Weekly Time: 42.50 ====================== =//= BETHESDA CAR RENTAL =//= ==-=-= Employee Payroll =-=-== ------------------------------------------- Employee #: -22446688 Employee Name: Unknown Hourly Salary: 22.35 Weekly Time: 42.50 Regular Pay: 894.00 Overtime Pay: 55.88 Total Pay: 949.88 ======================
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote