EECS2030 Sample Test 2


Instructions

There is one programming question and three other questions.

Instructions for submitting your work are given in the question sections. You may submit as many times as you want; your most recent submission will be the one recorded.

You may leave the lab when you are finished with the test.


Programming question

Implement this API. You do not have to include javadoc comments.

Submit your program using the following command in a terminal (make sure you are in the directory containing your file Test2A.java):

submit 1030 test2A Test2A.java


Other questions

Question 1

Non-static methods and constructors have an implicit parameter named



Question 2

The class MyInteger has a single attribute named value of type int. Consider the following implementation of the compareTo method.

public int compareTo(MyInteger other)
{
   int comparison;
   if (this.value < other.value)
   {
      comparison = -1;
   }
   else
   {
      comparison = 1;
   }
   return comparison;
}
Which of the following properties does the above compareTo not satisfy?
For all x, y and z different from null,
A. x.compareTo(y) returns 0 if and only if y.compareTo(x) returns 0.
B. x.compareTo(y) returns a value smaller than 0 if and only if y.compareTo(x) returns a value greater than 0.
C. x.compareTo(y) throws an exception if and only if y.compareTo(x) throws an exception.
D. if x.compareTo(y) returns a value smaller than 0 and y.compareTo(z) returns a value smaller than 0 then x.compareTo(z) returns a value smaller than 0.


Question 3

Modify the above compareTo method so that it satisfies the above four properties.