EECS2030E Test 3
Version C
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
test3C.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/Test3C/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 secEtest3C PiggyBank.java
SOLUTION using a list
SOLUTION using a map
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 */
(a): Show how to make a shallow copy of the list t
using Java.
List<String> copy = new ArrayList<>(t);
// OR
List<String> copy = new ArrayList<>();
for (String s : t) {
copy.add(s);
}
(b): Show how to make a deep copy of the list t
using Java.
List<String> copy = new ArrayList<>();
for (String s : t) {
copy.add(new String(s));
}
(c): Does the method showCoins
from the programming
question have to return a deep copy? Explain your answer.
No, a deep copy is not required because the elements of
the collection are Coins and Coins are immutable.
B. (3 marks)
When a class has fields that are not of primitive type, we
often need to consider using composition instead of aggregation.
(a) What is the main disadvantage of using composition instead
of aggregation?
The main disadvantage is the cost of time and memory in making defensive
copies of the field.
(b) Under what circumstances is composition required? Your
answer should be more detailed than "composition implies ownership".
Composition is required when there are class invariants that depend on
the state of non-primitive fields.
C. (3 marks)
Examine the Coin
API .
Which field, constructor, or method could be removed that would cause the
least amount of inconvenience for clients of the class? Explain your answer.
(Hint: The correct answer is not toString
).
The constructor can be removed (made private) because clients don't need to create
new Coin objects. References to all of the possible Coin objects are available
as public static final fields (Coin.PENNY, Coin.NICKEL, etc), and coins are
immutable so references to Coin objects can be shared freely without worrying
that the value of the coin might change.
All of the other public methods are very useful: compareTo is useful for sorting
collections of coins, equals is useful (if there is a public constructor) for
comparing coins for equality, hashCode is required if equals is overridden,
getValue is useful for getting the numeric value of a coin, and toString
is useful for printing coins.
If the constructor is removed (made private), then both equals and hashCode can
be safely removed; why?
submit 2030L secEtest3C answers.txt