EECS2030E Test 3
Version D
You have 80 minutes to complete this test. This is a closed book test.
GETTING STARTED
Start eclipse.
Download this project file .
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.
Resources
SOLUTION with list
SOLUTION with map
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 secEtest3D OwnedPiggyBank.java
Question 2 (12 marks total)
A. (6 marks)
Recall that you used the Owner
class in the programming
question for this test (see
the API here ).
Consider the following list:
List<Owner> t = new ArrayList<>();
/* some code not shown that adds many owners to t */
(a): Show how to make a shallow copy of the list t
using Java.
List<Owner> copy = new ArrayList<>(t);
// OR
List<Owner> copy = new ArrayList<>();
for (Owner w : t) {
copy.add(w);
}
(b): Is it possible to make a deep copy of t
using Java?
If so, show how; if not, then explain why the deep copy cannot be made.
No, there is no copy constructor in Owner, and the constructor
Owner(String name) makes a new owner that is not equal to any other
Owner because its id is guaranteed to be unique.
(c):
In your implementation of OwnedPiggyBank
, is OwnedPiggyBank
an aggregation of an Owner
or a composition of an Owner
? Explain
why you chose to use aggregation or composition for the relationship between
the OwnedPiggyBank
and its Owner
.
It is an aggregation of an Owner because it is impossible to make a copy
of an Owner.
B. (3 marks)
Suppose that
OwnedPiggyBank
had a method named shallowCopy
that returned a new list that contained references to the coins that
are in the piggy bank. Is there any way for a client to use the
list returned by shallowCopy
to change the state of
the piggy bank (for example, can the client remove coins from
the piggy bank by manipulating the returned list, or can the client change
the values of the coins in the piggy bank)? If you answer yes,
show how the state of the piggy bank can be changed. If you answer
no, explain why the state of the piggy bank cannot be changed.
No, the returned list is a new list so modifying the list does not
modify the piggy bank's internal collection. Coin is immutable,
so it is impossible to change the values of the coins in the
returned list or in the piggy bank itself.
C. (3 marks)
OwnedPiggyBank
has a serious privacy leak. What is
the privacy leak, and what is the implication of this particular privacy
leak?
It returns a reference to the owner which allows any client to empty
the piggy bank of its coins.
submit 2030L secEtest3D answers.txt