What is an obligatory method?
What is a class invariant?
What kinds of fields can a static method use?
What kinds of fields can a non-static method use?
What is the hashCode method used for?
What is the compareTo method used for?
This question refers to the Weight class from the practice programming set.
Suppose that we wanted to provide the following two constructors for Weight:
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?
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.
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:
Temperature
Comparable
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.
What are the 5 parts of the equals contract?