1

So I have done everything the task requires in the code below.

The problem I am have is about the

  1. Defense copy's of a Object
  2. Change the values in the Employee object.

For 2

I change the values but I call new which is creating a new instance of the object.

employee1 = new Employee("626347B", "Sam O'Conor", 24000);

This way the problem works as the second time I call mainDepartment.display();the values are the same.

But this does not feel like proper encapsulation as i am creating a new object.

I was thinking of

employee1.setName("Conor Bryan);

Is how you test for encapsulation, now when I call mainDepartment.display(); the name value does change so the question is wrong.

!-----Question-----!

Display the Employee details

Display the department details

Change the values in the Employee object.

Display the Department details again (The information should not change)

Again display the Employee details (The information should be changed here).

!----Test----!

package question1;

public class Test {

    public static void main(String[] args) {
        //Creating a instance of both Employee and Department
        Employee employee1 = new Employee("2726354E", "Bob Ings", 30000 );

        //Updated Code for Department to take a copy of Employee 
        Employee copy = new Employee("2726354E", "Bob Ings", 30000 );

        Department mainDepartment = new Department("Main Floor", copy);

        //Displaying both instances of Employee and Department
        employee1.display();    
        mainDepartment.display();

        System.out.println("");     


        //Changing values in constructor for the instance of Employee we made earlier on 
        employee1 = new Employee("626347B", "Sam O'Conor", 24000);

        mainDepartment.display();

        System.out.println("");     
        System.out.println("");

        employee1.display();

    }

}

!----Department----!

package question1;

public class Department {
    private String deptName;
    private Employee employee;
    private int officeNumber;

    //Constructor with all three parameters 
    public Department(String deptName, Employee employee, int officeNumber) {

        this.deptName = deptName;
        this.employee = employee;
        this.officeNumber = officeNumber;
    }

    //Constructor with the officeNumber set to 0
    public Department(String deptName, Employee employee) {

        this.deptName = deptName;
        this.employee = employee;
        this.officeNumber = 0;
    }

    //Displaying the instance of the object information in a anesthetically pleasing manner
    public void display() {
        System.out.println("Department");
        Employee.seperationLine();
        System.out.println("Department Name: " + getDeptName());
        Employee.seperationLine();
        System.out.println("Employee: " + employee.toString());
        Employee.seperationLine();
        System.out.println("Office Number: " + getOfficeNumber());

    }



    //Getters and Setters
    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public int getOfficeNumber() {
        return officeNumber;
    }

    public void setOfficeNumber(int officeNumber) {
        this.officeNumber = officeNumber;
    }


}

!----Employee----!

 package question1;

public class Employee {
    private String ppsNum;
    private String name;
    private double salary;

    public Employee() {}

    //Parameterized constructor 
    public Employee(String ppsNum, String name, double salary) {
        this.ppsNum = ppsNum;
        this.name = name;
        this.salary = salary;
    }

    //Defensive copy constructor
    public Employee(Employee copy) {
        this.ppsNum = copy.ppsNum;
        this.name = copy.name;
        this.salary = copy.salary;
    }

    //Displaying the instance of the object information in a anesthetically pleasing manner
    public void display() {
        System.out.println("Employee Information");
        seperationLine();
        System.out.println("Name: " + getName());
        seperationLine();
        System.out.println("PPS number: " + getPpsNum());
        seperationLine();
        System.out.println("Salary: " + getSalary() + "0");
        seperationLine();
        System.out.println("\n");


    }

    public String toString() {
        return "[ppsNum=" + ppsNum + ", name=" + name + ", salary=" + salary + "]";
    }

    //Getters and Setters 
    public String getPpsNum() {
        return ppsNum;
    }

    public void setPpsNum(String ppsNum) {
        this.ppsNum = ppsNum;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public static void seperationLine() {
        System.out.println("------------------------");
    }






}
Liam
  • 568
  • 2
  • 15
  • I think your definition of the word `encapsulation` is making you think too hard... Encapsulation in Object Oriented Programming is basically the idea of encapsulating (hiding) multiple pieces of data in a single reference.... In other words you have an employee reference `Employee emp = new Employee("name", age)` that you can refer to with one reference `emp`, but it does not point to one piece of data, it points to many (the other data is encapsulated in/by the object) – RobOhRob Oct 18 '19 at 15:04

1 Answers1

1
employee1 = new Employee("626347B", "Sam O'Conor", 24000);

is not changing the values in an Employee object, it is creating a new Employee object.

I was thinking of

employee1.setName("Conor Bryan);

Yes, this is the correct way to change a value in an object. Do that instead of creating a new object, then display the result and you should see that the value of employee1 changed.

In order to fulfill the second requirement (that Department doesn't change) you need to make a copy of the Employee when you pass it to Department.

//Constructor with all three parameters 
public Department(String deptName, Employee employee, int officeNumber) {

    this.deptName = deptName;
    this.employee = employee;  // make a copy here
    this.officeNumber = officeNumber;
}

The easiest way to do this is probably to implement a copy constructor in your Employee class, then use that in Department. You'd need to make a copy anywhere you set the employee attribute in Department, so in both of your constructors and in the setEmployee method.

Community
  • 1
  • 1
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • This is what I though, but in the question, when I set `employee1.setName("Conor Bryan);` and then call the `mainDepartment.display();` the second time the values do change – Liam Oct 18 '19 at 12:42
  • In the question it says they should not chnage – Liam Oct 18 '19 at 12:42
  • `If this is done correctly Department is a fully encapsulated Class as the attributes cannot be changed from outside the class once they have been set in the constructor!` – Liam Oct 18 '19 at 12:43
  • 1
    @Liam Ah, I see what you mean. This requires a change to Department. – Bill the Lizard Oct 18 '19 at 12:43
  • @ Bill the Lizard: Would this be the same as a `defensive copies of object/reference`? – Liam Oct 18 '19 at 12:55
  • 1
    @Liam Yes, Department would be making defensive copies of objects passed to it. This ensures that the state of a Department object doesn't change when an Employee object is changed outside of the Department. – Bill the Lizard Oct 18 '19 at 12:57
  • @ Bill the Lizard: I am not sure if I am doing this correct as I have declared a new instance of Employee(Defensive Copy) to pass into the Department constructor. The updated code is in the Employee class and the Test class. It works but is this what the question is asking – Liam Oct 18 '19 at 14:30
  • 1
    @Liam Your copy constructor looks correct. You just need to use it in Department. Anywhere you have `this.employee = employee;`, you should make it say `this.employee = new Employee(employee);` so that a copy is made. – Bill the Lizard Oct 18 '19 at 15:11