EECS2030Z Test 3

Version C


GETTING STARTED

  1. Download this project archive file
  2. Start eclipse; use the workspace suggested by eclipse (or remember the directory name of your workspace).
  3. Import the test project by doing the following:
    1. Under the File menu choose Import...
    2. Under General choose Existing Projects into Workspace and press Next
    3. Click the Select archive file radio button, and click the Browse... button.
    4. Navigate to your home directory (the file chooser is probably in the workspace directory).
    5. Select the file test3C.zip and click OK
    6. Click Finish.
  4. All of the files you need for this test should now appear in eclipse.
  5. Open a terminal. You will use this terminal to submit your work.
  6. Copy and paste the command cd workspace/Test3C/src/test3 into the terminal and press enter.

Java API

The Java API is here


Question 1 (30 marks total)

SOLUTION

Implement the class described by this API. You do not have to include javadoc comments.

submit 2030 test3C ContactSet.java


Question 2 (20 marks total)

A. (3 marks)

Fall 2017-18 students can ignore this question.

Consider the following three lines of Java code:

PhoneNumber n1 = PhoneNumber.getInstance(416, 967, 1111);
PhoneNumber n2 = PhoneNumber.getInstance(416, 967, 1111);
System.out.println(n1 == n2);

The Java code shown above prints true because PhoneNumber is a particular kind of class; what name is given to such classes?

multiton


B. (3 marks)

Suppose that a client writes the following method:

public static void sort(List<Contact> contacts) {
    Collections.sort(contacts);
}

What method from the Contact class lets the client sort a list of contacts using Collections.sort?

compareTo(Contact) or just compareTo


C. (4 marks)

The API for Contact says that every contact has a name (of type String) and a phone number (of type PhoneNumber). Would you implement Contact as an aggregation or a composition of a PhoneNumber? Explain your answer.

Aggregation. The contact can freely share its PhoneNumber instance because PhoneNumber is immutable.

4 marks for stating aggregation is safe because of immutability of PhoneNumber.
Up to 2 marks for stating composition should be used to preserve the state of the PhoneNumber.
0 marks for any answer that has no explanation.


D. (5 marks)

Consider the constructor ContactSet(ContactSet other):

(Part 1): Based on the API of the constructor, does the constructor make a deep copy or a shallow copy of the set belonging to other?

(Part 2): Your answer here depends on your answer for (Part 1); see the two choices below:

If you answered deep copy in (Part 1) then show how to make a shallow copy of the set belonging to other.

If you answered shallow copy in (Part 1) then show how to make a deep copy of the set belonging to other.

(Part 1):
Deep copy.

// shallow copy
this.contacts = new HashSet<>(other.contacts);

or

// also a shallow copy
this.contacts = new HashSet<>();
for (Contact c : other.contacts) {
  this.contacts.add(c);
}
// deep copy
this.contacts = new HashSet<>();
for (Contact c : other.contacts) {
  this.contacts.add(new Contact(c));
}

2 marks for Part 1 (1 mark if the answer is shallow copy)
3 marks for Part 2 (deduct 2 marks if the shallow copy is actually a deep copy, or if the deep copy is actually a shallow copy).


E. (5 marks)

(Part 1): What class invariant does ContactSet have regarding the contacts in its collection?

(Part 2): Based on the API for ContactSet which method allows a client to break the class invariant of ContactSet? Explain how a client can break the class invariant of ContactSet after using the method (showing actual Java code that does so is acceptable but not required).

(Part 1): A contact set guarantees that every contact is unique (i.e., no two contacts are equal)

(Part 2): None of the methods can be used to break the invariant.

2 marks for Part 1.
3 marks for Part 2.

submit 2030 test3C answers.txt