EECS2030Z Test 3

Version B


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 test3B.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/Test3B/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 test3B CellPhone.java


Question 2 (20 marks total)

A. (3 marks)

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

CellPhone phone = new CellPhone(new CellNumber(416, 736, 2100));

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

There are 2 CellNumber objects. One is created by the client, and the other is created by the CellPhone constructor (because a CellPhone and its CellNumber form a composition).

1 mark for indicating there are 2 objects.
1 mark for noting the CellNumber created by the client.
1 mark for noting the CellNumber created by the CellPhone constructor.


B. (3 marks)

Fall 2017-18 students can ignore this question.

What is the definition of a singleton class?

A singleton class is a class that ensures there is 0 or 1 instances of the class; ok if the answer says there is exactly 1 instance.

3 marks for definition.


C. (4 marks)

The API for the CellPhone method named getCallLog says that the method returns a shallow copy of the list of called cell numbers. What code would you write if the API had said that method returned a deep copy of the list of called numbers?

List<CellNumber> result = new ArrayList<>();
for (CellNumber n : this.callLog) {
  result.add(new CellNumber(n));
}
return result;

2 marks for the new list.
2 marks for adding new CellNumber instances to the list.


D. (5 marks)

(Part 1): Besides the fact that the call log is never equal to null, what class invariant does CellPhone have regarding its call log?

(Part 2): Explain why returning a shallow copy in getCallLog is sufficient to ensure that the invariant remains true (in other words, why is a deep copy of the call log not required to maintain the invariant?)

(Part 1): The order of contacts in the call log are from the oldest call to the most recent call.

(Part 2): Modifying the returned list does not affect the CellPhone because the returned list is a new list. It is impossible to modify the CellNumbers in the list because CellNumber is immutable. Because the CellPhone call log cannot be modified, and the numbers in the log cannot be modified a shallow copy is sufficient to preserve the invariant.

2 marks for Part 1.
3 marks for Part 2 (1 mark for stating that the returned list is a new list, and 2 marks for indicating that CellNumber is immutable).


E. (5 marks)

(Part 1): What two conditions are required to be true for compareTo to be consistent with equals?

(Part 2): Explain why the CellNumber implementation of compareTo is consistent with equals. One sentence should be sufficient for your answer.

(Part 1):
if x.compareTo(y) == 0 then x.equals(y) == true
if x.equals(y) == true then x.compareTo(y) == 0

(Part 2): Both compareTo and equals use the same fields to perform the comparison.

2 marks for Part 1 (1 mark for each condition).
3 marks for Part 2.

submit 2030 test3B answers.txt