Results 1 to 18 of 18
Thread: Comparable interface
- 01-31-2012, 06:07 AM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Comparable interface
Say there are GeometricObject, Circle, Rectangle class as follows:
Java Code:public abstract class GeometricObject extends Object implements Comparable<Object> { public int prc; public String color; public void setColor(String color) { this.color = color; } public abstract double getArea(); public static Comparable max(Comparable c1, Comparable c2) { if (c1.compareTo(c2) > 1) return c1; else return c2; } }Java Code:public class Circle extends GeometricObject { private double radius; public int prc; public Circle() { super.prc = 1; } Circle(double radius) { this.radius = radius; } @Override public double getArea() { return radius * radius * Math.PI; } @Override public int compareTo(Object o) { if (getArea() > ((Circle) o).getArea()) { return 1; } else if (getArea() < ((Circle) o).getArea()) { return -1; } else { return 0; } } }And a tester:Java Code:public class Rectangle extends GeometricObject { private double width; private double height; public Rectangle() { } public Rectangle(double width, double height, String color) { this.width = width; this.height = height; setColor(color); } /** Return area */ @Override public double getArea() { return width * height; } @Override public int compareTo(Object o) { if (getArea() > ((Rectangle) o).getArea()) { return 1; } else if (getArea() < ((Rectangle) o).getArea()) { return -1; } else { return 0; } } }
As I'm doing this exercize and do not have answer but I have You, I will appreciate if You could give me some answers on this matter.Java Code:public class TestCircleRectangle { public static void main(String[] args) { Circle c1 = new Circle(5); Circle c2 = new Circle(7); Rectangle r1 = new Rectangle(5, 5, "red"); Rectangle r2 = new Rectangle(7, 7, "red"); Comparable com1 = GeometricObject.max(r1, r2);//ok Comparable com2 = GeometricObject.max(r1, c2);//error } }
Am I using Comparable interface right?
How I'm suppose to handle error comparing objects of type Circle and Rectangle?Last edited by diamonddragon; 01-31-2012 at 06:15 AM.
- 01-31-2012, 07:00 AM #2
Re: Comparable interface
You're using it wrong. A class that implements Comparable should implement it with itself as the type parameter:
Java Code:public class Circle implements Comparable<Circle> { @Override int compareTo(Circle other) { // ... } }Get in the habit of using standard Java naming conventions!
- 01-31-2012, 10:23 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Comparable interface
Well, Comparable<GeometricObject>.
Then GeometricObject should do the comparison.
If GeometricObjects are not Comparable with each other (a Circle cannot be compared with a Rectangle) then the subclasses should implement Comparable.
Now, from the code you are using, you are comparing based on the result of the getArea() method, so I'd say GeometricObject should implement the compareTo method, and not the subclasses.
- 01-31-2012, 05:13 PM #4
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
- 01-31-2012, 05:28 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Comparable interface
Yes, they are Comparable objects.
But it makes no sense for them to implement the compareTo method.
- 01-31-2012, 05:33 PM #6
Re: Comparable interface
Yes, but I don't think that's what you want. You probably want Circle to implement Comparable<Circle>, not Comparable. Then you will get a compile-time error if you try to compare a Circle to a Rectangle.
But, as Tolls noted, if you do want to be able to compare Circles and Rectangles based on a property (such as area) that is common to both of them, then you would want them both to implement Comparable<GeometricObject>. And you would not explicitly implement Comparable in Circle or Rectangle; they would inherit it from GeometricObject. It might look like this:
Java Code:public class GeometricObject implements Comparable<GeometricObject> { // ... @Override public int compareTo(GeometricObject other) { return this.getArea() - other.getArea(); } }Get in the habit of using standard Java naming conventions!
- 01-31-2012, 06:01 PM #7
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: Comparable interface
Thank You for answers, very much.
Now it looks like this.
Do I need abstract method at line 6 in GeometricObject class?
Or I'm using that method signature from Comparable interface?
Java Code:public abstract class GeometricObject implements Comparable<GeometricObject> { public abstract double getArea(); @Override public abstract int compareTo(GeometricObject c); //do I need this abstract method? public static Comparable max(GeometricObject o1, GeometricObject o2) { if (o1.compareTo(o2) > 1) return o1; else return o2; } }Java Code:public class Circle extends GeometricObject { private double radius; @Override public double getArea() { return radius * radius * Math.PI; } @Override public int compareTo(GeometricObject o) { if (getArea() > o.getArea()) { return 1; } else if (getArea() < o.getArea()) { return -1; } else { return 0; } } }Java Code:public class Rectangle extends GeometricObject { private double width; private double height; /** Return area */ @Override public double getArea() { return width * height; } @Override public int compareTo(GeometricObject o) { if (getArea() > o.getArea()) { return 1; } else if (getArea() < (o).getArea()) { return -1; } else { return 0; } } }
- 01-31-2012, 06:12 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Comparable interface
No...move your compareTo code into GeometricObject.
Both those methods are identical, so you may as well have a single one in GeometricObject.
- 01-31-2012, 06:17 PM #9
Re: Comparable interface
The compareTo(...) method in GeometricObject does not need to be abstract. And you do not need to override it in Circle or Rectangle, since they both make the comparison in exactly the same way. I would just move your implementation of the method from Circle into GeometricObject and have Circle and Rectangle inherit it.
Get in the habit of using standard Java naming conventions!
- 01-31-2012, 06:39 PM #10
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: Comparable interface
Finally, compareTo in GeometricObject Override compareTo in Comparable interface, and sice Circle and Rectangle extends GeometricObject, they inherit compareTo method from instance of GeometricObject:
Java Code:public abstract class GeometricObject implements Comparable<GeometricObject>{ public abstract double getArea(); @Override public int compareTo(GeometricObject o) { if (getArea() > o.getArea()) { return 1; } else if (getArea() < o.getArea()) { return -1; } else { return 0; } } public static Comparable max(GeometricObject o1, GeometricObject o2) { if (o1.compareTo(o2) > 1) return o1; else return o2; } }Java Code:public class Circle extends GeometricObject { private double radius; @Override public double getArea() { return radius * radius * Math.PI; } }
- 01-31-2012, 06:49 PM #11
Re: Comparable interface
Looks good to me!
Get in the habit of using standard Java naming conventions!
- 01-31-2012, 06:58 PM #12
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: Comparable interface
Just that I don't see much use of Comparable interface in this case.
- 01-31-2012, 07:00 PM #13
Re: Comparable interface
The only reason I've ever made my classes Comparable is so I could use the list sorting methods in the Collections class.
Get in the habit of using standard Java naming conventions!
- 01-31-2012, 07:13 PM #14
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: Comparable interface
So, using interface is to use patern for some methods headers that has to be implemented in class that implements specific interface?
Last edited by diamonddragon; 01-31-2012 at 07:15 PM.
- 01-31-2012, 08:06 PM #15
- 01-31-2012, 08:52 PM #16
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: Comparable interface
Is it ok if say ClassX extends ClassY and ClassX implements InterfaceY are much similar except interface is not allowed to have method implementation and all inteface fields are final and static?
Last edited by diamonddragon; 01-31-2012 at 08:59 PM.
- 01-31-2012, 10:37 PM #17
Re: Comparable interface
The most important way in which "X extends Y" and "X implements Y" are similar is that in either case, you can treat the X as a Y. You can declare variables of type Y and use them to store objects of any class that extends Y (if Y is a class) or implements Y (if Y is an interface).
Get in the habit of using standard Java naming conventions!
- 02-01-2012, 12:07 AM #18
Re: Comparable interface
Couldn't tell you. That question is unrelated to the subject, so I'd start another thread.
Get in the habit of using standard Java naming conventions!
Similar Threads
-
Implement Comparable interface
By JojoDiaz in forum New To JavaReplies: 3Last Post: 11-07-2011, 07:10 AM -
comparable interface, generic types
By dylandrop in forum New To JavaReplies: 3Last Post: 04-16-2011, 07:45 PM -
I don't understand comparable Interface Please help me
By chathura87 in forum New To JavaReplies: 11Last Post: 03-01-2011, 06:48 AM -
Comparable Interface
By Yelrubk in forum New To JavaReplies: 3Last Post: 04-28-2010, 02:46 PM -
interface Comparable<T> problem
By Lennon-Guru in forum New To JavaReplies: 3Last Post: 03-05-2008, 12:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks