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 a class Employee. Your Employee class should include the following attributes: First name (string) Last...

    Create a 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 another class HourlyEmployee that inherits from the Employee class. HourEmployee must use the inherited parent class variables and add in HourlyRate and HoursWorked. Your HourEmployee class should contain a constructor that calls the constructor from the...

  • Suppose you were given the job to write a Java class to manage employee payroll information...

    Suppose you were given the job to write a Java class to manage employee payroll information in an HR management system. Each employee will be modeled as a single EmployeePayroll object (much like the Student objects discussed in class). The state of the EmployeePayroll object includes the first name and last name of the employee, a unique 15 digit employee number for each employee, a number of hours that they've worked this week (which could be a fraction of an...

  • Within c++ Create a simple Employee class with name, department, and title. Create an hourlyEmployee class...

    Within c++ Create a simple Employee class with name, department, and title. Create an hourlyEmployee class (which inherits from the Employee class) for a basic payroll program to compute the net pay salary of hourly based employees. Your program should also find the average net pay for a small company. To define the class, include the appropriate data members, member functions, constructors, and access modifiers. For simplicity, use a constant tax rate of 30% to compute the tax amount. Employees...

  • Design a Ship class that has the following class members: A field for the name of the ship...

    Design a Ship class that has the following class members: A field for the name of the ship A field for the year the ship was built A constructor Appropriate accessors and mutators toString method that displays the ships name and the year it was built Design a CruiseShip class the inherits from the Ship class. Include the following members: A field for the max number of passengers A constructor Appropriate accessors and mutators toString method that overrides the Ship toString method. The method...

  • 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...

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