Question

Design a PayRoll class that has data members for an employee’s first and last name, hourly...

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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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+"\t"+this.employeeLastName+"\t"+this.getHourlyPayRate()+"\t"+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);
      

   }

}

Add a comment
Know the answer?
Add Answer to:
Design a PayRoll class that has data members for an employee’s first and last name, hourly...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design a Payroll class with the following fields:

    IN JAVADesign a Payroll class with the following fields:• name: a String containing the employee's name• idNumber: an int representing the employee's ID number• rate: a double containing the employee's hourly pay rate• hours: an int representing the number of hours this employee has workedThe class should also have the following methods:• Constructor: takes the employee's name and ID number as arguments• Accessors: allow access to all of the fields of the Payroll class• Mutators: let the user assign values...

  • Develop a WPF application that has a button to calculate an employee’s weekly pay, given the...

    Develop a WPF application that has a button to calculate an employee’s weekly pay, given the number of hours worked. An employee should have a first name, last name, age, and hourly consulting rate. You should be able to create an employee object and provide the hours worked to calculate the weekly pay. The application assumes a standard workweek of 40 hours. Any hours worked over 40 hours in a week are considered overtime and earn time and a half....

  • in c++ Define and implement the class Employee with the following requirements: private data members string...

    in c++ Define and implement the class Employee with the following requirements: private data members string type name a. b. double type hourlyRate 2. public member functions a. default constructor that sets the data member name to blank"and hourlyRate to zero b. A constructor with parameters to initialize the private data members c. Set and get methods for all private data members d. A constant method weeklyPay() that receives a parameter representing the number of hours the employee worked per...

  • Create an abstract class Employee. Your Employee class should include the following attributes: First name (string)...

    Create an abstract class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create an abstract method called earnings.      Create another class HourlyEmployee that inherits from the abstract Employee class.   HourEmployee must use the inherited parent class variables and add in attributes HourlyRate and HoursWorked. Your HourEmployee class should...

  • Create an abstract class Employee. Your Employee class should include the following attributes: First name (string)...

    Create an abstract class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create an abstract method called earnings.      Create another class HourlyEmployee that inherits from the abstract Employee class.   HourEmployee must use the inherited parent class variables and add in attributes HourlyRate and HoursWorked. Your HourEmployee class should...

  • java Payroll class Exceptions Programming Challenge 5 of Chapter 6 required you to write a Payroll...

    java Payroll class Exceptions Programming Challenge 5 of Chapter 6 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 would be invalid. If you implemented this field as a numeric variable, then a negative number or...

  • Design a class named EmployeeRecord that holds an employee’s ID number, name, and payrate. Include mutator...

    Design a class named EmployeeRecord that holds an employee’s ID number, name, and payrate. Include mutator methods to set the values for each data field and output the values for each data field. Create the class diagram and write the code that defines the class and implements the mutator methods. need help please with this question

  • Python 3 Question Please keep the code introductory friendly and include comments. Many thanks. Prompt: The...

    Python 3 Question Please keep the code introductory friendly and include comments. Many thanks. Prompt: The owners of the Annan Supermarket would like to have a program that computes the weekly gross pay of their employees. The user will enter an employee’s first name, last name, the hourly rate of pay, and the number of hours worked for the week. In addition, Annan Supermarkets would like the program to compute the employee’s net pay and overtime pay. Overtime hours, any...

  • [JAVA] Program: Design a Ship class that the following members: A field for the name of...

    [JAVA] Program: Design a Ship class that the following members: A field for the name of the ship (a string) o A field for the year the the ship was built (a string) o A constructor and appropriate accessors and mutators A toString method that displays the ship's name and the year it was built Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers...

  • Design a program(Creating a RAPTOR flowchart) that will read a file of employee records containing employee...

    Design a program(Creating a RAPTOR flowchart) that will read a file of employee records containing employee number, employee name, hourly pay rate, regular hours worked and overtime hours worked. The company pays its employees weekly, according to the following rules: regular pay = regular hours worked × hourly rate of pay overtime pay = overtime hours worked × hourly rate of pay × 1.5 total pay = regular pay + overtime pay Your program is to read the input data...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT