aspose file tools*
The moose likes Java in General and the fly likes is this correct way of implimenting comparable interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "is this correct way of implimenting comparable interface" Watch "is this correct way of implimenting comparable interface" New topic
Author

is this correct way of implimenting comparable interface

Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
this is my main class


this is my bean class

Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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

Joined: Jan 08, 2010
Posts: 510
i change my class to this


is this ok?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Samanthi perera wrote:
is this ok?


What happens when you test it?
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
this is my main class


when i run this it gives


why it gives null for first item..........
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
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:




Bye,
Nicola
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
that means this implimentation is ok?
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
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

Joined: Jan 08, 2010
Posts: 510
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

Joined: Apr 10, 2010
Posts: 308
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.


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: is this correct way of implimenting comparable interface