Circle Sorting Help!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NycCraze88
    New Member
    • Dec 2007
    • 18

    #16
    If someone can IM me on BiG DoGG 536, this could go by alot quicker.

    Comment

    • NycCraze88
      New Member
      • Dec 2007
      • 18

      #17
      Here is my entire code. The thing looks fine, but I get a small error all the way down below in the main, where it says Collections.sor t(circles, circleComp) (I've bolded and underlined where Eclipse gives me the error warning). The error reads: "The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (ArrayList<Circ le>, SortCircle)"

      I dont know how to fix this and I think this is the only error preventing me from making this code work, so please, can someone just take a look at it and tell me whats wrong with it? Im so close yet so far lol.

      //Code
      package trials;

      import java.awt.Color;
      import java.awt.Point;
      import java.io.Seriali zable;

      public class Circle implements Serializable{

      private static final long serialVersionUI D = -920715980216052 8525L;
      public Point center;
      public int radius;
      public Color color;

      public Circle(Point center, int radius, Color color){
      this.center = center;
      this.radius = radius;
      this.color = color;
      }

      public Point getCenter() {
      return center;
      }

      public Color getColor() {
      return color;
      }

      public Integer getRadius() {
      return radius;
      }
      }
      ---------------------------------------------------------------------------------------

      package trials;

      import java.util.Array List;
      import java.util.Compa rator;


      public class CircleSort {

      private static final long serialVersionUI D = -920715980216052 8525L;
      public static int compareInts(int x, int y){
      if (x > y)
      return -1;
      else if (y > x)
      return 1;
      else
      return 0;
      }

      public static void SortCircle (ArrayList<Circ le> circles, Comparator<Circ le> compare){

      class CircleComparato r implements Comparator<Circ le> {

      public int compare(Circle cOne, Circle cTwo) {

      int result = compareInts(cOn e.getRadius(), cTwo.getRadius( ));
      if (cOne.getRadius ()== cTwo.getRadius( ))
      {
      result = compareInts(cOn e.getColor().ge tGreen(), cTwo.getColor() .getGreen());
      }
      else if (cOne.getColor( ).getGreen() == cTwo.getColor() .getGreen())
      {
      result = compareInts(cOn e.getColor().ge tBlue(), cTwo.getColor() .getBlue());
      }
      else if(cOne.getColo r().getBlue() == cTwo.getColor() .getBlue())
      {
      result = compareInts(cOn e.getColor().ge tRed(), cTwo.getColor() .getRed());
      }

      return result;
      }
      }
      }
      }
      -----------------------------------------------------------------------------------------------
      package trials;

      import java.awt.Color;
      import java.awt.Point;
      import java.io.FileOut putStream;
      import java.io.ObjectO utputStream;
      import java.util.Array List;
      import java.util.List;
      import java.util.Rando m;
      import java.util.Colle ctions;

      public class CircleSortMain {

      public static void main(String[] args){

      ArrayList<Circl e> circles = new ArrayList<Circl e>();
      Random rint = new Random();
      for(int i = 0; i < 5000; ++i){
      circles.add(
      new Circle(
      new Point(rint.next Int(500),rint.n extInt(500)),//sets center
      rint.nextInt(50 0),//sets radius
      new Color(rint.next Int(256),rint.n extInt(256),rin t.nextInt(256)) ));//sets color
      }
      ObjectOutputStr eam out = new ObjectOutputStr eam(new FileOutputStrea m("circles.arra ylist"));
      out.writeObject (circles);

      SortCircle circleComp = new SortCircle();
      Collections.sort(circles, circleComp);
      System.out.prin tln(circles + "/n");
      }
      }

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #18
        Things are going wrong in class CircleSort. It has a method called SortCircle that takes two parameters (circles and compare) that are never used. All this method does is declare a local class which implements Comparator. That's too tricky. I suggest you keep it simple -- define the comparator as a top-level class - not nested in anything.

        The logic of the comparator is also wrong. If should be
        - compare radius
        - if tied, compare green components
        - if still tied compare red components,
        - if still tied, compare blue components
        (I may have the order wrong there, but I hope the intent is clear.)

        Comment

        • NycCraze88
          New Member
          • Dec 2007
          • 18

          #19
          The logic of the comparator is exactly the way you said it should be isnt it? it first compares the radius, if equal, then compare the green color components and so forth. I took out the Sort circle method, but i still get an error in the main. How can I fix this error?

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #20
            Looking back on your original assignment, I'm not certain about the requirements:
            Your job is to:
            (a) Sort the circles by radius, smallest first
            (b) Sort the circles by the red component of their color, largest first, and for those with the same red component by the blue (largest first) and if the red and blue is the same, then by green (largest first). This requires three sorts, the last two of which need to be stable. First sort by green, then blue (stable) and finally by red( stable). Note that merge sort is fast and stable. Quick sort is fast, but not stable.
            That reads to me as a requirement that you are able to carry out two separate sorts: by radius and by color. Are you sure you are meant to only write one comparator: order by radius, then break ties by ordering by color?

            Assuming that, the logic in your comparator (reply #17) is wrong:
            [CODE=Java]
            int result = compareInts(cOn e.getRadius(), cTwo.getRadius( ));
            if (cOne.getRadius ()== cTwo.getRadius( )) {
            result = compareInts(cOn e.getColor().ge tGreen(), cTwo.getColor() .getGreen());
            } else if (cOne.getColor( ).getGreen() == cTwo.getColor() .getGreen()) {
            [/CODE]
            This reads:

            Compare radii; if they are equal, compare green else if green is equal... But if radii are different, you should be done!
            Also note that the original requirement was to sort by red, then blue, then green, and your code is (attempting) green, then blue, then red.

            I suggest you look more closely at my example in reply #9 -- note that my code does not have a single else in the comparator!!!

            As for writing a comparator class, reply #9 shows how to do that as well. Understand that code and you are done.

            Comment

            Working...