Results 1 to 4 of 4
Thread: Implement Comparable interface
- 11-07-2011, 06:30 AM #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 { 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 /////////////////////////////////////////////////////////////////////////
- 11-07-2011, 06:54 AM #2
Member
- Join Date
- Sep 2011
- Posts
- 29
- Rep Power
- 0
Re: Implement Comparable interface
Anyone? Thanks in advance
- 11-07-2011, 06:59 AM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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) ); } }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.Anyone? Thanks in advanceLast edited by camickr; 11-07-2011 at 07:02 AM.
- 11-07-2011, 07:10 AM #4
Member
- Join Date
- Sep 2011
- Posts
- 29
- Rep Power
- 0
Re: Implement Comparable interface
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!
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.
Similar Threads
-
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 -
Having a real hard time with Comparable interface and comparing String length
By paulmmj in forum New To JavaReplies: 6Last Post: 01-18-2011, 02:31 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