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.

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.

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

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

posted 8 years ago

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

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.

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

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.

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

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.

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: 5

posted 5 years ago

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?