EECS2030E Test 3

Version B

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 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/Test3A/src/test3 into the terminal and press enter.

Resources

SOLUTION


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.

Note that java.util.Date lacks a copy constructor. The following example shows you how to make a copy of a Date object:

Date d = new Date();                 // the current date
Date copy = new Date(d.getTime());   // a new Date equal to d

submit 2030L secEtest3B ItemizedBill.java


Question 2 (12 marks total)

A. (6 marks)

Consider the following list:

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

(Part 1): Show how to make an alias of the list dates using Java.

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

(Part 3): What is the big-O complexity for your answers to (Part 1) and (Part 2)?

(Part 1):

List<Date> alias = dates;
  
(Part 2):
    
List<Date> deep = new ArrayList<>();
for (Date d : dates) {
  deep.add(new Date(d.getTime));
}

(Part 3): O(1) for the alias because all that is required is an assignment.
O(n) for the deep copy because every date in the list is copied and added to the list.

Marking scheme: 2 marks for each part.

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

In Part 3, no explanation is required.


B. (3 marks)

(Part 1): According to its API, every Item has-a String that is a description of the item. Do you think that Item is an aggregation of a String or a composition of String? Explain your answer.

(Part 2): What line or lines of Java code could you add to the following to prove your answer to (Part 1)?

String description = "Pepsi cola";
int price = 1;
Item item = new Item(description, price);
// what line(s) could you put here?

(Part 1): Item is an aggregation of a String. There is no need to use composition because strings are immutable.

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

(Part 2):
boolean isSameObject = item.getDescription() == description;

isSameObject will be true if Item uses aggregation, and it will be false 
if Item uses composition.

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


C. (3 marks)

Aggregation and composition are both has-a relationships. Using one or two sentences, state what the main difference is between aggregation and composition.

Composition implies ownership.

(Similar possible answer): Composition means that the object has exclusive access to its composed object (X has exclusive access to its Y object).

Marking scheme: 3 marks.


submit 2030L secEtest3B answers.txt