• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difficulty creating an Addressbook with a compare method  RSS feed

 
Scott Treanor
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to create an address book using a compareNames Method

First I created a separate main method and created two objects
   
Here is my Java File called AddressBook.java with just the compareNames method


I can't figure it out. Can you please help
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're going to post code, please UseCodeTags (← click that link to learn how)

Since AddressBook is a class that you defined, you'll have to override its equals() method and code the logic for two AddressBook objects being "equal" to each other. Java won't automatically go and look at the fields that you defined in AddressBook and compare them to each other, you have to write that logic yourself.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, reconsider whether "AddressBook" is the best name for this object. Your class seems to represent only one name. Does an Address Book have only one name in it or is it a collection of names in alphabetical order? In a real-world address book, what would you call one name that you put into it?
 
Scott Treanor
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Junilu:

I looked on the web for how to Override the equals() method and found some code that was helpfull however I don't understand part of it.
return Double.compare(re, c.re) == 0 && Double.compare(im, c.im) == 0;


I was succesful with the following but it compares Doubles





Here is my code so far, am I getting close or am I way off



 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't copy code if you don't understand what it does.  You are getting close but how does line 81 make any sense?  That's like saying that if I want to see if you, Scott Treanor, are really who you say you are, I'm going to check the temperature outside and compare it with the temperature of the oven in my neighbor's house. Totally nonsensical, right?

On line 78, you have a reference, c, to a Person object.  That's what you want to check, right? What do you check it against? Well, the current object, of course, the one you're calling equals() on.  That would be what "this" refers to, like what you have line 67.

Does that give you any ideas? If not, then maybe you need to find more examples of how to override the equals() method.

The example that you looked at, the one that you tried to copy was comparing two complex numbers. Obviously, a ComplexNumber object will have different attributes from a Person object.  You wouldn't compare attributes that a ComplexNumber has to see if two Person objects are equal, would you? That would make no sense at all.

Here's an example of how you'd do it with a Person object: https://www.sitepoint.com/implement-javas-equals-method-correctly/

But again, Don't Just Copy Code If You Don't Understand What It Does!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!