1.4-Software-Development-Principles

Cloning Persons

Introduction

To practice with the process of cloning we present the case as shown in the slides and the theory:

Assignment

Step 1 - Pass the original list by reference.

Create an ArrayList with some Persons in it.

Pass that list to a method that removes the first element and changes the third one.

private void changeList(ArrayList<Person> list) {
	// ...
}

Confirm that your original list has now been changed.

Step 2 - Pass a copy of the list.

Before passing your list to the changeList method, first create a new list and copy all Persons from the original to the copy of the list.

Confirm that in your original list the first Person is still present, but the third one is still changed.

Step 3 - Pass a deep clone of the list.

This time not only create a new copy the list, but also create copies of all Persons.

Confirm that in your original list the first Person is still present. This time the third Person remains unchanged. All changes are now restricted to the cloned list.

Testing

After creating a clone of your list, you may want to actually test that you succeeded. The test method assertEquals uses the equals method to confirm that two object instances have equivalent values.

Please use the assertNotSame (this is the inverse of assertSame) which confirms that although they are equal they are not the very same reference!

So each element in the original list should be equal to each element in the clone, but they must not be the same object.