EECS2030E Test 3

Version A

You have 80 minutes to complete this test. This is a closed book test.


GETTING STARTED

  1. Start eclipse.
  2. Download this project file.
  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 test3A.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/Test3A/src/test3 into the terminal and press enter.

Resources


Question 1 (18 marks total)

IMPORTANT NOTE: You are not given the fields needed to implement the class in this question. You must analyze the API of the class, decide what fields are required, and add the fields to the class.

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

submit 2030L secEtest3A CombinationLock.java

SOLUTION


Question 2 (12 marks total)

A. (6 marks)

Consider the following list:

List<String> t = new ArrayList<>();
/* some code not shown that adds many strings to t */

(Part 1): Show how to make a shallow copy of the list t using Java.

(Part 2): Show how to make a deep copy of the list t using Java.

(Part 3): Suppose that t was a field in the following class:

public class X {
  private List<String> t;

  /* constructors not shown */

  public List<String> getList() {
    /* returns a copy of this.t */
  }
}

If X is a composition of a List of strings, does getList return a shallow copy or a deep copy of this.t? Explain your answer using at most 3 sentences.

(Part 1):

List<String> shallow = new ArrayList<>(t);

or

List<String> shallow = new ArrayList<>();
for (String s : t) {
  shallow.add(s);
}
  
(Part 2):
    
List<String> deep = new ArrayList<>();
for (String s : t) {
  deep.add(new String(s));
}

(Part 3): getList can return a shallow copy in this case because strings are immutable.

If the student answered "a deep copy" then the explanation would have to explain why a deep copy is required in this particular situation (not why a deep copy is required in general); i.e., the explanation would have to state that the identity (address) of each string in the list might be used for something important.

Marking scheme: 2 marks for each part.

In Part 2, a new String must be created inside a loop to receive any marks.

In Part 3, an explanation must be present to receive any marks.


B. (3 marks)

(Part 1): The API for CombinationLock promises that: The length of the combination will always be at least MIN_LENGTH numbers. What is the name for a condition that a class promises will always be true?

(Part 2): Suppose that setCombination was implemented such that it set the combination of the lock to be an alias to the list provided by the client. Show how to break the promise made in (Part 1) of this question by adding one more line of code to the code below:

// client code somewhere
// -- lock is a CombinationLock
// -- owner is the owner of lock

List<Integer> combi = Arrays.asList(17, 3, 88, 2);   // the combination 17-3-88-2
lock.setCombination(owner, combi);
// what line could you put here to break the promise made in (Part 1) of this question?

(Part 1): class invariant

(Part 2):
combi.remove(0);

or any other line of code that causes the size of the list combi to become 
less than MIN_LENGTH (4).

Marking scheme: 1 mark for Part 1, and 2 marks for Part 2.


C. (3 marks)

In your implementation of CombinationLock, is CombinationLock an aggregation of a User or a composition of a User? Explain why you chose to use aggregation or composition for the relationship between the CombinationLock and its User.

CombinationLock is an aggregation of a User.

One reason is that the lock does not own the user (owner).

A second reason is that it is impossible to make a copy of a User (there is no copy constructor in User and the other constructor promises to create a user with a unique id).

Marking scheme: 1 mark for "aggregation" and 2 marks for the explanation.


submit 2030L secEtest3A answers.txt