EECS2030E Practice written questions for Test 2

A.

What is an obligatory method?

A method that can be invoked using any reference (i.e., a method inherited from java.lang.Object).

B.

What is a class invariant?

A condition regarding the observable state of the object that is always true.

C.

What kinds of fields can a static method use?

A static method can use only the static fields of the class.

D.

What kinds of fields can a non-static method use?

A non-static method can use both the static and non-static fields of the class.

E.

What is the hashCode method used for?

Hashed collections use hashCode to determine where to look for an object in the collection.

F.

What is the compareTo method used for?

To compare two objects for their relative order.

G.

This question refers to the Weight class from the practice programming set.

Suppose that we wanted to provide the following two constructors for Weight:

/** 
 * Initializes a weight to the given weight in kilograms.
*/ 
public Weight(double kg) { // implementation not shown }
        
        
/** 
 * Initializes a weight to the given weight in pounds.
 */ 
public Weight(double lb) { // implementation not shown }
        

(a) Why can we not provide both of these constructors?
(b) What technique was described in the lecture slides that could be used in place of one or both of the constructors?

(a) They have the same signature (they have the same name and the parameter lists both consist of one double value).
(b) Create static factory methods:

public static Weight kilograms(double kg) { // ... }
public static Weight pounds(double lb) { // ... }

H.

This question refers to the Weight class from the practice programming set.

Suppose that Weight had the following compareTo method:

public int compareTo(Weight other) {
    int result = Double.compare(this.get(), other.get());
    return result;
}
        

Is compareTo consistent with equals for Weight? Explain your answer.

No, compareTo is not consistent with equals. Consider two Weight objects both equal to 1 pound:

Weight w1 = new Weight(1, Weight.LB);
Weight w2 = new Weight(Weight.KG_PER_LB, Weight.KG);

w1.equals(w2) returns true, but w1.compareTo(w2) returns a positive value instead of 0.

I.

This question refers to the Temperature class from the practice programming set.

Suppose that we have Temperature implement the Comparable interface by providing the following implementation of compareTo:

public int compareTo(Temperature other) {
    double t = this.getTemperature();
    double u = other.getTemperature();
    return Double.compare(t, u);
}
        

The implementation technically satisfies the requirements of compareTo but could be improved. Briefly explain how you would improve this implementation of compareTo.

getTemperature gets the numeric value of the temperature ignoring the units of the temperature; thus, 0 degrees Celcius would be considered less than 32 degrees Fahrenheit even though both temperatures are equal. The implementation could be improved by returning Double.compare(this.degC, other.degC) where degC is the value of the temperature in degrees Celcius.

J.

What are the 5 parts of the equals contract?

1. x.equals(x) is always true
2. x.equals(y) is true if and only if y.equals(x) is true
3. if x.equals(y) is true, and y.equals(z) is true, then x.equals(z) must be true
4. x.equals(y) always returns the same value if the states of x and y do not change 5. x.equals(null) is always false and does not throw an exception