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 hourly pay rate

ID: 3694515 • Letter: D

Question

Design a PayRoll class that has data members for an employee's hourly pay rate and number of hours worked. Write a program with an array of seven PayRoll objects. The program should read the number of hours each employee worked and their hourly pay rate from a file and call class functions to store this information in the appropriate objects. It should then call a class function, once for each object, to return the employee's gross pay, so this information can be displayed. Sample data to test this program can be found in the payroll dat file. Create a file called payroll.dat file that contains the following data

Explanation / Answer

Please find the required solution:

import java.util.Scanner;

//payroll class
public class PayRoll {

   // instance variables
   double hourly_rate;
   double number_of_hours;

   // setters and getters
   public double getHourly_rate() {
       return hourly_rate;
   }

   public void setHourly_rate(double hourly_rate) {
       this.hourly_rate = hourly_rate;
   }

   public double getNumber_of_hours() {
       return number_of_hours;
   }

   public void setNumber_of_hours(double number_of_hours) {
       this.number_of_hours = number_of_hours;
   }

   // main method to test
   public static void main(String[] args) {
       // read file
       Scanner fileInput = new Scanner("payroll.dat");
       // while file has more data
       while (fileInput.hasNext()) {

           // create pay roll object
           PayRoll payroll = new PayRoll();
           double hourly_rate = 0;
           double number_of_hours = 0;
           if (fileInput.hasNext())
               hourly_rate = fileInput.nextDouble();
           if (fileInput.hasNext())
               number_of_hours = fileInput.nextDouble();
           payroll.setHourly_rate(hourly_rate);
           payroll.setNumber_of_hours(number_of_hours);
       }
       fileInput.close();
   }
}