For phase 2 of the project, we are going to modify some of the classes so that t
ID: 3733719 • Letter: F
Question
For phase 2 of the project, we are going to modify some of the classes so that they provide additional methods and/or so that they implement the Comparable interface. We are also going to modify classes that deal with aggregation so that they make deep copies of their private fields.
In terms of aggregation, remember that a class is an aggregate class if any of its instance variables are of a type other than primitive or String. As such, we want to make sure we make deep copies of the fields in all constructors, getter and setter methods.
To make copies of the fields, you need to call the copy constructor of the appropriate class.
In the case of the ArrayList fields, you need to create new ArrayList objects and add copies of the elements in the original list, similar to how it’s done in the designingclasses2.Course class posted on my website. Note that for the listOfEmployees field in the Department class, you have to check if it’s an HourlyEmployee or a SalariedEmployee object in order to call the appropriate copy constructor.
The equals method compares two objects of the same type: the calling object and the one passed as an argument. It has one parameter whose data type is Object and it returns true if the argument is an object of the same type, is not null, and has the same value for all fields as the calling object. Otherwise, it returns false.
Include javadoc comments for all new methods added to the project.
Below is the list of modifications we are making to the project (feel free to use the solution for phase 1 that I posted in moodle).
Department class:
Modification 1: In the first constructor, make a copy of the manager and the employees parameters. When making a copy of the elements in the employees ArrayList, use the instanceof operator to make sure that the appropriate copy constructor is called.
Modification 2: In the copy constructor, make a copy of deptObj.departmentManager and deptObj.listOfEmployees. When making a copy of the elements in deptObj.listOfEmployees, use the instanceof operator to make sure that the appropriate copy constructor is called.
Modification 3: In the setDepartmentManager method, make a copy of the deptManager parameter.
Modification 4: In the setListOfEmployees method, make a copy of the employees parameters. When making a copy of the elements, use the instanceof operator to make sure that the appropriate copy constructor is called.
Modification 5: In the getDepartmentManager method, return a copy of the departmentManager instance variable.
Modification 6: In the getListOfEmployees method, return a copy of the listOfEmployees instance variable. When making a copy of the elements, use the instanceof operator to make sure that the appropriate copy constructor is called.
Modification 7: Provide an implementation for the equals method such that it compares the value of the departmentName instance variable of the two objects. In other words, two Department objects are considered equal if they have the same name.
The following is the method signature:
public boolean equals(Object obj)
{
// provide implementation
}
Modification 8: Make the class implement the Comparable interface.
Modification 9: Provide an implementation for the compareTo method such that it compares the value of the departmentName instance variable of the two objects. If the departmentName 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 returns 0.
Note:
Use the compareTo method of the String class.
The following is the method signature:
public int compareTo(Department dept)
{
// provide implementation
}
Modification 10: Write a method called addEmployee, which doesn’t return a value and has 1 parameter of type Employee. It adds a copy of the Employee parameter to the listOfEmployees instance variable.
Note:
You need to use the instanceof operator to cast the parameter into either HourlyEmployee or SalariedEmployee; very similar to how it’s done in the constructors except that you’ll be working with only one Employee object as opposed to an ArrayList.
The following is the method signature:
public void addEmployee(Employee employee)
{
// provide implementation
}
Below is the code that needs to be modified:
Department.java
package payrollsystem_phase2;
import java.util.ArrayList;
/**
* The Department class represents a department within a company. Departments have a
* list of employees and a manager.
*
* @author
*/
public class Department
{
// instance variables
private int departmentID;
private String departmentName;
private Manager departmentManager;
private ArrayList listOfEmployees = new ArrayList<>();
/**
* This constructor sets the department's id and name, as well as the list of
* employees and manager.
* @param id Department's id.
* @param name Department's name.
* @param manager Department's manager.
* @param employees Department's list of employees.
*/
public Department(int id, String name, Manager manager, ArrayList employees)
{
departmentID = id;
departmentName = name;
departmentManager = new Manager(manager);
listOfEmployees = employees;
}
/**
* 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 deptObj The object being copied.
*/
public Department(Department deptObj)
{
if( deptObj != null )
{
departmentID = deptObj.departmentID;
departmentName = deptObj.departmentName;
departmentManager = deptObj.departmentManager;
listOfEmployees = deptObj.listOfEmployees;
}
}
/**
* The getDepartmentID method returns the department's id number.
* @return The department's id.
*/
public int getDepartmentID()
{
return departmentID;
}
/**
* The getDepartmentName method returns the department's name.
* @return The department's name.
*/
public String getDepartmentName()
{
return departmentName;
}
/**
* The getDepartmentManager method returns the department's manager.
* @return The department's manager.
*/
public Manager getDepartmentManager()
{
return new Manager(departmentManager);
}
/**
* The getListOfEmployees method returns the department's list of employees.
* @return The department's list of employees as an ArrayList of Employee elements.
*/
public ArrayList getListOfEmployees()
{
return listOfEmployees;
}
/**
* The setDepartmentID method sets the id for this department.
* @param id The value to store in the id field for this department.
*/
public void setDepartmentID(int id)
{
departmentID = id;
}
/**
* The setDepartmentName method sets the name for this department.
* @param name The value to store in the name field for this department.
*/
public void setDepartmentName(String name)
{
departmentName = name;
}
/**
* The setDepartmentManager method sets the manager for this department.
* @param deptManager The value to store in the manager field for this department.
*/
public void setDepartmentManager(Manager deptManager)
{
departmentManager = new Manager(deptManager);
}
/**
* The setListOfEmployees method sets the list of employees for this department.
* @param employees The value, as an ArrayList of Employee elements, to store
* in the list of employees field for this department.
*/
public void setListOfEmployees(ArrayList employees)
{
listOfEmployees = employees;
}
/**
* The toString method returns a string containing the department's data.
* @return A String containing the Department's information: id, name,
* manager, and list of employees.
*/
@Override
public String toString()
{
String output = String.format( " %-30s %s %-30s %s %-30s %s %-30s %s",
"Department ID:", departmentID,
"Department Name:", departmentName,
"Department Manager:", departmentManager,
"List of Employees:", "");
for( Employee emp : listOfEmployees )
output += emp + " ";
output += "------------------------------------------------------------------ ";
return output;
}
}
Below is the Main Method:
PayrollSystem_Phase2.java
package payrollsystem_phase2;
import java.util.*;
public class PayrollSystem_Phase2
{
public static void main(String[] args)
{
System.out.println("/*******************************************************************************/");
System.out.println("/* Testing aggregation in the Employee class: making copies of listOfPaychecks */");
System.out.println("/*******************************************************************************/");
Paycheck paycheck_1 = new Paycheck(1, "01/01/2018", "01/07/2018", 442.50, 66.38, 0.0, 376.12);
Paycheck paycheck_2 = new Paycheck(1, "01/08/2018", "01/14/2018", 442.50, 66.38, 50.0, 426.12);
ArrayList employee1Paychecks = new ArrayList<>();
employee1Paychecks.add(paycheck_1);
employee1Paychecks.add(paycheck_2);
// Creating an HourlyEmployee object
HourlyEmployee employee_1 = new HourlyEmployee(1, "Albert", "Hernandez", employee1Paychecks, 14.75, 30);
// Creating another HourlyEmployee object using the copy constructor
HourlyEmployee employee_2 = new HourlyEmployee(employee_1);
employee_2.setEmployeeID(2);
employee_2.setFirstName("Nancy");
// Creating a third HourlyEmployee object
HourlyEmployee employee_3 = new HourlyEmployee(3, "John", "Smith", null, 20, 35);
ArrayList checks = employee_1.getListOfPaychecks();
for(Paycheck pay : checks)
pay.setEmployeeID(3);
employee_3.setListOfPaychecks(checks);
// Changing employee1Paychecks should not affect the listOfPaychecks field inside any of the employee objects
employee1Paychecks.get(0).setPeriodBeginDate("11/11/1111");
employee1Paychecks.remove(1);
// Changing the listOfPaychecks field inside employee_1 should not affect the other employees
Paycheck paycheck_3 = new Paycheck(1, "01/15/2018", "01/22/2018", 662.50, 78.38, 0.0, 584.12);
employee_1.addPaycheck(paycheck_3);
// display the employees information
System.out.println(employee_1);
System.out.println(employee_2);
System.out.println(employee_3);
System.out.println(" ");
System.out.println("/*******************************************************************************/");
System.out.println("/* Testing the equals and compareTo methods in the Employee class */");
System.out.println("/*******************************************************************************/");
System.out.println("This should return false: " + employee_1.equals(employee_2));
System.out.println("This should return any negative number: " + employee_1.compareTo(employee_2));
// change employee_1 first name to Nancy, now they have the same first and last name
employee_1.setFirstName("Nancy");
System.out.println("This should return true: " + employee_1.equals(employee_2));
System.out.println("This should return 0: " + employee_1.compareTo(employee_2));
// change employee_1 first name back to Albert
employee_1.setFirstName("Albert");
System.out.println("This should return any positive number: " + employee_3.compareTo(employee_2));
System.out.println(" ");
System.out.println("/*******************************************************************************/");
System.out.println("/* Testing the equals and compareTo methods in the Paycheck class */");
System.out.println("/*******************************************************************************/");
Paycheck paycheck_4 = new Paycheck(paycheck_2);
System.out.println("This should return true: " + paycheck_2.equals(paycheck_4));
System.out.println("This should return 0: " + paycheck_2.compareTo(paycheck_4));
paycheck_4.setPeriodBeginDate("01/22/2018");
System.out.println("This should return false: " + paycheck_2.equals(paycheck_4));
System.out.println("This should return any negative number: " + paycheck_2.compareTo(paycheck_4));
System.out.println("This should return any positive number: " + paycheck_4.compareTo(paycheck_2));
System.out.println(" ");
System.out.println("/*******************************************************************************/");
System.out.println("/* Testing aggregation in the Department class: making copies of */");
System.out.println("/* departmentManager and listOfEmployees */");
System.out.println("/*******************************************************************************/");
Paycheck paycheck_5 = new Paycheck(4, "01/15/2018", "01/21/2018", 1288.45, 257.70, 80.0, 1110.75);
ArrayList michaelsPaychecks = new ArrayList<>();
michaelsPaychecks.add(paycheck_5);
Manager manager_1 = new Manager(4, "Michael", "Colt", michaelsPaychecks, 67000, 50);
ArrayList dept1Employees = new ArrayList<>();
dept1Employees.add(employee_1);
dept1Employees.add(employee_2);
// Create a Department object
Department dept_1 = new Department(1, "Human Resources", manager_1, dept1Employees);
// Create another Department object
Department dept_2 = new Department(dept_1);
dept_2.setDepartmentID(2);
dept_2.setDepartmentName("Information Technology");
Paycheck paycheck_6 = new Paycheck(5, "01/15/2018", "01/21/2018", 1250.0, 250.0, 72.0, 1072.0);
ArrayList luisasPaychecks = new ArrayList<>();
luisasPaychecks.add(paycheck_6);
Manager manager_2 = new Manager(5, "Luisa", "Lopez", luisasPaychecks, 65000, 90);
dept_2.setDepartmentManager(manager_2);
ArrayList dept2Employees = new ArrayList<>();
dept2Employees.add(employee_3);
dept_2.setListOfEmployees(dept2Employees);
dept_2.addEmployee(new SalariedEmployee(6, "Joe", "Brown", null, 45400));
// Changing manager_1, employee_3, dept1Employees, and dept2Employees should not affect the data inside the departments
manager_1.setFirstName("Blah");
manager_1.setLastName("Blah");
employee_3.setPeriodHours(11);
dept1Employees.clear();
dept2Employees.add(employee_2);
// display the departments information
System.out.println(dept_1);
System.out.println(" ");
System.out.println(dept_2);
System.out.println(" ");
System.out.println("/*******************************************************************************/");
System.out.println("/* Testing the equals and compareTo methods in the Department class */");
System.out.println("/*******************************************************************************/");
System.out.println("This should return false: " + dept_1.equals(dept_2));
System.out.println("This should return a negative number: " + dept_1.compareTo(dept_2));
System.out.println("This should return a positive number: " + dept_2.compareTo(dept_1));
dept_1.setDepartmentName("Information Technology");
System.out.println("This should return true: " + dept_1.equals(dept_2));
System.out.println("This should return 0: " + dept_2.compareTo(dept_1));
dept_1.setDepartmentName("Human Resources");
System.out.println(" ");
System.out.println("/********************************************************************************/");
System.out.println("/* Testing Aggregation in the Company class: making copies of listOfDepartments */");
System.out.println("/********************************************************************************/");
// create an ArrayList of Departments
ArrayList departments = new ArrayList<>();
departments.add(dept_1);
// Creating a Company object.
Company company = new Company("Our Company", departments);
company.addDepartment(dept_2);
SalariedEmployee employee_7 = new SalariedEmployee(7, "Daniel", "Davis", null, 49400);
company.addEmployeeToDepartment(2, employee_7);
Manager manager_3 = new Manager(8, "Robert", "Porter", null, 65000, 100);
company.setDepartmentManager(2, manager_3);
// Display the company information
System.out.println(company.toString());
}
}
Explanation / Answer
Please find the code above.. i have included the code as directed in instructions. For any doubts, please ask in comments.. If the answer helps, please upvote. Thanks!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.