Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to declare compareTo() method and invoke it from the main method?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there!
I am trying to find out what would be correct syntex for declaring a compareTo() method and invoking it from the main program. I went through books but I didn't find anything related to compare two integers (but I found for String comparison). I also found suggestion for using interface but I'm not sure how to implement that. Can anyone please help me?

Mahi.


 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, if you are going to write a compareTo() method in your class, then you may as well declare to the world that instances of your class can now be used wherever a Comparable<State> is expected. You do that by declaring that your class implements Comparable<State>



Now, as far as implementing a useful compareTo() method for your class, primitives (int, double, etc.) have no methods. You can not do something like this:


However the (<, >, <=, >=, ==) operators are defined for ints so you can do this.


so in your compareTo() method, when comparing primitives you should do something similar.

[ February 18, 2007: Message edited by: Garrett Rowe ]
 
Mahi Rahman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hei Garrett!
Thanks a lot for your reply. I now understand what was the problem. Cheers!
Mahi.
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic