Results 1 to 4 of 4
  1. #1
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Implement Comparable interface

    Hey everyone,

    I'm supposed to use class Circle so it implements the Comparable interface.

    the output should look like this,

    Circle(3.00)
    Circle(4.00)
    Circle(3.01)
    Circle(1.00)
    Circle(2.00)
    Circle(2.99)

    then arrays.sort(Circles) will make it look like

    Circle(1.00)
    Circle(2.00)
    Circle(2.99)

    My teacher said to use Math.signum but i have no idea how to implement that into my code. Here is what i have
    Java Code:
    import java.util.Arrays;
    /////////////////////////////////////////////////////////////////////////
    class Hw11
    {
    //-----------------------------------------------------------------------
       public static void main ( String [] args ) throws Exception
       {
          Circle[] circles = { new Circle(3),
                               new Circle(4),
                               new Circle(3.01),
                               new Circle(1),
                               new Circle(2),
                               new Circle(2.99) };
    
          for ( Circle c : circles ) System.out.println(c);
    
          Arrays.sort(circles);
    
          System.out.println();
          for ( Circle c : circles ) System.out.println(c);
       }
    //-----------------------------------------------------------------------
    } // end class Hw14
    /////////////////////////////////////////////////////////////////////////
    class Circle implements Comparable
    {
       private double radius;
    
       public Circle (double radius) 
       {
          this.radius = radius;
       }
       public int compareTo (Object c) 
       {
          Circle that = (Circle) c;
          return (int)( this.radius - that.radius );
       }
       public String toString () 
       {
          return String.format("%s%.2f%s", "Circle(",radius,")");
       }
       
    }// end class Circle
    /////////////////////////////////////////////////////////////////////////

  2. #2
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Implement Comparable interface

    Anyone? Thanks in advance

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Implement Comparable interface

    Does your sort work? What happens when you cast a "double" to an "int"? You get truncation so you are returning an incorrect value from the compareTo() method.

    signum() is just a method that converts any negative value to -1 and any positive value to +1, so now when you cast from a double to an int you don't have to worry about truncation.

    Here is some simple code for you to play with:
    Java Code:
    public class Test
    {
    	public static void main(String args[]) throws Exception
    	{
    		double a = 2.1;
    		double b = 3.0;
    		double c = b - a;
    		System.out.println( c );
    		System.out.println( (int)c );
    		System.out.println( (int)Math.signum(c) );
    	}
    }
    Anyone? Thanks in advance
    Don't be so impatient. It's only been 30 minutes. People answer questions when they have time, not because you posted a question. Next time I'll delete my answer.
    Last edited by camickr; 11-07-2011 at 07:02 AM.

  4. #4
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Implement Comparable interface


    Don't be so impatient. It's only been 30 minutes. People answer questions when they have time, not because you posted a question. Next time I'll delete my answer.
    Yeah i figured that was coming. I just have a due date but thank you for your response. I appreciate the help and definitely needed it!

Similar Threads

  1. comparable interface, generic types
    By dylandrop in forum New To Java
    Replies: 3
    Last Post: 04-16-2011, 07:45 PM
  2. I don't understand comparable Interface Please help me
    By chathura87 in forum New To Java
    Replies: 11
    Last Post: 03-01-2011, 06:48 AM
  3. Replies: 6
    Last Post: 01-18-2011, 02:31 AM
  4. Comparable Interface
    By Yelrubk in forum New To Java
    Replies: 3
    Last Post: 04-28-2010, 02:46 PM
  5. interface Comparable<T> problem
    By Lennon-Guru in forum New To Java
    Replies: 3
    Last Post: 03-05-2008, 12: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
  •