EECS2030E Test 2

Version D

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 test2D.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/Test2D/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 secEtest2D Range.java

SOLUTION


Question 2 (12 marks total)

A. 2 marks

In the declaration of an field, what does the keyword final mean?

final means that a field can be assigned a value only once.

 

B. 3 marks

Suppose that Range had the following compareTo method:

@Override
public int compareTo(Range other) {
    if (this.overlaps(other)) {
        return 0;
    }
    else if (this.getMaximum() < other.getMinimum()) {
        return -1;
    }
    else {
        // this.getMinimum() > other.getMaximum() == true
        return 1;
    }
}

Is this a good implementation of compareTo? Explain why or why not.

No, the implementation of compareTo is not transitive. Consider the following:

Range x = new Range(1.0, 2.0);
Range y = new Range(1.9, 3.0);     // overlaps with x between 1.9 and 2.0
Range z = new Range(2.9, 4.0);     // overlaps with y between 2.9 and 2.0

int xy = x.compareTo(y);           // returns 0
int yz = y.compareTo(z);           // returns 0
int xz = x.compareTo(z);           // returns -1  oops, not transitive

 

C. 5 marks

Consider the following implementation of equals for the class Range:

@Override
public boolean equals(Range obj) {
    if (this.getClass() != obj.getClass()) {
        return false;
    }
    if (obj == null) {
        return false;
    }
    Range other = (Range) obj;
    double thisMin = this.getMinimum();
    double thisMax = this.getMaximum();
    double otherMin = other.getMinimum();
    double otherMax = other.getMaximum();
    if (Double.doubleToLongBits(thisMin) != Double.doubleToLongBits(otherMin) &&
        Double.doubleToLongBits(thisMax) != Double.doubleToLongBits(otherMax)) {
        return false;
    }
    return true;
}

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

1. the signature is incorrect (parameter has type Range instead of Object)
2. must check for null before using obj.getClass()
3. the && should be ||

(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 (it throws a NullPointerException when obj.getClass() is called if obj is null).

Also acceptable is that the given implementation is not transitive.

Many students said that x.equals(x) does not return true but this is incorrect. If you trace through the given implementation you will see that x.equals(x) returns true.

 

D. 2 marks

What term is used to describe the technique where a constructor of a class invokes another constructor of the same class?

constructor chaining

 

To submit your written questions:

submit 2030L secEtest2D answers.txt