Question: Chapter 11 - Exception (JAVA OOP) Attached you will find a file from Programming Challenge 5 of chapter 6 with required you to write a

Chapter 11 - Exception (JAVA OOP)

Attached you will find a file from Programming Challenge 5 of chapter 6 with required you to write a Payroll class that calculates an employees payroll. Write exception classes for the following error conditions:

An empty string is given for the employees name.

An invalid value is given for the employees 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 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. This is a Demo Classimport java.util.Scanner;

/**

This program demonstrates a solution to the

Payroll Class programming challenge.

*/

public class PayrollDemo

{

public static void main(String[] args)

{

// Variables for input

String name; // An employee's name

int id; // An employee's ID number

double payRate; // An employee's pay rate

double hoursWorked; // The number of hours worked

// Create a Scanner object for keyboard input.

Scanner keyboard = new Scanner(System.in);

// Get the employee's name.

System.out.print("Enter the employee's name: ");

name = keyboard.nextLine();

// Get the employee's ID number.

System.out.print("Enter the employee's ID number: ");

id = keyboard.nextInt();

// Get the employee's pay rate.

System.out.print("Enter the employee's hourly pay rate: ");

payRate = keyboard.nextDouble();

// Get the number of hours worked by the employee.

System.out.print("Enter the number of hours worked " +

" by the employee: ");

hoursWorked = keyboard.nextDouble();

// Create a Payroll object and store the data in it.

Payroll worker = new Payroll(name, id);

worker.setPayRate(payRate);

worker.setHoursWorked(hoursWorked);

// Display the employee's payroll data.

System.out.println(" Employee Payroll Data");

System.out.println("Name: " + worker.getName());

System.out.println("ID Number: " + worker.getIdNumber());

System.out.println("Hourly pay rate: " + worker.getPayRate());

System.out.println("Hours worked: " + worker.getHoursWorked());

System.out.println("Gross pay: $" + worker.getGrossPay());

}

}

//This is Payroll Class. public class Payroll

{

private String name; // Employee name

private int idNumber; // ID number

private double payRate; // Hourly pay rate

private double hoursWorked; // Number of hours worked

/**

The constructor initializes an object with the

employee's name and ID number.

@param n The employee's name.

@param i The employee's ID number.

*/

public Payroll(String n, int i)

{

setName(n);

setIdNumber(i);

}

/**

The setName sets the employee's name.

@param n The employee's name.

*/

public void setName(String n)

{

name = n;

}

/**

The setIdNumber sets the employee's ID number.

@param i The employee's ID number.

*/

public void setIdNumber(int i)

{

idNumber = i;

}

/**

The setPayRate sets the employee's pay rate.

@param p The employee's pay rate.

*/

public void setPayRate(double p)

{

payRate = p;

}

/**

The setHoursWorked sets the number of hours worked.

@param h The number of hours worked.

*/

public void setHoursWorked(double h)

{

hoursWorked = h;

}

/**

The getName returns the employee's name.

@return The employee's name.

*/

public String getName()

{

return name;

}

/**

The getIdNumber returns the employee's ID number.

@return The employee's ID number.

*/

public int getIdNumber()

{

return idNumber;

}

/**

The getPayRate returns the employee's pay rate.

@return The employee's pay rate.

*/

public double getPayRate()

{

return payRate;

}

/**

The getHoursWorked returns the hours worked by the

employee.

@return The hours worked.

*/

public double getHoursWorked()

{

return hoursWorked;

}

/**

The getGrossPay returns the employee's gross pay.

@return The employee's gross pay.

*/

public double getGrossPay()

{

return hoursWorked * payRate;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!