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

Design a PayRoll class that has data members for an employee’s first and last na

ID: 3802372 • Letter: D

Question

Design a PayRoll class that has data members for an employee’s first and last name, hourly pay rate, number of hours worked. The default constructor will set the first and last name to empty string, and hours worked and pay rate to zero. The class must have mutator functions to set each of the data members of the class and accessors to access them. The class should also have the following member functions:
displayPayroll function that displays all data members of the a PayRoll instance on one line separated by blanks readPayRoll function that reads data members of a PayRoll instance from an input stream associated with a text file (assume all data members are on one line separated by commas) getGross function that will return a double calculated by multiplying the hours worked by the pay rate Design a PayRoll class that has data members for an employee’s first and last name, hourly pay rate, number of hours worked. The default constructor will set the first and last name to empty string, and hours worked and pay rate to zero. The class must have mutator functions to set each of the data members of the class and accessors to access them. The class should also have the following member functions:
displayPayroll function that displays all data members of the a PayRoll instance on one line separated by blanks readPayRoll function that reads data members of a PayRoll instance from an input stream associated with a text file (assume all data members are on one line separated by commas) getGross function that will return a double calculated by multiplying the hours worked by the pay rate Design a PayRoll class that has data members for an employee’s first and last name, hourly pay rate, number of hours worked. The default constructor will set the first and last name to empty string, and hours worked and pay rate to zero. The class must have mutator functions to set each of the data members of the class and accessors to access them. The class should also have the following member functions:
displayPayroll function that displays all data members of the a PayRoll instance on one line separated by blanks readPayRoll function that reads data members of a PayRoll instance from an input stream associated with a text file (assume all data members are on one line separated by commas) getGross function that will return a double calculated by multiplying the hours worked by the pay rate

Explanation / Answer

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class PayRoll {
  
   private String employeeFirstName;
   private String employeeLastName;
   private float hourlyPayRate;
   private float numberOfHoursWorked;
  
   PayRoll()
   {
       this.employeeFirstName="";
       this.employeeLastName="";
       this.hourlyPayRate=0;
       this.numberOfHoursWorked=0;
   }
  
  

   public String getEmployeeFirstName() {
       return employeeFirstName;
   }

   public void setEmployeeFirstName(String employeeFirstName) {
       this.employeeFirstName = employeeFirstName;
   }

   public String getEmployeeLastName() {
       return employeeLastName;
   }

   public void setEmployeeLastName(String employeeLastName) {
       this.employeeLastName = employeeLastName;
   }

   public float getHourlyPayRate() {
       return hourlyPayRate;
   }

   public void setHourlyPayRate(float hourlyPayRate) {
       this.hourlyPayRate = hourlyPayRate;
   }

   public float getNumberOfHoursWorked() {
       return numberOfHoursWorked;
   }

   public void setNumberOfHoursWorked(float numberOfHoursWorked) {
       this.numberOfHoursWorked = numberOfHoursWorked;
   }
  
   public void displayPayroll()
   {
       System.out.println(this.employeeFirstName+" "+this.employeeLastName+" "+this.getHourlyPayRate()+" "+this.getNumberOfHoursWorked());
   }
  
   public void readPayRoll(String filePath) throws IOException
   {
       InputStream in = new FileInputStream(new File(filePath));
BufferedReader reader = new BufferedReader(new InputStreamReader(in));

String line;
String payRollTokens[];
  
  
if((line = reader.readLine()) != null) {
  
   payRollTokens=line.split(",");
   this.setEmployeeFirstName(payRollTokens[0]);
   this.setEmployeeLastName(payRollTokens[1]);
   this.setHourlyPayRate(Float.parseFloat(payRollTokens[2]));
   this.setNumberOfHoursWorked(Float.parseFloat(payRollTokens[3]));
  
}
  
reader.close();
      
      
      
   }

   public double gross()
   {
       return this.getHourlyPayRate()*this.getNumberOfHoursWorked();
   }

   public static void main(String[] args) {
      
      
       PayRoll p= new PayRoll();
      
       p.displayPayroll();
      
       PayRoll q= new PayRoll();
      
       String filePath = "C:\vhost\testdata.txt";
       try {
           q.readPayRoll(filePath);
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       q.displayPayroll();
      
       Double gross=q.gross();
      
       System.out.println("Gross:"+gross);
      

   }

}