Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with code!!!

 
John Lockheart
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's three spots with comments, where i'm having problems with the code. For my compareTo() method my compiler says an int cannot be deferenced, than how am i supposed to compare the two integers?

 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
John Lockheart
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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:
 
John Lockheart
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks! i'll make some modifications. other than that im pretty much done. Thanks for all the input!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic