Results 1 to 8 of 8
  1. #1
    romero4742 is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Question Help with implements Comparable

    Hey guys so I had this hmk assignment due today I was not able to turn it in because I am completely lost. I have tried immitating the way my professor did an in class example but it is not working. Anyways, can someone help me at least give me a hint on what I am doing wrong. I'll really appreciate it. :)

    **BTW the point of the program is to first display the size of the circles as they are and then order them from lowest to highest.**

    import java.io.*;
    import java.util.Scanner;
    import java.util.Arrays;
    /////////////////////////////////////////////////////////////////////////
    class Hw14
    {
    //-----------------------------------------------------------------------
    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 o)
    {
    Circle that = (Circle) o;
    return(int) (this.radius - that.radius);
    }
    //-----------------------------------------------------------------------
    public String toString()
    {
    return String.format("%5.2",radius);
    }
    //-----------------------------------------------------------------------
    } // end class Circle
    /////////////////////////////////////////////////////////////////////////

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Help with implements Comparable

    but it is not working
    Define not working (Exceptions? Does it compile?...). And please wrap your code in the code tags for proper formatting.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help with implements Comparable

    Does your code compile? If not and you can't understand the compiler's messages, post them.

    it is not working
    Does the code compile but do something unwanted or unintended at runtime? In that case, say what the program *does* do and, in the case of runtime exceptions, post the entire stack trace and say which lines of your code it is referring to.

  4. #4
    romero4742 is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Re: Help with implements Comparable

    Well when I run the program this comes out... like I said I'm very lost in this assignment

    edu.csupomona.cs.cs141.Circle@73795327
    edu.csupomona.cs.cs141.Circle@224260ab
    edu.csupomona.cs.cs141.Circle@33d88c1f
    edu.csupomona.cs.cs141.Circle@4dd761d0
    edu.csupomona.cs.cs141.Circle@7756c69c
    edu.csupomona.cs.cs141.Circle@620968f9

    Exception in thread "main"
    java.lang.ClassCastException: edu.csupomona.cs.cs141.Circle cannot be cast to java.lang.Comparable
    at java.util.ComparableTimSort.countRunAndMakeAscendi ng(ComparableTimSort.java:290)
    at java.util.ComparableTimSort.sort(
    ComparableTimSort.java:157)
    at java.util.ComparableTimSort.sort(
    ComparableTimSort.java:146)
    at java.util.Arrays.sort(
    Arrays.java:472)
    at edu.csupomona.cs.cs141.Hw14.main(
    Hw14.java:26)

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help with implements Comparable

    That stack trace doesn't come from the code you posted.

    That code defines two classes - Hw14 and Circle - neither of which are in a package. The stack trace is talking about another class - edu.csupomona.cs.cs141.Circle

    Perhaps it would help if you describe how you are compiling and running the program.

    Or... Put the code you posted into 2 files. Hw14.java should contain the code for the Hw14 class and Circle.java should contain the code for the circle class. (You can put all the code in the Hw14.java file, but it better to have one class per source file). Make sure these .java files are the only thing in some directory (folder) then, using the command line/terminal navigate to that folder and compile and run with

    Java Code:
    javac -cp . *.java
    java -cp . Hw14
    The reason for the dash-c-p-space-dot is to set the location of the classpath (the place where classes are looked for) to the directory containing your source files. And the reason for *that* is so that the other Circle class (edu.csupomona.cs.cs141.Circle) is not accessed.

    -----

    If you use the code you originally posted you will still get a runtime exception, but not the one involving the bogus Circle class not implementing Comparable.

  6. #6
    romero4742 is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Re: Help with implements Comparable

    [QUOTE=pbrockway2;284723]That stack trace doesn't come from the code you posted.

    That code defines two classes - Hw14 and Circle - neither of which are in a package. The stack trace is talking about another class - edu.csupomona.cs.cs141.Circle
    QUOTE]

    Oh yeah I just stopped using eclipse and copied everything to a .txt file to compile it from the command line. Also I solved most of the problems I was only missing the f in the String.format(%5.2,radius);
    Thank you for your responses :)

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help with implements Comparable

    Great. I guess you also spotted that the cast to (int) was making the comparison a little inaccurate.

  8. #8
    romero4742 is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Re: Help with implements Comparable

    Quote Originally Posted by pbrockway2 View Post
    Great. I guess you also spotted that the cast to (int) was making the comparison a little inaccurate.
    :D yes I did I ended up using the Double.compare(this.radius, that.radius) and it worked perfectly
    :)

Similar Threads

  1. Comparable interface
    By diamonddragon in forum New To Java
    Replies: 17
    Last Post: 02-01-2012, 12:07 AM
  2. Comparable checks
    By Billywizz in forum New To Java
    Replies: 6
    Last Post: 12-07-2011, 04:37 AM
  3. Comparable and Comparator
    By jeanjiang in forum New To Java
    Replies: 7
    Last Post: 04-23-2011, 07:59 AM
  4. Got hit by the Comparable block again, help pls.
    By niu_niu in forum New To Java
    Replies: 7
    Last Post: 06-24-2010, 09:49 AM
  5. Comparable Interface
    By Yelrubk in forum New To Java
    Replies: 3
    Last Post: 04-28-2010, 02:46 PM

Tags for this Thread

Posting Permissions

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