java Modification 8: Add an instance method called addPaycheck that does not ret
ID: 3734843 • Letter: J
Question
java
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
Employee.java
import java.util.ArrayList;
public abstract class Employee
{
// instance variables
private int employeeID;
private String firstName;
private String lastName;
private ArrayList 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;
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 + " ";
}
public void addPaycheck(Paycheck checkObj) {
// provide implementation
listOfPaychecks.add(checkObj);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.