• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

is this correct way of implimenting comparable interface

 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is my main class


this is my bean class

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it isn't.

see api:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html


int compareTo(Object o)
.....
Returns: a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.


You must return 0 when two objects are equal,
but the code returns 1 where two dogs have the same number of legs and 0 in other cases.

Try this:
 
Samanthi perera
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i change my class to this


is this ok?
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samanthi perera wrote:
is this ok?



What happens when you test it?
 
Samanthi perera
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is my main class


when i run this it gives


why it gives null for first item..........
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You did not set d3 name, that's why it's null.
You set two times d2's name.
Look at your code




then, as you sorted the three dogs, d3 happens to be the first one in your arraylist. and when you get its name it is null.
I mean:



 
Samanthi perera
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that means this implimentation is ok?
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i was answering to your last question, if i did not get it wrong: why it gives null for the first item.

As you wrote your compareTo method it happens that a dog is placed before another dog in your List if it has less legs, is that what you meant?

When you construct a dog it has 0 legs.

then when your code is executed:


it happens that

d1 has 6 legs
d2 has 1 leg
d3 has 0 legs

then the order is

d3
d2
d1

I hope i replied to your question

 
Samanthi perera
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no , i change the main class as below


now it gives result here


so i think this implimentation is correct,because it gives result as expected.
i need to know is this is good implimentation?
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can only tell you that now it's correct.
In my opinion it's up to you to decide whether it is also good or not. If in your domain exist dogs with more or less than 4 legs, it could be also good.


 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends on what you mean by "good". I'd say "no", because there's an amazing amount of code, several poor naming issues, and some miscellaneous improvements. See embedded comments, then compare.

coll.javaTestComparableDog.java (my version)dog.javaDog.java (my version)
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic