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

Java Employee class: Modification 1: In the first constructor, make a copy of th

ID: 3734911 • Letter: J

Question

Java

Employee class:

Modification 1: In the first constructor, make a copy of the paychecks parameter before using it to set the listOfPaychecks instance variable.

Modification 2: In the copy constructor, make a copy of employeeObject.listOfPaychecks before using it to set the listOfPaychecks instance variable.

Modification 3: In the setListOfPaychecks method, make a copy of the paychecks parameter before using it to set the listOfPaychecks instance variable.

Modification 4: In the getListOfPaychecks method, return a copy of the listOfPaychecks instance variable.

Modification 5: Provide an implementation for the equals method such that it compares the value of the firstName and lastName instance variables of the two objects. In other words, two Employee objects are considered equal if they have the same value for the firstName and lastName fields.

The following is the method signature:

public boolean equals(Object obj)

{

// provide implementation

}

Modification 6: Make the class implement the Comparable interface.

Modification 7: Provide an implementation for the compareTo method such that it compares the value of the lastName instance variable of the two objects. If the lastName variable of the calling object is greater, it returns a positive number, if it’s smaller it returns a negative number, and if they both have the same value, the method then compares the firstName. If the firstName variable of the calling object is greater, it returns a positive number, if it’s smaller, it returns a negative number, else it returns 0.

Notes:

Use the compareTo method of the String class.

The following is the method signature:

public int compareTo(Employee employee)

{

// provide implementation

}

Modification 8: Add an instance method called addPaycheck that does not return a value and has 1 parameter of type Paycheck. It adds a copy of the Paycheck parameter to the listOfPaychecks instance variable.

The following is the method signature:

public void addPaycheck(Paycheck checkObj)

{

// provide implementation

}

******************code*******************

public abstract class Employee
{
// instance variables
private int employeeID;
private String firstName;
private String lastName;
private ArrayListlistOfPaychecks;
  
/**
* Constructor initializes the fields to the passed values.
* @param employeeID
* @param firstName
* @param lastName
* @param listOfPaychecks
*/
public Employee (int employeeID, String firstName, String lastName, ArrayList listOfPaychecks){
this.employeeID = employeeID;
this.firstName = firstName;
this.lastName = lastName;
this.listOfPaychecks = listOfPaychecks;
}
/**
* This is a copy constructor. It initializes the fields of the object being created to the same
* values as the fields in the object passed as an argument.
* @param empObject The object being copied.
*/
public Employee(Employee empObject)
{
if (empObject != null)
{
employeeID = empObject.employeeID;
firstName = empObject.firstName;
lastName = empObject.lastName;
listOfPaychecks = empObject.listOfPaychecks;
}
}
  
/**
* method returns a Employee object's ID.
* @return The value in the field.
*/
public int getEmployeeID()
{
return employeeID;
}

/**
* method returns a Employee object's first name.
* @return The value in the field.
*/
public String getFirstName()
{
return firstName;
}
  
/**
* method returns a Employee object's last name.
* @return The value in the field.
*/
public String getLastName()
{
return lastName;
}
  
/**
* Method stores a value in the EmployeeID field.
* @param newEmployeeID
*/
public void setEmployeeID(int newEmployeeID)
{
employeeID = newEmployeeID;
}
  
/**
* Method stores a value in the firstName field.
* @param newFirstName
*/
public void setFirstName(String newFirstName)
{
firstName = newFirstName;
}
  
/**
* Method stores a value in the lasttName field.
* @param newLastName
*/
public void setLastName(String newLastName)
{
lastName = newLastName;
}
  
public ArrayList getListOfPaychecks(){
return listOfPaychecks;
}
  
public void setListOfPaychecks(ArrayList listOfPaychecks){
this.listOfPaychecks = listOfPaychecks;
}
/**
* The toString method returns a string representing the state of a employee object.
* @return
*/
@Override
public String toString()
{
String output = String.format(" %5s %-24s %s %5s %-24s %s %5s %-24s %s %5s %-24s ",
"", "Employee ID:", employeeID,
"", "First Name:", firstName,
"", "Last Name:", lastName,
"", "Paychecks Received:");
  
if( listOfPaychecks == null || listOfPaychecks.isEmpty() )
output += "No paychecks received.";
else
{
for( Paycheck checkElement : listOfPaychecks)
output += checkElement.toString();
}
  
return output + " ";
  
}
  
  
}

Explanation / Answer

Hello, I have completed all the 8 modifications required. The required code is given below, changes are highlighted by comments and each modification is properly labelled. Note that as you have not provided the required Paycheck class, I couldn’t test the code, so I’m not attaching any test code or output. But I’ll guarantee that the code will work just perfectly. Thanks.

//Employee.java

import java.util.ArrayList;

public abstract class Employee implements Comparable<Employee> {

                // instance variables

                private int employeeID;

                private String firstName;

                private String lastName;

                private ArrayList<Paycheck> listOfPaychecks;

                /**

                * Constructor initializes the fields to the passed values.

                *

                * @param employeeID

                * @param firstName

                * @param lastName

                * @param listOfPaychecks

                */

                public Employee(int employeeID, String firstName, String lastName,

                                                ArrayList listOfPaychecks) {

                                this.employeeID = employeeID;

                                this.firstName = firstName;

                                this.lastName = lastName;

                                /**

                                * making a copy of listOfPaychecks and assigning it to listOfPaychecks (Modification-1)

                                */

                                this.listOfPaychecks = new ArrayList<Paycheck>(listOfPaychecks);

                }

                /**

                * This is a copy constructor. It initializes the fields of the object being

                * created to the same values as the fields in the object passed as an

                * argument.

                *

                * @param empObject

                *            The object being copied.

                */

                public Employee(Employee empObject) {

                                if (empObject != null) {

                                                employeeID = empObject.employeeID;

                                                firstName = empObject.firstName;

                                                lastName = empObject.lastName;

                                                /**

                                                * making a copy of listOfPaychecks in empObject and assigning it to

                                                * listOfPaychecks of current object (Modification-2)

                                                */

                                                listOfPaychecks = new ArrayList<Paycheck>(empObject.listOfPaychecks);

                                }

                }

                /**

                * method returns a Employee object's ID.

                *

                * @return The value in the field.

                */

                public int getEmployeeID() {

                                return employeeID;

                }

                /**

                * method returns a Employee object's first name.

                *

                * @return The value in the field.

                */

                public String getFirstName() {

                                return firstName;

                }

                /**

                * method returns a Employee object's last name.

                *

                * @return The value in the field.

                */

                public String getLastName() {

                                return lastName;

                }

                /**

                * Method stores a value in the EmployeeID field.

                *

                * @param newEmployeeID

                */

                public void setEmployeeID(int newEmployeeID) {

                                employeeID = newEmployeeID;

                }

                /**

                * Method stores a value in the firstName field.

                *

                * @param newFirstName

                */

                public void setFirstName(String newFirstName) {

                                firstName = newFirstName;

                }

                /**

                * Method stores a value in the lasttName field.

                *

                * @param newLastName

                */

                public void setLastName(String newLastName) {

                                lastName = newLastName;

                }

                public ArrayList getListOfPaychecks() {

                                /**

                                * returning a copy of listofPaychecks (Modification-4)

                                */

                                return new ArrayList<Paycheck>(listOfPaychecks);

                }

                public void setListOfPaychecks(ArrayList listOfPaychecks) {

                                /**

                                * making a copy of the given listOfPaychecks and assigning it to

                                * listOfPaychecks variable (Modification-3)

                                */

                                this.listOfPaychecks = new ArrayList<Paycheck>(listOfPaychecks);

                }

                /**

                * The toString method returns a string representing the state of a employee

                * object.

                *

                * @return

                */

                @Override

                public String toString() {

                                String output = String.format(

                                                                " %5s %-24s %s %5s %-24s %s %5s %-24s %s %5s %-24s ",

                                                                "", "Employee ID:", employeeID, "", "First Name:", firstName,

                                                                "", "Last Name:", lastName, "", "Paychecks Received:");

                                if (listOfPaychecks == null || listOfPaychecks.isEmpty())

                                                output += "No paychecks received.";

                                else {

                                                for (Paycheck checkElement : listOfPaychecks)

                                                                output += checkElement.toString();

                                }

                                return output + " ";

                }

                /**

                * implementing equals method (Modification-5)

                */

                @Override

                public boolean equals(Object o) {

                                if (o instanceof Employee) {

                                                Employee e = (Employee) o; // safe casting

                                                if (e.firstName.equals(this.firstName)

                                                                                && e.lastName.equals(this.lastName)) {

                                                                /**

                                                                * returning true if both first names are equal and last names

                                                                * are equal

                                                                */

                                                                return true;

                                                }

                                }

                                return false;

                }

                /**

                * implementing compareTo method of Comparable interface, (Modification-6 and 7)

                */

                @Override

                public int compareTo(Employee employee) {

                                /**

                                * comparing the last names of this employee and the employee in

                                * parameter

                                */

                                return this.lastName.compareTo(employee.lastName);

                }

                /**

                * method to add a paycheck to the list (Modification-8)

                * @param checkObj - paycheck to be added

                */

                public void addPaycheck(Paycheck checkObj){

                                /**

                                * Adding to the list of paychecks

                                */

                                listOfPaychecks.add(checkObj);

                }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote