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