Results 1 to 18 of 18
  1. #1
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default 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;
            }      
        }
    }
    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;
            }
        }
        
    }
    And a tester:
    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                                       
        }
    }
    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.

    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 07:15 AM.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

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

  4. #4
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Comparable interface

    Quote Originally Posted by kjkrum View Post
    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) {
            // ...
        }
    }
    If Circle and Rectangle extends GeometricObject and GeometricObject implements Comparable, does Circle and Rectangle implement Comparable?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Comparable interface

    Yes, they are Comparable objects.
    But it makes no sense for them to implement the compareTo method.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Comparable interface

    Quote Originally Posted by diamonddragon View Post
    If Circle and Rectangle extends GeometricObject and GeometricObject implements Comparable, does Circle and Rectangle implement Comparable?
    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!

  7. #7
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default 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;
            }
        }    
    }

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

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

  9. #9
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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!

  10. #10
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default 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;
        }    
    }

  11. #11
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Comparable interface

    Looks good to me!
    Get in the habit of using standard Java naming conventions!

  12. #12
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Comparable interface

    Just that I don't see much use of Comparable interface in this case.

  13. #13
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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!

  14. #14
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default 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 08:15 PM.

  15. #15
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Comparable interface

    Correct.
    Get in the habit of using standard Java naming conventions!

  16. #16
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default 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 09:59 PM.

  17. #17
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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!

  18. #18
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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

  1. Implement Comparable interface
    By JojoDiaz in forum New To Java
    Replies: 3
    Last Post: 11-07-2011, 08:10 AM
  2. comparable interface, generic types
    By dylandrop in forum New To Java
    Replies: 3
    Last Post: 04-16-2011, 08:45 PM
  3. I don't understand comparable Interface Please help me
    By chathura87 in forum New To Java
    Replies: 11
    Last Post: 03-01-2011, 07:48 AM
  4. Comparable Interface
    By Yelrubk in forum New To Java
    Replies: 3
    Last Post: 04-28-2010, 03:46 PM
  5. interface Comparable<T> problem
    By Lennon-Guru in forum New To Java
    Replies: 3
    Last Post: 03-05-2008, 01:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •