>...I added a return -2 after both the if blocks.

Signature
Andrew Thompson
http://www.physci.org/
>> ...I added a return -2 after both the if blocks.
>
[quoted text clipped - 10 lines]
> It might be more appropriate to throw an
> IllegalArgumentException in this instance.
First, the OP implemented "MyComparable", an apparent clone of
java.lang.Comparable.
It does raise the question of why not to use java.lang.Comparable.
Regardless, -2 is a perfectly valid return value.
> Returns a negative integer, zero, or a positive integer
> as this object is less than, equal to, or greater than the specified object.
More to the point, the OP neglected to address that
> [t]he implementor must ensure
> sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y.
and that
> [t]he implementor must also ensure that the relation is transitive:
> (x.compareTo(y)>0 && y.compareTo(z)>0) implies x.compareTo(z)>0.
and that
> the implementor must ensure that
> x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)),
> for all z.
Plus, they really should have looked into that
> [i]t is strongly recommended, but not strictly required that
> (x.compareTo(y)==0) == (x.equals(y)). Generally speaking,
> any class that implements the Comparable interface and
> violates this condition should clearly indicate this fact.
> The recommended language is
> "Note: this class has a natural ordering that is inconsistent with equals."
(All from <http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html>.)
See Also: Comparator
<http://java.sun.com/javase/6/docs/api/java/util/Comparator.html>
which has highly similar verbiage.
It is highly irregular to compare different classes that do not have a common
supertype. That will make it extremely convoluted to fulfill the complete
contract of Comparable.
The OP should define a supertype for Circle and Rectangle and implement the
compareTo() on the supertype.

Signature
Lew
ankur - 09 Dec 2007 08:19 GMT
> >> ...I added a return -2 after both the if blocks.
>
[quoted text clipped - 61 lines]
> --
> Lew
Lew,
Thanks for the explanation. As you are saying I can have
Class TwoDimObj() implement Comparable and define compareTo(). Then
Circle() and Rectangle() can extend TwoDimObj and have their own
getArea() method. The test class can assign Circle and Rectangle
objects to TwoDimObj variables and one of them can call compareTo()
like:
Circle.compareTo(Rectangle). I got it.
Ankur
Lew - 09 Dec 2007 16:38 GMT
> Circle.compareTo(Rectangle). I got it.
Nope. compareTo() is not a static method. You have to instantiate a Circle
and call the method on the instance. You also have to instantiate a Rectangle
and pass the instance to the method.

Signature
Lew
ankur - 09 Dec 2007 21:59 GMT
> > Circle.compareTo(Rectangle). I got it.
>
[quoted text clipped - 4 lines]
> --
> Lew
Sure Lew I understand that. I was just trying to write the code in
short! Thanks !
ankur - 09 Dec 2007 08:20 GMT
> >> ...I added a return -2 after both the if blocks.
>
[quoted text clipped - 61 lines]
> --
> Lew
What/who is OP ?? I did not get that ?
Ankur
Andrew Thompson - 09 Dec 2007 08:27 GMT
>> >> ...I added a return -2 after both the if blocks.
>>
>[quoted text clipped - 61 lines]
Please trim replies of the redundant lines, as well as..
>> --
>> Lew
..signatures.
>What/who is OP ?? I did not get that ?
GIYF <http://www.google.com/search?q=OP+abbreviation>

Signature
Andrew Thompson
http://www.physci.org/
Lew - 09 Dec 2007 16:37 GMT
>>>>> ...I added a return -2 after both the if blocks.
>> [quoted text clipped - 61 lines]
[quoted text clipped - 9 lines]
>
> GIYF <http://www.google.com/search?q=OP+abbreviation>
Original Poster - the person who starts a thread.

Signature
Lew