• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Need help with code!!!

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks! i'll make some modifications. other than that im pretty much done. Thanks for all the input!!!
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic