Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 cod.

import 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/** The Payroll class stores data about an employee's pay for the Payroll Class programming challenge. */

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learn To Program Databases With Visual Basic 6

Authors: John Smiley

1st Edition

1902745035, 978-1902745039

More Books

Students also viewed these Databases questions

Question

Describe buyer and lessee performance of sales and lease contracts.

Answered: 1 week ago