Question

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

  1. 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 hour), and the wages per hour the employee earns (measured in dollars and cents). Hours worked and wages per hour are never allowed to fall below zero, and the class should enforce this behavior.

    The behavior of the EmployeePayroll object is that any of the above member variables can be accessed (so accessor methods need to be written for them). First name, last name and Employee ID can only be set in a constructor method - there should be no mutators given for these fields, and a special constructor should be written to set these values on initialization. The wages per hour field should have a mutator method (but keep in mind that wages cannot be allowed to be set to less than $0.0). In addition to the above, the class should provide three different ways to change the number of hours worked - on that merely sets the hours as a mutator, one that adds to the hours already in place for the employee, and one that resets the employee's hours to zero. Given the above capabilities of the employee payroll record and the skeleton below, write a Java class that can store an employee payroll record.

    public class EmployeePayroll {
      // private variables go here
    
      public EmployeePayroll( /* Parameters go here*/ ) {
        // Fill in the constructor.  Default for hours and wages is 0 
      }
    
      // write accessors as described above
    
      // write mutators as described above
    
      public void incrementHours(/*Parameters go here*/) {
        // Fill in the method.  This method should take a number of hours worked
        // as an argument and increment the total hours by that amount.
      }
    
      public void resetPayCycle(/* Parameters go here*/) {
        // Fill in the method.  This method should reset the variables to start a new week
      }
    }
    
    
  2. Override the methods toString() and equals() inherited from Object for this EmployeePayroll class. The toString() method should return the employee's name, ID, and current calculated pay (wages * hours) as a String. The equals() method should return true if two EmployeePayroll objects have the same employee ids, regardless of name, wages or hours.
  3. Write a short test program that tests the EmployeePayroll class you implemented above. Your short program should create a few new EmployeePayroll objects and test a variety of conditions, including a number that your class should not allow (such as setting a negative hourly wage).
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem including Test program. Make sure you paste both files separately. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//EmployeePayroll.java

public class EmployeePayroll {

    // private variables go

    private String firstName;

    private String lastName;

    private String employeeID; // better to make this String than int

    private double hoursWorked;

    private double wagePerHour;

    // constructor taking first,last names and employee id

    public EmployeePayroll(String first, String last, String id) {

         firstName = first;

         lastName = last;

         employeeID = id;

         // using 0 for hours and wage

         hoursWorked = 0;

         wagePerHour = 0;

    }

    // accessors as described

    public String getFirstName() {

         return firstName;

    }

    public String getLastName() {

         return lastName;

    }

    public String getEmployeeID() {

         return employeeID;

    }

    public double getHoursWorked() {

         return hoursWorked;

    }

    public double getWagePerHour() {

         return wagePerHour;

    }

    // mutators as described

    public void setHoursWorked(double hoursWorked) {

         // changing hours only if parameter is positive

         if (hoursWorked >= 0)

             this.hoursWorked = hoursWorked;

    }

    public void setWagePerHour(double wagePerHour) {

         // changing wage only if parameter is positive

         if (wagePerHour >= 0)

             this.wagePerHour = wagePerHour;

    }

    // adds some specific hours to hours worked

    public void incrementHours(double hoursWorked) {

         // changing hours only if parameter is positive

         if (hoursWorked > 0) {

             // adding parameter hoursWorked to instance variable hoursWorked

             this.hoursWorked += hoursWorked;

         }

    }

    public void resetPayCycle() {

         // clearing hours worked

         hoursWorked = 0;

    }

    // returns the pay for current pay cycle

    public double calculatePay() {

         return hoursWorked * wagePerHour;

    }

    @Override

    public String toString() {

         // returning a String containing full name, id and pay formatted to 2

         // digits after decimal

         return String.format("Name: %s, ID: %s, Pay: $%.2f", firstName + " "

                 + lastName, employeeID, calculatePay());

    }

    @Override

    public boolean equals(Object ob) {

         //assuming ob is an EmployeePayroll

         EmployeePayroll emp = (EmployeePayroll) ob;

         //returning true if both employees have same id

         return this.employeeID.equals(emp.employeeID);

    }

}

//Test.java (tester program)

public class Test {

    public static void main(String[] args) {

         //creating two EmployeePayroll objects

         EmployeePayroll emp1 = new EmployeePayroll("Oliver", "Queen",

                 "123456789112345");

         EmployeePayroll emp2 = new EmployeePayroll("Felicity", "Smoak",

                 "333453489112995");

        

         //setting 10 as hours worked, 25 as wages per hour for emp1

         emp1.setHoursWorked(10);

         emp1.setWagePerHour(25);

        

         //setting 42 as hours worked, 20 as wages per hour for emp2

         emp2.setHoursWorked(42);

         emp2.setWagePerHour(20);

        

         //displaying both employees details

         System.out.println("emp1: " + emp1);

         System.out.println("emp2: " + emp2);

        

         //displaying hours worked for emp1

         System.out.println("emp1 hours worked: " + emp1.getHoursWorked());

         //trying to change to -2 (should not be effective)

         emp1.setHoursWorked(-2);

         //displaying hours worked for emp1

         System.out.println("emp1 hours worked: " + emp1.getHoursWorked());

        

         //adding 10 hours to hours worked for emp1

         emp1.incrementHours(10);

         //displaying hours worked for emp1

         System.out.println("emp1 hours worked: " + emp1.getHoursWorked());

        

         //clearing hours worked and displaying

         emp1.resetPayCycle();

         System.out.println("emp1 hours worked: " + emp1.getHoursWorked());

        

         //similarly testing setWagePerHour on emp2

         System.out.println("emp2 wage per hour: " + emp2.getWagePerHour());

         emp2.setWagePerHour(-24);

         System.out.println("emp2 wage per hour: " + emp2.getWagePerHour());

            

         //testing equals method

         System.out.println("emp1 == emp2: "+emp1.equals(emp2));

         System.out.println("emp1 == emp1: "+emp1.equals(emp1));

        

    }

}

/*OUTPUT*/

emp1: Name: Oliver Queen, ID: 123456789112345, Pay: $250.00

emp2: Name: Felicity Smoak, ID: 333453489112995, Pay: $840.00

emp1 hours worked: 10.0

emp1 hours worked: 10.0

emp1 hours worked: 20.0

emp1 hours worked: 0.0

emp2 wage per hour: 20.0

emp2 wage per hour: 20.0

emp1 == emp2: false

emp1 == emp1: true

Add a comment
Know the answer?
Add Answer to:
Suppose you were given the job to write a Java class to manage employee payroll information...
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...

  • It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming...

    It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming Challenge 2 Employee Class.pdf Program Template: // Chapter 13, Programming Challenge 2: Employee Class #include <iostream> #include <string> using namespace std; // Employee Class Declaration class Employee { private: string name; // Employee's name int idNumber; // ID number string department; // Department name string position; // Employee's position public: // TODO: Constructors // TODO: Accessors // TODO: Mutators }; // Constructor #1 Employee::Employee(string...

  • Using C++, Write a class named Employee that has the following member variables: name. A string...

    Using C++, Write a class named Employee that has the following member variables: name. A string that holds the employee's name. idNumber. An int variable that holds the employee's ID number. department. A string that holds the name of the department where the employee works. position. A string that holds the employee's job title. The class should have the following constructors: A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee's name,...

  • C++ Lab 9A Inheritance Employee Class Create a project C2010Lab9a; add a source file Lab9a.cpp to...

    C++ Lab 9A Inheritance Employee Class Create a project C2010Lab9a; add a source file Lab9a.cpp to the project. Copy and paste the code is listed below: Design a class named Employee. The class should keep the following information in member variables: Employee name Employee number Hire date // Specification file for the Employee class #ifndef EMPLOYEE_H #define EMPLOYEE_H #include <string> using namespace std; class Employee { private:        // Declare the Employee name string variable here. // Declare the Employee...

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

  • J Inc. has a file with employee details. You are asked to write a C++ program...

    J Inc. has a file with employee details. You are asked to write a C++ program to read the file and display the results with appropriate formatting. Read from the file a person's name, SSN, and hourly wage, the number of hours worked in a week, and his or her status and store it in appropriate vector of obiects. For the output, you must display the person's name, SSN, wage, hours worked, straight time pay, overtime pay, employee status and...

  • public class EmployeeDriver public static void main(String[] args) 1/declare create an Employee object named joo. Une...

    public class EmployeeDriver public static void main(String[] args) 1/declare create an Employee object named joo. Une no-arg constructor 1/change the attributes for joe as the following: //name to Joe Cool, id to 1111111, hours to 20, pay rate to 15 //dealare & create another Employee oblegt named one using arg-constructor // The name of the new employees Jane Doeid is 2001122. She makes $10.50/hour // change the object Jane's hours to 40. 1/change the object jane's pay rate to $12.00....

  • Given attached you will find a file from Programming Challenge 5 of chapter 6 with required...

    Given attached you will find a file from Programming Challenge 5 of chapter 6 with 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 to the employee’s ID number. If you implemented this field as a string, then an empty string could be invalid. If you implemented this field as a numeric variable, then a...

  • Write a Java class named Employee to meet the requirements described in the UML Class Diagram...

    Write a Java class named Employee to meet the requirements described in the UML Class Diagram below: Note: Code added in the toString cell are not usually included in a regular UML class diagram. This was added so you can understand what I expect from you. If your code compiles cleanly, is defined correctly and functions exactly as required, the expected output of your program will solve the following problem: “An IT company with four departments and a staff strength...

  • Write a C++ program Write a class named Employee that has the following member variables: name...

    Write a C++ program Write a class named Employee that has the following member variables: name A string that holds the employee name idNumber An int to hold employee id number department A string to hold the name of the department position A string to hold the job position The class will have three constructors: 1. A constructor that accepts all the internal data such as: Employee susan("Susan Meyers", 47899, "Accounting", "Vice President"); 2. A constructor that accepts some of...

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