EECS2030E Test 2

Version C

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 test2C.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/Test2C/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.

The programming question asks you to implement a class that represents a domino tile. A domino tile is a playing piece used in a family of games called dominoes. The face of a domino tile is split into two sides with each side marked with a value between 0 and some upper value (often 6). Although there are many variations of domino-based games, a common theme is that players try to build lines of domino tiles by placing tiles end to end so that the values on the ends of adjacent tiles match.

A common set of domino tiles is called the double-six set. The double-six set is made up of 28 tiles with no tiles repeated representing all combinations of pairs of values. The double-six tiles are shown below:

To submit your programming question:

submit 2030L secEtest2C Domino.java

SOLUTION


Question 2 (12 marks total)

A. 2 marks

Does Domino have a class invariant involving its values? If so, what is it?

Yes, the values of a domino are between Domino.MIN_VALUE to Domino.MAX_VALUE (inclusive).

 

B. 3 marks

Suppose that Domino had the following hashCode method:

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

(a) Is this a legal implementation of hashCode?

Yes, the method returns an int value that does not change if the state of the domino does not change (although see part (b) for the technically correct answer)

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

No, all dominoes with the same value1 will return the same hash code regardless of the value of value2.

Technically, the given hashCode implementation does not satisfy the postcondition of hashCode; namely, if two objects are equal then the hashCode must return the same value for both objects.

 

C. 5 marks

Refer to the picture of the double-six set of dominoes in Question 1. The dominoes in the picture are shown in sorted order where the smallest domino is the domino having two zeros (0-0) and the largest domino is the domino having two sixes (6-6). In any row, dominoes increase in order moving to the right (e.g., in the first row, the domino 0-0 is the smallest domino and 0-6 is largest domino). Dominoes increase in order moving down from row to row (e.g., all dominoes in the second row are larger than all dominoes in the first row, all dominoes in the third row are larger than all dominoes in the second row, and so on).

Explain how to implement compareTo for Domino so that the list returned by allDominoes could be sorted as described above. A Java implementation is not required; a sufficiently clear description of the implementation can be done using only two or three sentences.

compareTo should compare two dominoes by their smallest values first; if their smallest values are equal then compareTo should compare the dominoes using the second values; i.e.,

public int compareTo(Domino other) {
    int thisMin = Math.min(this.getValue1(), this.getValue2());
    int otherMin = Math.min(other.getValue1(), other.getValue2());
    int result = Integer.compare(thisMin, otherMin);
    if (result == 0) {
        int thisMax = Math.max(this.getValue1(), this.getValue2());
        int otherMax = Math.max(other.getValue1(), other.getValue2());
        result = Integer.compare(thisMax, otherMax);
    }
    return result;
}

 

D. 2 marks

Are Domino objects immutable? Explain your answer using one or two sentences.

Yes. There are no methods that change the state of a domino.

 

To submit your written questions:

submit 2030L secEtest2C answers.txt