EECS2030E Test 2
Version B
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. You may have to wait
about 10 seconds before the file browser appears.
In the file browser that appears, navigate to your home directory.
Select the file test2B.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/Test2B/src/test2
into the terminal and press enter.
Resources
SOLUTION
Question 1 (18 marks total)
Implement the
class described by this API . You do not have to include javadoc comments.
The Money
class should keep track of the total current
value of all Money
objects. For example, consider the following
main
method:
public static void main(String[] args) {
Money m1 = new Money(1, 0); // $1.00
Money m2 = new Money(2, 0); // $2.00
Money m3 = new Money(1, 49); // $1.49
// the total current value of all Money object is $4.49
System.out.println(Money.getTotalDollars()); // prints 4 (rounds to nearest dollar)
m3.addCents(2);
// the total current value of all Money object is $4.51
System.out.println(Money.getTotalDollars()); // prints 5 (rounds to nearest dollar)
m1.addDollars(10);
// the total current value of all Money object is $14.51
System.out.println(Money.getTotalDollars()); // prints 15 (rounds to nearest dollar)
Money m4 = new Money(100, 49); // $100.49
// the total current value of all Money object is $115.00
System.out.println(Money.getTotalDollars()); // prints 115 (rounds to nearest dollar)
}
To submit your programming question:
submit 2030L secEtest2B Money.java
Question 2 (12 marks total)
A. 2 marks
(a) Examine the API for Money
.
For a correct implementation of Money
, is it possible
to represent negative amounts of money?
No. The constructors and mutator methods all throw exceptions
preventing the number of cents and dollars from becoming negative.
(b) Given your answer to part (a), what is a possible class
invariant for Money
?
1. The amount of cents is greater than or equal to 0.
2. The amount of dollars is greater than or equal to 0.
3. The total number of dollars is greater than or equal to 0.
B. 3 marks
Suppose that Money
had the following compareTo
method:
@Override
public int compareTo(Money other) {
return (this.getDollars() * CENTS_PER_DOLLAR + this.getCents()) -
(other.getDollars() * CENTS_PER_DOLLAR + other.getCents());
}
(a) Is this a legal implementation of compareTo
?
Yes, the implementation returns an int value. (Arguably,
the answer is no; see part (b) below)
(b) Is this a good implementation of compareTo
?
Explain why or why not.
No. The calculation can overflow the range of int which will
cause the wrong value to be returned.
C. 5 marks
Consider the following implementation of equals
for the class Money
:
@Override
public boolean equals(Object obj) {
if (this == obj) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
Money other = (Money) obj;
if (this.getDollars() != other.getDollars() && this.getCents() != other.getCents()) {
return false;
}
return true;
}
(a) State what errors, if any, are in the implementation.
1. x.equals(x) returns false instead of true
2. The method returns false if and only if both the number of dollars and
the number of cents are different (the && should be ||)
3. The method throws an exception if obj is null.
(b) Are all parts of the equals contract provided by
the implementation? If you answer no, state which parts
of the equals contract are not supported.
No. x.equals(null) does not return false, and x.equals(x) does not return true.
D. 2 marks
Suppose that we added the following methods
to the Money
class that return new
Money
objects whose value are equal to
the specified number of coins:
public static Money fromNickels(int numberOfNickels) {
return new Money(0, 5 * numberOfNickels);
}
public static Money fromDimes(int numberOfDimes) {
return new Money(0, 10 * numberOfDimes);
}
public static Money fromQuarters(int numberOfQuarters) {
return new Money(0, 25 * numberOfQuarters);
}
(a) What name is given to methods such as fromNickels
,
fromDimes
, and fromQuarters
?
static factory method
(b) Instead of providing the three methods shown above, could
we provide three constructors that provided the same functionality?
Explain your answer.
No, because all three methods have a single parameter of type int
and we cannot have three constructors with the same signature.
To submit your written questions:
submit 2030L secEtest2B answers.txt