EECS2030Z Test 3
Version D
GETTING STARTED
Download this project archive file
Start eclipse; use the workspace suggested by eclipse (or remember the
directory name of your workspace).
Import the test project by doing the following:
Under the File menu choose Import...
Under General choose Existing Projects into Workspace
and press Next
Click the Select archive file radio button, and click
the Browse... button.
Navigate to your home directory (the file chooser is probably in
the workspace directory).
Select the file test3D.zip and click OK
Click Finish .
All of the files you need for this test should now appear in eclipse.
Open a terminal. You will use this terminal to submit your work.
Copy and paste the command cd workspace/Test3D/src/test3
into
the terminal and press enter.
Question 1 (30 marks total)
SOLUTION
IMPORTANT NOTE: In this question you may be required to make a copy of
a Date
instance; unfortunately, Date
has no
copy constructor. To copy a Date
instance d
you
should do the following:
// d is a Date reference
Date copy = new Date(d.getTime());
Implement the
class described by this API .
You do not have to include javadoc comments.
submit 2030 test3D CallLog.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 n = PhoneNumber.getInstance(416, 736, 2100);
CallLogEntry e = new CallLogEntry(new Date(), n);
How many Date
objects are created when running the client's code?
Explain how you determined the number of Date
objects.
There is 1 Date object. One is created by the client, and zero
are created by the CallLogEntry constructor (because a CallLogEntry and its
Date form an aggregation).
1 mark for indicating there are 1 objects.
1 mark for noting the Date created by the client.
1 mark for noting that CallLogEntry constructor does not create a Date object.
B. (3 marks)
Suppose that you wanted to count the number of CallLogEntry
objects created by a program.
(Part 1):
What field would you need to add
to the CallLogEntry
class to keep track of the number of
CallLogEntry
objects created? Include the modifiers and type
of the field in your answer.
(Part 2):
Where in CallLogEntry
would you update the value of the
field that you added in (Part 1)?
(Part 1):
private static int numEntries = 0;
(Part 2):
You would increment numEntries in the constructors (or in a single constructor
if chaining is used).
2 mark for Part 1 (1 mark for static, 1 mark for int and name).
1 mark for Part 2.
C. (4 marks)
What is the difference between a shallow copy of a list and a deep copy of
a list?
In a deep copy, the elements of the new list are new copies of the elements
from the copied list.
D. (5 marks)
Suppose that CallLog
had a getLog
method that was implemented like so:
public List<CallLogEntry> getLog() {
List<CallLogEntry> result;
// code that makes result a shallow copy of this.log
return result;
}
Suppose that a client writes the following code:
// assume that c is a CallLog with many entries
List<CallLogEntry> copy = c.getLog();
Assuming that getLog
returns a shallow copy, show how the
client can break the class invariant of CallLog
. Explain why
your answer breaks the class invariant.
CallLogEntry is an aggregation of a Date, and Date is mutable. By using
the getDate() method of CallLogEntry, the client can change one of
of the entries in the shallow copy so that it has the same Date as another
entry, which breaks the class invariant of CallLog.
2 marks for indicating the class invariant of CallLog.
3 marks for explaining how the date of an entry can be modified using
the shallow copy.
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 CallLogEntry
implementation of compareTo
is inconsistent with equals
. One or two sentences 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):
compareTo only uses the date in the comparison whereas equals uses the date
and the phone number in the comparison.
2 marks for Part 1 (1 mark for each condition).
3 marks for Part 2.
submit 2030 test3D answers.txt