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

Given attached you will find a file from Programming Challenge 5 of chapter 6 wi

ID: 3735157 • Letter: G

Question

Given attached you will find a file from 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 to 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 occur. Demonstrate the exception classes in a program.

Make sure the program should run both internally and externally.

/**
   Payroll class
   Chapter 6, Programming Challenge 5
*/

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)
   {
name = n;
idNumber = 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

Please find my implementation.

Please let me know in case of any issue.

Payroll.java

package org.students;

/**
Payroll class
Chapter 6, Programming Challenge 5
*/

public class Payroll
{
   int count=0;
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.
@param i The employee's ID number.
*/
public Payroll(String name, int idNumber, double payRate, double hoursWorked) {
   super();
  
if(name.length()==0 || name=="")
   {
   try {
       throw new PayrollException(":: EMPLOYEE NAME SHOULD NOT BE EMPTY ::");
   } catch (PayrollException e) {
       System.out.println("* PAYROLL CLASS OBJECT FAILED TO CREATE *");
       System.out.println("---------------------------------------------");
   }
  
   }
else if(idNumber<=0)
{
   try {
       throw new PayrollException(":: EMPLOYEE NO MUST BE GREATER THAN 0 ::");
   } catch (PayrollException e) {
       System.out.println("* PAYROLL CLASS OBJECT FAILED TO CREATE *");
       System.out.println("---------------------------------------------");
   }  
}
else if(hoursWorked<0 || hoursWorked>84)
{
   try {
       throw new PayrollException(":: NO OF HOURS MUST BE BETWEEN 0 AND 84 ::");
   } catch (PayrollException e) {
       System.out.println("* PAYROLL CLASS OBJECT FAILED TO CREATE *");
       System.out.println("---------------------------------------------");
   }  
}
  
else if(payRate<0 || payRate >25)
{
   try {
       throw new PayrollException(":: PAYRATE MUST BE BETWEEN 0 AND 25 ::");
   } catch (PayrollException e) {
       System.out.println("* PAYROLL CLASS OBJECT FAILED TO CREATE *");
       System.out.println("---------------------------------------------");
   }  
}
  
else {
   this.name = name;
   this.idNumber = idNumber;
   this.payRate = payRate;
   this.hoursWorked = hoursWorked;
   count=1;
   System.out.println("::PAYROLL OBJECT CREATED SUCCESSFULLY ::");
}
}

/**
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;
}

@Override
public String toString() {
   return "Payroll [name=" + name + ", idNumber=" + idNumber + ", payRate="
           + payRate + ", hoursWorked=" + hoursWorked + ", Gross Pay="
           + getGrossPay() + "]";
}

}  

_____________________________________________________________________________________________

PayrollException.java

package org.students;

public class PayrollException extends Throwable {

   public PayrollException(String message)
   {
       System.out.println(message);
       //System.out.println("== AppartmentException constructor ==");
   }
}

_____________________________________________________________________________________________

PayrollTest.java

package org.students;

import java.util.Scanner;

public class PayrollTest {

  
   public static void main(String[] args) {
       Scanner sc1=new Scanner(System.in);
       Scanner sc=new Scanner(System.in);
      
   while(true)
   {
       System.out.print("Enter the Name of an Employee ::");
       String name=sc.nextLine();
       System.out.print("Enter the Employee ID ::");
       int id=sc1.nextInt();
       System.out.print("No of Hours Employee Worked ::");
       int noOfHours=sc1.nextInt();
       System.out.print("Hourly Pay Rate for an Employee ::");
       int payRate=sc1.nextInt();
       Payroll pr=new Payroll(name,id,noOfHours,payRate);
      
if(pr.count==0)
{continue;}
else
{
   System.out.println(pr.toString());
   System.out.println("* PROGRAM EXIT *");break;}
   }
  
  
  

   }

}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

output:

Enter the Name of an Employee ::
Enter the Employee ID ::45
No of Hours Employee Worked ::4
Hourly Pay Rate for an Employee ::3
:: EMPLOYEE NAME SHOULD NOT BE EMPTY ::
* PAYROLL CLASS OBJECT FAILED TO CREATE *
---------------------------------------------
Enter the Name of an Employee ::williams
Enter the Employee ID ::-1
No of Hours Employee Worked ::5
Hourly Pay Rate for an Employee ::8
:: EMPLOYEE NO MUST BE GREATER THAN 0 ::
* PAYROLL CLASS OBJECT FAILED TO CREATE *
---------------------------------------------
Enter the Name of an Employee ::williams
Enter the Employee ID ::8
No of Hours Employee Worked ::-1
Hourly Pay Rate for an Employee ::9
:: PAYRATE MUST BE BETWEEN 0 AND 25 ::
* PAYROLL CLASS OBJECT FAILED TO CREATE *
---------------------------------------------
Enter the Name of an Employee ::williams
Enter the Employee ID ::8
No of Hours Employee Worked ::24
Hourly Pay Rate for an Employee ::-1
:: NO OF HOURS MUST BE BETWEEN 0 AND 84 ::
* PAYROLL CLASS OBJECT FAILED TO CREATE *
---------------------------------------------
Enter the Name of an Employee ::williams
Enter the Employee ID ::8
No of Hours Employee Worked ::24
Hourly Pay Rate for an Employee ::80
::PAYROLL OBJECT CREATED SUCCESSFULLY ::
Payroll [name=williams, idNumber=8, payRate=24.0, hoursWorked=80.0, Gross Pay=1920.0]
* PROGRAM EXIT *

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