An integer is not an object, so you can not call methods with its reference.
Use the operators that are provided to compare primitives. You will need to use < > and == and return the appropriate values.
"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Joined: Oct 13, 2006
what about returning the diffrence? Would that be a proper compareTo method? Also for the other methods I had problems with. All i wanted to do was write the least code possible for the program. But i realise that I cannot reach down the hiearchy for variables, only upwards. I'll just have to play with it.
Joined: Oct 17, 2006
In a compareTo(), there are 3 possibilities. The Object passed into compareTO() is either less then, equal to, or greater then the value of the called object. A returned value < 0 is considered to be less then, greater then is >0 and equals is ==0. This is convention and should be used unless you want to annoy people, but is not enforced by the compiler or JVM.
Returning the difference could be used, but makes things more complicated, and would only be suitable in specific cases. If you are comparing Strings, how would you calculate a difference, and what use would it be. Between integral values, it would work, but if there isn't a need to know how much they are off, then what is the point of the extra calculations?
You also need to declare that your class implements the Comparable interface.
Here is a simple Comparable class:
Using the class
Of course this means you can reference MyInt objects as Comparable references. Which can be useful sometimes.
If you are using Java 1.5 or higher you can use generics to make things simpler:
[ February 24, 2007: Message edited by: David McCombs ] [ February 24, 2007: Message edited by: David McCombs ]
Originally posted by John Lockheart: what about returning the diffrence? Would that be a proper compareTo method? ...
Yes, that's a common approach for coding compareTo using numeric values. It's a quick and easy way to get the appropriate positive, negative, or zero result. [ February 24, 2007: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
[John]: what about returning the diffrence? Would that be a proper compareTo method? ...
[marc]: Yes, that's a common approach for coding compareTo using numeric values. It's a quick and easy way to get the appropriate positive, negative, or zero result.
Unfortunately, it's an approach that sometimes leads to incorrect results.
Does this print a positive or negative number? The answer is not what most people would expect, unfortunately. A better approach is to use a few if statements as David showed above. His compareTo() method can also be written like this:
"I'm not back." - Bill Harding, Twister
Joined: Oct 13, 2006
thanks! i'll make some modifications. other than that im pretty much done. Thanks for all the input!!!