EECS2030Z Test 3

Version A


GETTING STARTED

  1. Start eclipse; use the workspace suggested by eclipse (or remember the directory name of your workspace).
  2. 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 test3A.zip and click OK
    6. Click Finish.
  3. All of the files you need for this test should now appear in eclipse.
  4. Open a terminal. You will use this terminal to submit your work.
  5. Copy and paste the command cd workspace/Test3A/src/test3 into the terminal and press enter.

Question 1 (30 marks total)

SOLUTION

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

submit 2030 test3A SmartPhone.java


Question 2 (20 marks total)

A. (3 marks)

Suppose a client writes a main method that includes the following two lines of Java code:

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

How many PhoneNumber objects are created by the client's code? Explain how you determined the number of PhoneNumber objects.

There is 1 object because both phone numbers have the same state and PhoneNumber is a multiton class (1 instance per unique state).

1 mark for indicating there is only 1 object.
2 marks for indicating PhoneNumber is a multiton (or for stating there is only 1 object per unique state).


B. (3 marks)

Suppose that a client writes the following method:

public static List<Contact> sortedContacts(SmartPhone phone) {
    List<Contact> c = new ArrayList<Contact>(phone.getContacts());
    Collections.sort(c);
    return c;
}

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 the SmartPhone method named getContacts says that the method returns a deep copy of the set of contacts. What code would you write if the API had said that method returned a shallow copy of the set of contacts?

return new HashSet<Contact>(this.contacts);

OR

Set<Contact> result = new HashSet<Contact>(this.contacts);
for (Contact c : this.contacts) {
  result.add(c);
}
return result;

2 marks for creating a new set (does not matter what kind of set).
2 marks for adding aliases to the new set (no new contacts)


D. (5 marks)

The API for SmartPhone says that SmartPhone and its PhoneNumber form an aggregation. Is this a safe design, or should SmartPhone and PhoneNumber form a composition instead? Explain your answer using 1-3 sentences.

The design is safe because PhoneNumber is immutable. The phone number cannot be changed even if SmartPhone gives out a reference to its PhoneNumber object.

5 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.


E. (5 marks)

Suppose that h is a reference to a HashSet<Contact> instance, and number is a reference to a PhoneNumber.

Contact c = new Contact("Justin Trudeau", number);
h.add(c);                               // add the contact to the set
c.setName("Kathleen Wynn");             // change the name of the contact
boolean tf = h.contains(c);             // does the set contain the contact?

When the code fragment is run, tf is almost certainly equal to false, even though c is clearly in the set h. Explain why this is so using your knowledge of how the hased containers work in Java.

When the contact is added to the set, the hashcode used to determine which bucket the contact should be put into is computed based on the name "Justin Trudeau". When the contact is used as an argument to contains, the hashcode is computed based on the name "Kathleen Wynn" which (almost certainly) causes the set to look in a different bucket than the one it originally used to add the contact.

2 marks for indicating that the name of the contact has changed.
2 marks for indicating that the hash code has changed.
1 mark for explaining that wrong bucket is examined.

submit 2030 test3A answers.txt