Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using the equals method

 
Mark Captain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am attempting to check if two die are equal to eachother and im having a bit a trouble.

So far i have been given code in the DiceSimulator class that creates an object for each die



Now i have been having trouble with an equals method in the Die class. So far I have this and i am getting the error that die1 and die2 cannot be resolved. Any ideas? I have a feeling this is a quick fix.

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error states that the variables die1 and die2 cannot be resolved. Do you have them declared some where?

Do you really need them? Do you want to compare two objects of the Die class? Try like below



If you want to compare some variables of the two Die objects, you got to change the condition being checked in the if.
 
Mark Captain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:The error states that the variables die1 and die2 cannot be resolved. Do you have them declared some where?

Do you really need them? Do you want to compare two objects of the Die class? Try like below



If you want to compare some variables of the two Die objects, you got to change the condition being checked in the if.


say i wanted to compare a variable of the die object known as "spots" where in the equals code would i reference this?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Might be like below if its a String... Shall use == if it's a primitive..

 
Harsha Smith
Ranch Hand
Posts: 287
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsha's code also highlights another flaw in your original code. The equals method requires java.lang.Object as its parameter type. If it's something else you're not overriding the equals method but overloading it instead.
And if you're overriding equals you should also override hashCode.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a redundant bit of code in that method, and a potential error which may cause the equals method not to fulfil its general contract.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There is a redundant bit of code in that method, and a potential error which may cause the equals method not to fulfil its general contract.

If you mean Harsha's, I spotted the redundancy, but not a potential error. Could you elaborate?

Winston
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:I spotted the redundancy
Could you please tell where it is...
are those these?

and
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:If you mean Harsha's, I spotted the redundancy, but not a potential error. Could you elaborate?

I think he means the use of instanceof. Using that in an equals() method can lead to problems unless the class or the method is final. Think about how you might implement equals() in a subclass.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the redundancy is in o!=null && o instanceof Die. If o instanceof Die then o != null.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:I think he means the use of instanceof. Using that in an equals() method can lead to problems unless the class or the method is final. Think about how you might implement equals() in a subclass.

Oh, OK. I don't really think of that as a potential error, because using getClass() can cause other problems, such as violating LSP; and you have to use one or the other (unless you want to get quite involved). Personally, I always use instanceof unless I've got a good reason not to.

Winston
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, but it can lead to you breaking commutativity, which breaks the contract.

The way I see it, if you can make the equals() method final there's no problem. If you can't then there's no perfect solution, and it's a matter of what you can live with.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you make the class final, then there is no such problem either.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you make the class final, then there is no such problem either.

Yeah. That one actually solves a lot of problems.

In fact, I don't know why more schools don't recommend getting into the habit of doing it as a matter of course (like making variables private). After all, you can always remove it later on if it's too restrictive.

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect that a lot of teachers don’t know about final.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic