I'm just learning Java, so please bear with my ignorance. I have a feeling (hope) that this question has a very simple answer, just one that is evading me at the moment.

For our assignment, we have to write a boolean function that will take a given elephant, "e" (the program we're writing is used to store data about elephants) and return true if this elephant is e's mother, and e isn't null.

We have also previously written a getMother() function that will return the elephant's mother.

As such, I need to ensure e isn't null, and verify that e.getMother() returns the name of the elephant calling the function. Sounded easy enough, but several, several hours later I'm no closer to figuring out how to do that.

For example:

Elephant mom = new Elephant("Mom", 1990, 2, 'f');
Elephant dad = new Elephant("Dad", 1990, 10, 'm');
Elephant kid = new Elephant ("Joey", 1995, 7, 'M', 5, mom, dad);

mom.isMotherOf(kid); //Needs to return true

e.getMother will return the name of e's mother, but I don't know how to compare that to whatever elephant is calling the function

boolean isMotherOf(Elephant e) {
      
        return ( e != null && e.getMother() == /* ?????? */)
                
      }

Fot context, a copy of the assignemnt can be found here.

If anyone could point me in the right direction, it would greatly be appreciated. I've already spent more time on his than I did every project up to this point. Let me know if I need to clarify anything, or provide any of my previous code.

isMotherOf() is placed inside the elephant class, there is more than likely a global variable there called name.

so from isMotherOf(), you call e.getMother() which is return the name of e, you want to compare it with the name of the current object, the one that is calling isMotherOf(), you can get that by using "this.name" or "this.whateveryournamevariableiscalled"

The elephant class has a "nickname" field, but multiple elephants can share the same nickname.

I could compare this.nickname to e.getMother(), but I'm certain he will deduct points as multiple elephants could (in theory) have the same nickname.

At this point, however, that's looking like I viable option. I have no clue how he would like us to do this, and I've wasted enough time on this part already.

Thanks for taking the time to look at it for me.

return ( e != null && e.getMother() == /* ?????? */)

The java keyword this refers to the current object - so if you are testing for e.getMother() being the current Elephant (the one in which this method is being executed) it's as simple as

e.getMother() == this

Haha, I knew it would be something simple. I had never seen it done that way, I feel silly now.

Thanks so much.

return ( e != null && e.getMother() == /* ?????? */)

The java keyword this refers to the current object - so if you are testing for e.getMother() being the current Elephant (the one in which this method is being executed) it's as simple as

e.getMother() == this

the problem is that e.getMother() doesnt return the Elephant Object that represents e's mother :

e.getMother will return the name of e's mother,

Thanks for raising that important point.
The OP posted "We have also previously written a getMother() function that will return the elephant's mother." which is what I focused on, but he did also say "verify that e.getMother() returns the name of the elephant".
I assumed that the latter was a beginner mistake in the way the method was described, and that a getMother method would naturally return a maternal Elephant rather than a not-necessarily-unique name. But maybe you are right - in which case the getMother method needs to be re-thought!

Haha, I knew it would be something simple. I had never seen it done that way, I feel silly now.

I hope you don't! These things are only easy when you know the answer. Until then they are hard!

Thanks for raising that important point.
The OP posted "We have also previously written a getMother() function that will return the elephant's mother." which is what I focused on, but he did also say "verify that e.getMother() returns the name of the elephant".
I assumed that the latter was a beginner mistake in the way the method was described, and that a getMother method would naturally return a maternal Elephant rather than a not-necessarily-unique name. But maybe you are right - in which case the getMother method needs to be re-thought!

huh, i had missed that part in the original post, good then, if he has a "true" getMother() method then we're good!

Yup. One ref said returns e's mother, two other refs said "mother's name". On the other hand, when people first get exposed to OO concepts they often seem to have difficulty adapting to the idea that a variable may be an Elephant, rather than an integer or string.

09cody: if that answered your question then please mark this "solved" for the archives.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.