Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question from Mock Test about hashCode

 
Steven Gao Song
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jimmy halim
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because option D isn't violate the equals or hashcode contract.
It's not optimal but not violating the contract

with

You will get a lot 2 Object that not equal but has same hashcode, although it's maybe not optimal but it's not violating the contracts
Because every equals object will generate same hashcode return.

So creating stricter (more variable involved) equals than hascode will not violating the contract. But if you created stricter hashcode than equals you violating the contract :
"If two objects are equal according to the equals(object) method, then calling the hashCode() method on each of the two objects must produce the same integer result."

I hope I explain it clearly
Pardon me if there're something wrong with my grammar or vocab
 
Steven Gao Song
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK.



The result is
true
false

I think that the answer from the Mock test is not correct.
Only C is the answer.
 
jimmy halim
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right.
My mistake, I don't read the option well

It'll not violating the contract if the equals :


Using String's length and mathematic (*/+-) operation on equals is really not an equals
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

the contract is clearly violated in answer D.
If two objects have different rate, they are equal but (may) have a diffent hashCode.

Where is the question from?


Yours,
Bu.
 
Steven Gao Song
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
It is from Mock Test of K&B.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic