Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Equals Method problem

 
Tiffany Smith
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello..... please help ... I'm

I'm trying to compare two integers and return the value to another class, and I keep getting false even though the values are equal:

here is the method:


if you need the whole probram... here you go...


Any help you can provide is greatly appreciated... I should have a concussion from beating my head against this wall for so long... *sigh*
Tiffanhy
 
Henry Wong
author
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: Are you sure that you are calling your equals() method? Or the one the you inherited from the Object class?
 
Tiffany Smith
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Hint: Are you sure that you are calling your equals() method? Or the one the you inherited from the Object class?


Thanks for replying Henry. I'm still lost... I'm super noob. I'm in Ch. 4 of a very bad text book with assignments that are less than 1/2 explained. I'm still not sure why it's returning false. I tried comparing the values both within the main method and in my Counter class... but no dice.

What am I doing wrong?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiffany,
Welcome to JavaRanch!

Take a look at your equals() method. What does it take as a parameter? What are you passing? (it isn't the same type.)

If you don't know the answer to either of these questions, post which one you do know. Or you can see for yourself by renaming your method from "equals" to "equalsValue". This will give you a compiler error rather than functioning in a way to don't expect. And the compiler will answer the questions too.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote: Or you can see for yourself by renaming your method from "equals" to "equalsValue".


This is a good idea, but note that you have to change the name in two places for this to work. You have to change it where you define the function, and then also change it where you call the function, so i.e.,

... counter.equalsValue(counter2));
 
Tiffany Smith
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Tiffany,
Welcome to JavaRanch!

Take a look at your equals() method. What does it take as a parameter? What are you passing? (it isn't the same type.)

If you don't know the answer to either of these questions, post which one you do know. Or you can see for yourself by renaming your method from "equals" to "equalsValue". This will give you a compiler error rather than functioning in a way to don't expect. And the compiler will answer the questions too.


Thanks for the welcome.
I'm actually not passing anything anymore... I deleted the method in the counter class and am just using the following code in the main program:


I think both counter and counter2 are of the Counter class... I'm not getting an incompatible error with the code as it's written right now. Are they string values? I'm totally confused. Sorry to be so totally dense...

Here's my code:
 
Tiffany Smith
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually... I guess I was wrong about deleting the equals method... my homework assignment specifically calls for one... this just might kill me. Where's my nouse...

please help.... I'm lost and I can't find my way out...
 
Henry Wong
author
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiffany Smith wrote:I think both counter and counter2 are of the Counter class... I'm not getting an incompatible error with the code as it's written right now. Are they string values? I'm totally confused. Sorry to be so totally dense...


Just because you are not getting a compiler error doesn't mean that it is correct.

Your equals() method takes an primative int. You are passing it a Counter object. Obviously, these two types are completely incompatible... Something is obviously wrong.

What is wrong is... it is not calling your equals() method, but is calling the equals() method that is inherited from the Object class.... as already mentioned.

Henry
 
Tiffany Smith
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Tiffany Smith wrote:I think both counter and counter2 are of the Counter class... I'm not getting an incompatible error with the code as it's written right now. Are they string values? I'm totally confused. Sorry to be so totally dense...


Just because you are not getting a compiler error doesn't mean that it is correct.

Your equals() method takes an primative int. You are passing it a Counter object. Obviously, these two types are completely incompatible... Something is obviously wrong.

What is wrong is... it is not calling your equals() method, but is calling the equals() method that is inherited from the Object class.... as already mentioned.

Henry


Okay... that makes sense. Is there a way to make it call my equals method? I guess having two methods with the same name messes things up...
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your class have an equals method with exactly the same parameter types as Object.equals? If not then you're not overriding that method but overloading it instead.

Note that an equals method should not throw any exceptions if null is passed, or if the argument is of a completely different class. It should return if that is the case.
 
Tiffany Smith
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Does your class have an equals method with exactly the same parameter types as Object.equals? If not then you're not overriding that method but overloading it instead.

Note that an equals method should not throw any exceptions if null is passed, or if the argument is of a completely different class. It should return if that is the case.



Thanks everyone who replied... I just dropped the class. I am going to try this again with a better textbook/better teacher/and hopefully it will make sense.
 
Bert Wilkinson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From stuck on homework to "dropped the class" in less than 4 hours. Whew, what a trooper!
 
Henry Wong
author
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bert Wilkinson wrote:From stuck on homework to "dropped the class" in less than 4 hours. Whew, what a trooper!


Well, that is one of the purposes of school. To find what you like. To find what you don't like. To find what you are good at. To find what you are not good at. Better to figure it out now, before you enter the work force.

Henry
 
Johnny Doe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I'm reviving this thread because I'm stuck at the same point - same point of not understanding. And same homework problem. I understand that I can create a method an invoke it by using the object then a dot then the method. I don't get this equal overiding thing.
I think the book must bury this info somewhere.
Anyone want to demonstrate it (not the homework but rather the idea) with a few lines of code I can run (but not too few)? Maybe comment it? Thanks.
And you have only 4 hours or else..... :}

 
Henry Wong
author
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Doe wrote:Hi all, I'm reviving this thread because I'm stuck at the same point - same point of not understanding. And same homework problem. I understand that I can create a method an invoke it by using the object then a dot then the method. I don't get this equal overiding thing.
I think the book must bury this info somewhere.
Anyone want to demonstrate it (not the homework but rather the idea) with a few lines of code I can run (but not too few)? Maybe comment it? Thanks.
And you have only 4 hours or else..... :}


To answer Tiffany issue, not that it would matter as she dropped the course three years ago, she did *not* override the equals() method. To override a method, she needed to have the same signature -- she didn't. She had a different parameter, and hence, was calling the equals() method, from the Object class, that she did not override.


Now, Johnny, for you to be at the same point, you would have to make the same mistake. Is this true? And BTW, welcome to the ranch.

Henry
 
Johnny Doe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi thanks for the help. I made the same error.

In this homework there is the following "demo" that we *must* use. But I wonder : at the place below where I wrote 'huh?' (in the demo) is there a logic error?
Shouldn't they be comparing the value that is being incremented rather than the counter and counter2 objects themselves?




 
Johnny Doe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay - this seems to work. I used your hint that suggested that to override the equals method I must use the same (same parameter) [an object - in this case] in both the invocation (?) and the overriding method (?).

I guess, that when I do that- it works. I didn't really know that I could send a Counter object into the method. But I guess that is what alerts the compiler to do an override (?)
But what is the point of all that? Maybe I should do a non-override usage somehow? Compare two ints?


 
Winston Gutkowski
Bartender
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Doe wrote:I guess, that when I do that- it works. I didn't really know that I could send a Counter object into the method.

Not only can you; you MUST send an object to an equals() method because that's what they're for - comparing OBJECTS.

But I guess that is what alerts the compiler to do an override (?)

The signature of the method - which is STILL wrong.

Look carefully at the signature for Object.equals(), and copy/paste it if necessary.

Also, adding an "@Override" annotation will make the compiler give you an error message if you get it wrong.

HIH

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic