import java.util.*;
public class Measurement {
int count;
int accumulated;
public Measurement() {}
public void record(int v) {
count++;
accumulated += v;
}
public int average() {
return accumulated/count;
}
public boolean equals(Object other) {
if (this == other)
return true;
if (!(other instanceof Measurement))
return false;
Measurement o = (Measurement) other;
if (count != 0 && o.count != 0)
return average() == o.average();
return count == o.count;
}
public int hashCode() {
(1) INSERT CODE HERE
}
}
Which code, when inserted at (1), will provide a correct implementation of the hashCode() method in the following program? Select the two correct answers.
(a) return 31337;
(b) return accumulated / count;
(c) return (count << 16) ^ accumulated;
(d) return ~accumulated;
(e) return count == 0 ? 0 : average();
The correct answer is (a) and (e). (b) is incorrect because of count if it is 0, it will generate an arithmetic Exception, but I don't know the mean of (c) and (d). Why they are false?
ais not a "correct" implementation. It will compile but will give all objects the same hashcode which is exactly the opposite of what you want.