Programming Challenge 5 of chapter 6 with required you to write a Payroll class
ID: 3873771 • Letter: P
Question
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 for 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 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;
}
}
Explanation / Answer
Payroll.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package payrolldemo;
public class Payroll extends MyException
{
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) throws MyException
{
setName(n);
setIdNumber(i);
}
/**
The setName sets the employee's name.
@param n The employee's name.
*/
public void setName(String n)throws MyException
{
name = n;
if(name == null)
{
throw new MyException("Enter name please...",1);
}
}
/**
The setIdNumber sets the employee's ID number.
@param i The employee's ID number.
*/
public void setIdNumber(int i)throws MyException
{
idNumber = i;
if(idNumber > 1 || idNumber < 1000)
throw new MyException("Please enter valid id......",2);
}
/**
The setPayRate sets the employee's pay rate.
@param p The employee's pay rate.
*/
public void setPayRate(double p)throws MyException
{
payRate = p;
if(payRate < 1 || payRate > 25)
throw new MyException("Please enter valid pay rates......",3);
}
/**
The setHoursWorked sets the number of hours worked.
@param h The number of hours worked.
*/
public void setHoursWorked(double h)throws MyException
{
hoursWorked = h;
if(hoursWorked < 1 || hoursWorked > 84)
throw new MyException("Please enter valid number of hours worked",4);
}
/**
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;
}
}
MyException.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package payrolldemo;
public class MyException extends Exception {
public MyException(String message,int i)
{
super(message);
}
}
PayrollDemo.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package payrolldemo;
import java.io.IOException;
import java.util.Scanner;
/**
This program demonstrates a solution to the
Payroll Class programming challenge.
*/
public class PayrollDemo extends MyException
{
public static void main(String[] args) throws MyException
{
// 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());
}
public PayrollDemo(String message,int id) {
super(message,id);
}
}
Rate an upvote!!! thankyou
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.