• Post Reply Bookmark Topic Watch Topic
  • New Topic

equals method  RSS feed

 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create an equals method that takes an object reference and returns true if the given object equals this object.
Hint: You'll need 'instanceof' and cast to a (Geocache)

So far I have:



I think I have it correct up to the casting but I don't understand what I'm suppose to do with the this.equals(). Also I'm getting an error that I'm not returning a boolean... I get this all the time in other problems. I don't get why since I have to instances of returning booleans in this. "returns true if the given object equals this object" makes no sense to me. I assume the given object, in my case, is 'O'. What is 'this' object referring to?
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:

What are the members of GeoCache, and how would use them to determine of two instances are equal?

For example it could be something like:
return (this.cacheType == j.getCacheType && this.found == j.getFound);
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really don't understand this at all. So I got a hint and it turns out I was suppose to use a couple get methods I worked on previously. How I am suppose to know to do that from the description I'll never know. This whole assignment has been like this... vague.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need lots more than hints to write equals methods, which are bl**d* difficult. There are three classic resources, which I listed in this post. Read them all. You find that the hint about instanceof may actually be incorrect (shut up, Winston). The instanceof operator in that location is only reliable if you do not extend the class.
Ron McLeod's example will work nicely if the fields do not have their own equals() methods, i.e. they are primitives.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote: . . . I was suppose to use a couple get methods I worked on previously. . . .
You never need get methods inside the equals method. You have direct access to all fields, even private ones, because you are inside the same class.

If you have inaccessible fields because they are inside a superclass you write it like this:-That means you must write a correct equals method in the superclass, too.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:So far I have:...

OK, A couple of tips for you:
1. Whenever you override a method (and when you write an equals() method you are always overriding), add an
@Override
tag before it.

2. Always check for an identity (ie, same object) match first, viz:
Note: this is one of the very rare cases when you want to use '==' with objects.

3. Use a good name for your "cast" object. Personally, I like 'that', viz:


I don't get why since I have to instances of returning booleans in this. "returns true if the given object equals this object" makes no sense to me. I assume the given object, in my case, is 'O'. What is 'this' object referring to?

The object that the method is called on.

As you've probably already seen, equals() is called using something like x.equals(y). Well, in that case 'this' (inside the method) will be x, and 'o' will be y (don't use capital letters for variables or parameters).

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That means you must write a correct equals method in the superclass, too.

@Josh: Note that you can ONLY use the 'super.equals(o) && ...' construction in subclasses. It doesn't work for classes that extend Object (which is the default).

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!