Circle Sorting Help!!

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

    #1

    Circle Sorting Help!!

    Hey all,

    I'm in a Java data structures class in college and I have a problem with this one sorting assignment. I'm not very experienced in java and have tried tackling this assignment in an assortment of ways but have failed miserably. Basically, here is the assignment:::

    =============== =============== =============== ===========
    "If you look in http://csis.pace.edu/~bergin/temp/circles.zip , you will find a file containing, among other things, a file named circles.arrayli st. This file was created by writing an ArrayList of circle objects on an object output stream. It contains 5000 circle objects. The circle class and the main that created the circles file is also included in the zip. The data is random.

    You will need to include the trials.Circle class in your project. in particular, the package must be trials and you should not modify the circle class.

    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.

    Also build an adequete JUnit test to show that you have indeed sorted them as required.
    =============== =============== =============== ============


    Im really not to sure how to go about solving this. Im not asking you guys to solve this for me, but if someone could give me a push in the right direction and tell me what to do, I would appreciate it. Please keep in mind that Im an amateur programmer, and some things you guys will probably suggest might be out of my skill range, so a slight explanation wouldnt hurt :)

    Thanks in advance.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Have you learned how to write sorting methods and get them to work?

    Comment

    • NycCraze88
      New Member
      • Dec 2007
      • 18

      #3
      I'm not perfect with them but I think I can pull it off. What do you suggest?

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Well, then your program basically writes itself. You have a file that contains information about however many Circles - make a method to read them into an array/ArrayList/whatever and return that list/array. Then make a method to sort them by radius size. Then make a method to sort them by color (which, according to your problem, will be several methods combined). Then make a method to print out the circles in the array/list.

        Go ahead and give it a try and let us know where you get.

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          It's not clear from your assignment, but are you expected to code your own sort, or can you use the sorting methods in the API? If so, this is assignment is only a half page of code.

          Comment

          • NycCraze88
            New Member
            • Dec 2007
            • 18

            #6
            Well he said we could build our own sort programs or use something built into the java libraries to do the sorts...


            But anyway, I built one of my own that sorts the circle array....but Im not really sure how to adapt it so that it sorts the circles by radius, then by the green color component, then the blue, then red. I understand you're supposed to used comparators, just not sure how to do it.

            Here's my circle sorting code so far...
            =============== =============== =============== ============

            package trials;

            import java.awt.Color;
            import java.awt.Point;

            public class SortCircle {

            public static <Circle extends Comparable<Circ le>> void sort (Circle[] circles)
            {
            Circle currentMax;
            int currentMaxIndex ;

            for(int i = circles.length - 1; i >= 1; i--)
            { //Find the maximum in the list [0...i]
            currentMax = circles[i];
            currentMaxIndex = i;

            for (int j = i - 1; j >= 0; j--)
            {
            if (currentMax.com pareTo(circles[j]) < 0){
            currentMax = circles[j];
            currentMaxIndex = j;
            }
            }

            //Swap circles[i] with circles[currentMaxIndex] if necessary
            if (currentMaxInde x != i){
            circles[currentMaxIndex] = circles[i];
            circles[i] = currentMax;
            }
            }
            }

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              If you can use a sorting method like java.util.Array s.sort or java.util.Colle ctions.sort, I would prefer it to reinventing the wheel. Here is the tutorial page on Comparators:

              This collections Java tutorial describes interfaces, implementations, and algorithms in the Java Collections framework

              Comment

              • NycCraze88
                New Member
                • Dec 2007
                • 18

                #8
                Since I have to sort the circles in order ( By Radius first, if radius's are equal, sort by the green color component of the circles, if both greens are equal, then sort by the blue component and so forth) should I use nested If statements with sorts in each, or should I do something different?

                Comment

                • BigDaddyLH
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1216

                  #9
                  Originally posted by NycCraze88
                  Since I have to sort the circles in order ( By Radius first, if radius's are equal, sort by the green color component of the circles, if both greens are equal, then sort by the blue component and so forth) should I use nested If statements with sorts in each, or should I do something different?
                  A single comparator can order by one attribute first, then break times with a second attribute. For example:

                  [CODE=Java]import java.util.Compa rator;

                  class Person {
                  public String getLastName() {...}
                  public String getFirstName() {...}
                  ...
                  }

                  class NameComparator implements Comparator<Pers on> {
                  public int compare(Person x, Person y) {
                  int result = x.getLastName() .compareTo(y.ge tLastName());
                  if (result == 0) {
                  result = x.getFirstName( ).compareTo(y.g etFirstName());
                  }
                  return result;
                  }
                  }[/CODE]

                  Comment

                  • NycCraze88
                    New Member
                    • Dec 2007
                    • 18

                    #10
                    The code above is a big help, thanks dude.

                    Im off to class now but when Im back, I'll continue working on the code and i'll come back here if I need anymore help. Thanks everyone.

                    Comment

                    • NycCraze88
                      New Member
                      • Dec 2007
                      • 18

                      #11
                      Im still getting errors. Im trying to understand why this class wont extend the circle class (which contains the methods I need such as getRadius(), etc in order for me to be able to sort. Here's what I have so far..

                      Code:
                      package trials;
                      
                      import java.awt.Color;
                      import java.awt.Point;
                      import java.util.List;
                      import java.util.ArrayList;
                      import java.util.Comparator;
                      
                      public class CircleSort extends Circle{
                      	
                      	class CircleComparator implements Comparator<Circle> {
                      
                      	    public int compare(Circle cOne, Circle cTwo) {
                      	
                      	        int result = cOne.getRadius().compareTo(cTwo.getRadius());
                      	        if (cOne.getRadius() = cTwo.getRadius())
                      	        {
                      	            result = cTwo.getColor().getGreen().compareTo(cTwo.getColor().getGreen());
                      	        }
                      	        else if (cOne.getColor().getGreen() = cTwo.getColor().getGreen())
                      	        {
                      	        	result = cTwo.getColor().getBlue().compareTo(cTwo.getColor().getBlue());
                      	        }
                      	        else if(cOne.getColor().getBlue() = cTwo.getColor().getBlue())
                      	        {
                      	        	result = cOne.getColor().getRed().compareTo(cTwo.getColor().getRed());
                      	        }
                      	
                      	        return result;
                      	
                      	    }
                      	
                      	}
                      }
                      What now?

                      Comment

                      • NycCraze88
                        New Member
                        • Dec 2007
                        • 18

                        #12
                        I keep getting this error "Cannot invoke compateTo(int) on the primitive type int" when Im trying to compare the colors. How do I fix that. I also need to know how to implement the sort in the main.

                        Comment

                        • BigDaddyLH
                          Recognized Expert Top Contributor
                          • Dec 2007
                          • 1216

                          #13
                          Originally posted by NycCraze88
                          I keep getting this error "Cannot invoke compateTo(int) on the primitive type int" when Im trying to compare the colors. How do I fix that. I also need to know how to implement the sort in the main.
                          int is a primitive type, so ints are not objects. To compare ints I usually rely on a simple utility method like:

                          [CODE=Java]public static int compareInts(int x, int y) {
                          if (x < y)
                          return -1;
                          else if (x > y)
                          return 1;
                          else
                          return 0;
                          }[/CODE]
                          Or you can use the fact that the wrapper class Integer is comparable.

                          Comment

                          • NycCraze88
                            New Member
                            • Dec 2007
                            • 18

                            #14
                            Ok, but how would I incorporate that to my comparator? (I'm really bad at this lol)

                            Comment

                            • NycCraze88
                              New Member
                              • Dec 2007
                              • 18

                              #15
                              Nvm, I figured out how to incorporate it, I just need help with getting the sort to run in the main. How would I do that

                              Comment

                              Working...