Thread: Implement Comparable interface

1. Member
Join Date
Sep 2011
Posts
29
Rep Power
0

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
{

public Circle (double radius)
{
}
public int compareTo (Object c)
{
Circle that = (Circle) c;
}
public String toString ()
{
}

}// end class Circle
/////////////////////////////////////////////////////////////////////////```

2. Member
Join Date
Sep 2011
Posts
29
Rep Power
0

Re: Implement Comparable interface

Anyone? Thanks in advance

3. Senior Member
Join Date
Jul 2009
Posts
1,236
Rep Power
9

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 08:02 AM.

4. Member
Join Date
Sep 2011
Posts
29
Rep Power
0

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!

Posting Permissions

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