Cannot debug this code !!!

A

ankur

public interface MyComparable {
int compareTo(Object obj);
}



public class Rectangle {
public double width;
public double length;

public Rectangle(double w, double l)
{
width = w;
length = l;
}

public double area()
{
return length*width;
}
}


public class Circle implements MyComparable {
private double radius;
public Circle ( double r)
{
radius = r;
}
public double area()

{
return 3.1416*radius*radius;
}

public int compareTo(Object obj)
{
if (obj instanceof Rectangle)
{

Rectangle rec = (Rectangle)obj;
if ( this.area() < rec.area())
{
return -1;
}
else
if ( this.area() > rec.area())
{
return 1;
}
else
{
return 0;
}
}


if (obj instanceof Circle)
{
Circle cir = (Circle)obj;
if ( this.area() < cir.area())
{
return -1;
}
else if ( this.area() > cir.area())
{
return 1;
}
else
{
return 0;
}
}
}
}

public class TestComparable {

public static void main(String[] args) {
Circle cir = new Circle(4.5);
Rectangle rec = new Rectangle(3,4);

int res = -2;

res = cir.compareTo(rec);
System.out.printf("The result is %d", res);

}

}

I get an error message :

Exception in thread "main" java.lang.Error: Unresolved compilation
problem:
This method must return a result of type int

at Circle.compareTo(Circle.java:14)
at TestComparable.main(TestComparable.java:10)

Can you help ??

Thanks,
Ankur
 
K

Kira Yamato

public interface MyComparable {
int compareTo(Object obj);
}



public class Rectangle {
public double width;
public double length;

public Rectangle(double w, double l)
{
width = w;
length = l;
}

public double area()
{
return length*width;
}
}


public class Circle implements MyComparable {
private double radius;
public Circle ( double r)
{
radius = r;
}
public double area()

{
return 3.1416*radius*radius;
}

public int compareTo(Object obj)
{
if (obj instanceof Rectangle)
{

Rectangle rec = (Rectangle)obj;
if ( this.area() < rec.area())
{
return -1;
}
else
if ( this.area() > rec.area())
{
return 1;
}
else
{
return 0;
}
}


if (obj instanceof Circle)
{
Circle cir = (Circle)obj;
if ( this.area() < cir.area())
{
return -1;
}
else if ( this.area() > cir.area())
{
return 1;
}
else
{
return 0;
}
}
}

What happens if obj is neither Rectangle nor Circle?
}

public class TestComparable {

public static void main(String[] args) {
Circle cir = new Circle(4.5);
Rectangle rec = new Rectangle(3,4);

int res = -2;

res = cir.compareTo(rec);
System.out.printf("The result is %d", res);

}

}

I get an error message :

Exception in thread "main" java.lang.Error: Unresolved compilation
problem:
This method must return a result of type int

at Circle.compareTo(Circle.java:14)
at TestComparable.main(TestComparable.java:10)

Can you help ??

Thanks,
Ankur
 
A

ankur

Cool. You nailed it. I added a return -2 after both the if blocks.
But I am surprised about one thing. I am using eclipse. Why is the
code for Circle.java bothered about conditions it hasn't yet
encountered. I mean I am sending it a rectangle and circle and have
written a code to deal with it. So why get an error when I am not
sending it any third type of object ??

Ankur
public interface MyComparable {
int compareTo(Object obj);
}
public class Rectangle {
public double width;
public double length;
public Rectangle(double w, double l)
{
width = w;
length = l;
}
public double area()
{
return length*width;
}
}
public class Circle implements MyComparable {
private double radius;
public Circle ( double r)
{
radius = r;
}
public double area()
{
return 3.1416*radius*radius;
}
public int compareTo(Object obj)
{
if (obj instanceof Rectangle)
{
Rectangle rec = (Rectangle)obj;
if ( this.area() < rec.area())
{
return -1;
}
else
if ( this.area() > rec.area())
{
return 1;
}
else
{
return 0;
}
}
if (obj instanceof Circle)
{
Circle cir = (Circle)obj;
if ( this.area() < cir.area())
{
return -1;
}
else if ( this.area() > cir.area())
{
return 1;
}
else
{
return 0;
}
}
}

What happens if obj is neither Rectangle nor Circle?


public class TestComparable {
public static void main(String[] args) {
Circle cir = new Circle(4.5);
Rectangle rec = new Rectangle(3,4);
int res = -2;
res = cir.compareTo(rec);
System.out.printf("The result is %d", res);


I get an error message :
Exception in thread "main" java.lang.Error: Unresolved compilation
problem:
This method must return a result of type int
at Circle.compareTo(Circle.java:14)
at TestComparable.main(TestComparable.java:10)
Can you help ??
Thanks,
Ankur
 
A

Andrew Thompson

ankur wrote:

Please refrain from top-posting. It is also a good idea to trim
lines of earlier messages that are no longer immediately
relevant - like the 100+ lines of code you top-posted that
reply to.
Cool. You nailed it. I added a return -2 after both the if blocks.
But I am surprised about one thing. I am using eclipse. ..

How is that relevant?
..Why is the
code for Circle.java bothered about conditions it hasn't yet
encountered. I mean I am sending it a rectangle and circle and have
written a code to deal with it. So why get an error when I am not
sending it any third type of object ??

One reason is this. *You* did not give it a 'Triangle',
but someone *else* might.
 
L

Lew

ankur said:
Cool. You nailed it. I added a return -2 after both the if blocks.
But I am surprised about one thing. I am using eclipse. Why is the
code for Circle.java bothered about conditions it hasn't yet
encountered. I mean I am sending it a rectangle and circle and have
written a code to deal with it. So why get an error when I am not
sending it any third type of object ??

Please do not top-post.

The reason is that the error is determined by the compiler. At compilation,
there is no data, so there is not "yet encountered". It hasn't "encountered"
a Circle or a Rectangle, either. One thing it /can/ tell, however, is that
you have not "definitely assigned" a return value to the method. That is to
say, if you trace the code paths, as Kira Yamato did mentally, you can tell
that a condition was not handled. This caused the compiler error.

Again, not only have you "not [sent] it any third type of object" at
compilation time, you haven't sent it a first or second type of object either.
None of that happens until run time, but this was a compiler error, not a
run time error. The compiler simply caught that the logic was inherently
complete.

This is a strength of Java, that it catches this kind of error at compilation,
instead of making you wait until you run the program, or worse that a customer
does, to find it.
 
A

Andrew Thompson

ankur said:
...I added a return -2 after both the if blocks.

From the JavaDocs for Comparator..
"In the foregoing description, the notation sgn(expression)
designates the mathematical signum function, which is
defined to return one of -1, 0, or 1 according to whether
the value of expression is negative, zero or positive."

Note the lack of '-2'. I suspect this will lead to erratic
behaviour, since compare(Circle, Triangle) will return
the same number as compare(Triangle, Circle).

It might be more appropriate to throw an
IllegalArgumentException in this instance.

--
Andrew Thompson
http://www.physci.org/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
 
L

Lew

Andrew said:
From the JavaDocs for Comparator..
"In the foregoing description, the notation sgn(expression)
designates the mathematical signum function, which is
defined to return one of -1, 0, or 1 according to whether
the value of expression is negative, zero or positive."

Note the lack of '-2'. I suspect this will lead to erratic
behaviour, since compare(Circle, Triangle) will return
the same number as compare(Triangle, Circle).

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
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.
 
A

ankur

From the JavaDocs for Comparator..
"In the foregoing description, the notation sgn(expression)
designates the mathematical signum function, which is
defined to return one of -1, 0, or 1 according to whether
the value of expression is negative, zero or positive."
Note the lack of '-2'. I suspect this will lead to erratic
behaviour, since compare(Circle, Triangle) will return
the same number as compare(Triangle, Circle).
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
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.


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
 
A

ankur

From the JavaDocs for Comparator..
"In the foregoing description, the notation sgn(expression)
designates the mathematical signum function, which is
defined to return one of -1, 0, or 1 according to whether
the value of expression is negative, zero or positive."
Note the lack of '-2'. I suspect this will lead to erratic
behaviour, since compare(Circle, Triangle) will return
the same number as compare(Triangle, Circle).
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
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.


What/who is OP ?? I did not get that ?

Ankur
 
L

Lew

ankur said:
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.
 
A

ankur

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.

Sure Lew I understand that. I was just trying to write the code in
short! Thanks !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top