Question
Please help me code in java. Here is Ch.6 Challenge 5 solution public class Payroll { private String name; private int idNumber; private double payRate;
Please help me code in java.
Here is Ch.6 Challenge 5 solution
public class Payroll { private String name; private int idNumber; private double payRate; private double hoursWorked;
Payroll(String n, int i) { name = n; idNumber = i; }
public void setName(String n) { name = n; } public void setIdNumber(int i) { idNumber = i; } public void setPayRate(double p) { payRate = p; }
public void setHoursWorked(double h) { hoursWorked = h; }
public String getName() { return name; } public int getIdNumber() { return idNumber; }
public double getPayRate() { return payRate; } public double getHoursWorked() { return hoursWorked; }
public double getGrossPay() { return hoursWorked * payRate; } }
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 Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the employee's name: "); name = keyboard.nextLine(); System.out.print("Enter the employee's ID number: "); id = keyboard.nextInt(); System.out.print("Enter the employee's hourly pay rate: "); payRate = keyboard.nextDouble(); System.out.print("Enter the number of hours worked " + " by the employee: "); hoursWorked = keyboard.nextDouble();
Payroll worker = new Payroll(name, id); worker.setPayRate(payRate); worker.setHoursWorked(hoursWorked); 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()); } }
Design a Payroll class that has fields for an employee's name, ID number, hourly pay rate, and number of hours worked. Write the appropriate accessor and mutator methods and a constructor that accepts the employee's name and ID number as arguments. The class should also have a method that returns the employee's gross pay, which is calculated as the number of hours worked multiplied by the hourly pay rate. Write a program that demonstrates the class by creating a Payroll object, then asking the user to enter the data for an employee. The program should display the amount of gross pay earned. Solution is given Now Start your Lab 4... 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 he 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 given Payroll class so that it throws the appropriate exception when any of these errors occurs. Demonstrate the exception classes in a program. Deliverables: - InvalidNameException.java (10\%) - InvalidIDException.java (10\%) - InvalidHoursException.java (10\%) - InvalidHourlyRateException.java (10\%) - Payroll.java (25\%) - PayrollDemo.java (25\%) - Screenshot (10%)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started