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.
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
posted
0
that means this implimentation is ok?
Nicola Garofalo
Ranch Hand
Joined: Apr 10, 2010
Posts: 308
posted
0
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
posted
0
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
posted
0
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.
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.