Please see the question below:
class SortOf{
String name;
int bal;
String code;
short rate;
public int hashCode() {
return (code.length()*bal);
}
public boolean equals(Object o) {
//insert code here
}
}
Which of the following will fulfill the equals() and hashCode() contracts for this class?
(Choose all the apply.)
A. return ((SortOf)o).bal == this.bal;
B. return ((SortOf)o).code.length() == this.code.length();
C. return ((SortOf)o).code.length()*((SortOf)o).bal == this.code.length()*this.bal;
D. return ((SortOf)o).code.length()*((SortOf)o).bal*((SortOf)o).rate == this.code.length()*this.bal*this.rate;
The correct answer is C and D.
Why D is correct?
I don't thnk that it can guarantee the contract between equal and hashCode.