Win a copy of Terraform in Action this week in the Cloud forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

.equals method

 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I am having some trouble with the .equals method. I have a solid interface with 3 shape classes (Sphere, Cube, and a Cylinder). I have been asked to use the .equals method to determine if they are equal or not equal by creating new objects and then testing them. The problem that I am having is the actual implmentation of the .equals method of the Object class. Here is my main:



I am supposed to print out the statement if they are equal when they are true, but it only works if they are false. I know for a fact that the Cube object has a lenght of 30 and the new object has a lenght of 30. I am supposed to compare it to a BIG O object so that is why I declared the objects like that.

Thanks in advance.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One point of confusion I think you have is with where the equals method is implemented. It should be placed inside your class definitions.
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith, but why? It will work if I call the correct object won't it?
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm... you've showed us some testing code, but the most important code of all is the code for equals. Do you realize that Object's implementation of equals is:

Should your Shape classes inherit this version of equals or should they define their own? What did you do?
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. I will definately define my own, as the Object implementation of this object is inadequate to say the least.

Thanks for the heads up.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:
One point of confusion I think you have is with where the equals method is implemented. It should be placed inside your class definitions.



Originally posted by Gabriel White:
Thanks Keith, but why? It will work if I call the correct object won't it?



Short answer: because this is the way it works in Java.

Slightly longer answer: Ever class inherits the equals() method from Object, as already mentioned. Often the default implementation is not what you need, so you have to override it.

In order to override the equals() method, you first need to clearly define what it means for two objects to be "equal". So what does it mean for two Sphere objects to be equal? You might want to see what data you are using to represent the sphere. This should help in figuring this part out.

Please post your ideas. For now a (mostly) English description is desirable. From there we will be glad to help you with the syntax to implement it.

Layne
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic