9 Replies Latest reply on Nov 19, 2012 9:39 PM by dadams07

# understanding equals method

so first off, I'd like to say that I checked here first: http://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html
I can't apply this same style to my code, I think I may be going for something a little different.

So what I have to do is determine if two stocks are equal, by comparing their percent change. Easy enough.
I'm going to be creating two stock classes in main.

Here is my method that returns percentChange:

public double getChangePercent(){
double percentChange = (currentPrice - previousClosingPrice)/previousClosingPrice;
return percentChange;
}

Here is my equals method to determine if stocks are equal by comparing percentChange:

public boolean equals(Stock otherStock){

}

Here is how I will perform the equals operation from main:

if(firstStock.equals(secondStock){
System.out.println("Stocks are equal.");
}
else {
System.out.println("Stocks aren't equal.");
}

My question is I'm not quite sure how to setup my if statements inside of my equals method. I was thinking something like this:

if(stock1.getPercentChange == stock2.getPercentChange){
return getPercentChange;
}
else{
return false;
}

Obviously the code just above does not work in my equals method, but I'm assuming that has to be the logic behind it.
Any help is greatly appreciated!
Thank you!
• ###### 1. Re: understanding equals method
Welcome to the forum!
>
So what I have to do is determine if two stocks are equal, by comparing their percent change.
>
Why are you trying to use an 'equals' method for this? Did you read the doc you cited? It tells you what the default 'equals' method does
>
The equals() Method

The equals() method compares two objects for equality and returns true if they are equal. The equals() method provided in the Object class uses the identity operator (==) to determine whether two objects are equal. For primitive data types, this gives the correct result. For objects, however, it does not. The equals() method provided by Object tests whether the object references are equal—that is, if the objects compared are the exact same object.

To test whether two objects are equal in the sense of equivalency (containing the same information), you must override the equals() method.
>
An 'equals' method is used to compare two OBJECTs. Your 'getChangePercent' method returns a double primitive which is NOT an object and nothing in the code you posted shows that the change percent is even part of any object.

If you want to compute the percent change for two stocks and compare the percent change values just use ==.

But it doesn't make sense to say that two stocks are equal just because their percent change is equal. One stock could be priced at \$100 and increase by \$10. The second stock could be priced at \$10 and increase by \$1. The percent change for each stock is 10% but the stocks themselves are NOT equal, only the percent change in their price.

You need to explain more clearly what you are really trying to do. Forget the code, provide an example of two stocks and, in English, explain how you want to compare them to see if they are equal.
• ###### 2. Re: understanding equals method
why thank you :)!!

& actually, we were supposed to build our own equals method & I was just googling for some reference to put some things together.

But I was just messing around & I think I've got it:

public boolean equals(Stock otherStock){
if(this.getChangePercent() == otherStock.getChangePercent()){
return true;
}
else{
return false;
}
}

Not sure if it works, as I haven't implemented a main yet, but no errors! So thats a ++. ;P

Any suggestion on if this looks good or not, would be cool :)
• ###### 3. Re: understanding equals method
You missed the point of what I said above.
>
it doesn't make sense to say that two stocks are equal just because their percent change is equal.
>
• ###### 4. Re: understanding equals method
To All

I would suggest using the manual ( paper or online or PC program tutorial ).
Search the appropriate term.
If you don't suceed try, try and try again.
Hope this helps.

Bye for now.

A.Valli

P.S. THANKS TO THE PERSON/S WHO LEAD ME TO THIS SITE.
P.P.S. K.I.S.S(illy) principle.
P.P.P.S.My choice of tag is not favouritism or a commitment to .... (I am unfamiliar with this element of web sites)
• ###### 5. Re: understanding equals method
Ah, okay. Straight from the instructors text: //equals method determines if two stocks are "equal" by comparing their
//percent change.

My interpretation of this is: if the percent change is equal in both stocks, then that is true. If the percent change is not equal in both stocks, then return false.

That's all I want to be able to do.
• ###### 6. Re: understanding equals method
>
My interpretation of this is: if the percent change is equal in both stocks, then that is true. If the percent change is not equal in both stocks, then return false.

That's all I want to be able to do.
>
Then your method that uses == to compare the two double values will do that. But calling that method 'equals' is a dangerous practice since commonly accepted usage of an 'equals' method is to compare two objects. And the exampe I gave you should make it clear that two different stocks will NEVER be equal just because the percent change of their price is the same.
• ###### 7. Re: understanding equals method
Perfect, that's what I wanted to hear. & you're right! That definitely makes sense, & I will most certainly watch out for that on future assignments/projects.

• ###### 8. Re: understanding equals method
Just for completeness:
``return this.getChangePercent() == otherStock.getChangePercent() ``
is exactly the same as
jjthexer wrote:
``````         if(this.getChangePercent() == otherStock.getChangePercent()){
return true;
}
else{
return false;
}``````
but is much more readable. (at least IMHO)

bye
TPD
• ###### 9. Re: understanding equals method
Even though it compares just a single value (the percent change), your code still has pitfalls. What happens, for example, if the argument to equals() is null? Your method will crash instead of returning anything.