Q-1:
[hashcode & equals ]
clss Sortof{
String name;
int bal;
String code;
short rate;
public int hashCode() {
return (code.length() * bal);
}
public boolean equals(Object o) {
// insert code
}
}
Which of the following would fulfill the equals() and hashCode() contracts of the class?
A & B irrelevant
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 answer given is "C"
Note:
阅读(690) | 评论(0) | 转发(0) |