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

In this assignment, you are to create a class named Payroll. In the class, you a

ID: 3782368 • Letter: I

Question

In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String   id: String     hours: int    rate: double   private members . You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception should be thrown. An employee id should have the form LLNNNN. If that form is not received, an exception should be thrown. An employee's hours should neither be negative nor greater than 84. An exception should be thrown otherwise. An employee's pay rate should neither be negative nor greater than 25.00. An exception should be thrown otherwise. Demonstrate this class in a program (separate class or in the same class). The exception messages should be appropriate to the specific exception that has occurred. Create a package payroll for the project.

Explanation / Answer

Hi,

PFB the Payroll class and tester class in payroll package. Please comment for any queries/feedbacks.

Thanks,

Anita

Payroll.java

package payroll;

public class Payroll {
   private String name;
   private String id;
   private int hours;
   private double rate;

   //no-arg constructor
   public Payroll(){
       this.name = "";
       this.id = "";
       this.hours = 0;
       this.rate = 0;
   }

   //Paremeterized constructor
   public Payroll(String name, String id, int hours, double rate) throws Exception {
       super();
       validateParameters( name, id, hours, rate);
      
       this.name = name;
       this.id = id;
       this.hours = hours;
       this.rate = rate;
   }

   //Getters ans setters
   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getId() {
       return id;
   }

   public void setId(String id) {
       this.id = id;
   }

   public int getHours() {
       return hours;
   }

   public void setHours(int hours) {
       this.hours = hours;
   }

   public double getRate() {
       return rate;
   }

   public void setRate(double rate) {
       this.rate = rate;
   }

   public void validateParameters(String name, String id, int hours, double rate) throws Exception{
       if(null== name || "".equalsIgnoreCase(name)){
           throw new Exception("An employee name should not be empty!");
       }
       if(null==id || "".equalsIgnoreCase(id) ||
               id.length()!=6){
           throw new Exception("An employee id should have the form LLNNNN!");
       }
       else{
           for(int count=0;count<id.length();count++){
               if(count>=2){
                   if (!Character.isDigit(id.charAt(count))){
                       throw new Exception("An employee id should have the form LLNNNN!");
                   }
               }
               else if (count<2){
                   if (!Character.isLetter(id.charAt(count))){
                       throw new Exception("An employee id should have the form LLNNNN!");
                   }
               }
           }
       }
       if(hours<0 || hours>84){
           throw new Exception(" An employee's hours should neither be negative nor greater than 84!");
       }
       if(rate<0 || rate >25.00){
           throw new Exception(" An employee's pay rate should neither be negative nor greater than 25.00!");
       }
       System.out.println("Payroll created!");
   }

}

PayrollTester.java

package payroll;

public class PayrollTester {
   public static void main(String [] args) throws Exception{
       Payroll payroll= new Payroll("Helen Eve","KL3456",70,14);
   }

}

Sample Output:

   Payroll payroll= new Payroll("","KL3456",70,15);
       Exception in thread "main" java.lang.Exception: An employee name should not be empty!
   Payroll payroll= new Payroll("Helen Eve","233456",70,15);
       Exception in thread "main" java.lang.Exception: An employee id should have the form LLNNNN!
   Payroll payroll= new Payroll("Helen Eve","KL3456",90,15);
       Exception in thread "main" java.lang.Exception: An employee's hours should neither be negative nor greater than 84!
   Payroll payroll= new Payroll("Helen Eve","KL3456",70,40);
       Exception in thread "main" java.lang.Exception: An employee's pay rate should neither be negative nor greater than 25.00!
              
Success:
   Payroll payroll= new Payroll("Helen Eve","KL3456",70,14);
       Payroll created!