EECS2030E Test 2

Version A

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. You may have to wait about 10 seconds before the file browser appears.
    4. In the file browser that appears, navigate to your home directory.
    5. Select the file test2A.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/Test2A/src/test2 into the terminal and press enter.

Resources


Question 1 (18 marks total)

Implement the class described by this API. You do not have to include javadoc comments.

To submit your programming question:

submit 2030L secEtest2A Coin.java

SOLUTION


Question 2 (12 marks total)

A. 2 marks

State two class invariants that Coin has.

1. the type of a coin is equal to one of Coin.GOLD, Coin.SILVER, or Coin.COPPER
2. the numeric value of a coin is always greater than or equal to zero
3. the total number of coins is greater than or equal to zero

 

B. 3 marks

Suppose that Coin had the following hashCode method:

@Override
public int hashCode() {
    return this.getType().hashCode();
}

(a) Is this a legal implementation of hashCode?

Yes.

(b) Is this a good implementation of hashCode? Explain why or why not.

No, because all coins of the same metal type will return the same hash code regardless of the value of the coin.

 

C. 5 marks

Consider the following implementation of equals for the class Coin:

@Override
public boolean equals(Coin obj) {
    if (this.getClass() != obj.getClass()) {
        return false;
    }
    if (obj == null) {
        return false;
    }
    Coin other = (Coin) obj;
    if (this.getValue() != other.getValue()) {
        return false;
    }
    if (this.getType() != other.getType()) {
        return false;
    }
    return true;
}

(a) State what errors, if any, are in the implementation.

1. The method has the wrong signature (parameter type is Coin instead of Object) 2. We should check if obj is equal to null before invoking obj.getClass() to prevent throwing NullPointerException.
3. We should use equals instead of != to compare the types of the coins.

(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; instead a NullPointerException is thrown.

 

D. 2 marks

What two conditions are required for compareTo to be consistent with equals?

1. if x.compareTo(y) == 0 is true, then x.equals(y) is true
2. if x.equals(y) is true, then x.compareTo(y) == 0 is true

 

To submit your written questions:

submit 2030L secEtest2A answers.txt