Consider the following class:
public class Temperature implements Comparable
{ private int value;
// other methods go here
public int compareTo(Object otherObject)
{
Temperature otherTemp = (Temperature) otherObject;
__________________________________;
}
}
Which is the best statement to use to complete the compareTo() method?

a.return Integer.compare(otherTemp.value, value)
b.return (value – otherTemp.value)
c.return Integer.compare(value, otherTemp.value)
d.return (otherTemp.value – value)

Respuesta :

Answer:

c.return Integer.compare(value, otherTemp.value)

Explanation:

The compare() method as the name implies compares two integer values. If they are equal it returns 0, if the first number is smaller it returns -1, and if the first number is greater it returns 1.

It is an Integer class method that is why you need to type Integer.compare() to call the function.

For this example, the parameters that will be compared are value, and otherTemp.value. The type of compareTo method is an integer, we need to return the result.