• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Using the equals method

 
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.

 
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..

 
Ranch Hand
Posts: 287
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Marshal
Posts: 70224
282
  • 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.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
 
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
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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
Posts: 10777
71
Hibernate Eclipse IDE 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
Marshal
Posts: 70224
282
  • 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
Posts: 10777
71
Hibernate Eclipse IDE 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
Marshal
Posts: 70224
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect that a lot of teachers don’t know about final.
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic