• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting an ArrayList of Geometric Objects by area from smallest to largest

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello I have to create a method with the following header :


public static <E extends Comparable<E> > void sort ( ArrayList<E> list , int left, int right)

i also had to create a swap cells method and position of max integer method. and also had to read the preserved data file in with a scanner. I implemented the comparable interface I am having difficulty sorting my list by the area. It has to be in desscending order. Can someone please help me with my sorting method?

Geometric Object class: since it has comparator also am interested if i need to change this?

CODE:

Driver:


output:

Position of max area is...4
[>>> Circleblue
GeometricObject [color=blue, filled=true, dateCreated=Tue Mar 18 17:33:27 EDT 2014]
>>>Radius2.0Area: 12.566370614359172, >>> Circleblue
GeometricObject [color=blue, filled=true, dateCreated=Tue Mar 18 17:33:27 EDT 2014]
>>>Radius10.0Area: 314.1592653589793,
>>>Rectangleyellow
GeometricObject [color=yellow, filled=true, dateCreated=Tue Mar 18 17:33:27 EDT 2014]
>>>Height:6.0Width:10.0Area:60.0,
>>>Rectanglegreen
GeometricObject [color=green, filled=true, dateCreated=Tue Mar 18 17:33:27 EDT 2014]
>>>Height:11.0Width:5.0Area:55.0, >>> Circlered
GeometricObject [color=red, filled=false, dateCreated=Tue Mar 18 17:33:27 EDT 2014]
>>>Radius4.0Area: 50.26548245743669]
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain to me in words not code how your sorting algorithm is supposed to work (and assume you are explaining it to a 10 year old).
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And Welcome to the Ranch.
 
Danielle Aring
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want my sorting algorithm to basically check the entire list beginning from the left of the list (the beginning) to the right of the list (the end). What Id like it to do is compare the areas of each object as it traverses the list and moves those objects with a larger area to the right and those with the smaller areas to the left. until it is ordered from smallest to largest.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm.. so you find the position of the object with the max area... then you ignore it and swap left with right. I dunno.. doesn't sound right to me!
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should start by learning about sorting algorithms.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This looks pretty complicated. If your objects implement Comparable, then you don't have to do swapping on your own - the Collections.sort method does that for you. The descending order would be taken care of by calling Collections.reverse afterwards.

So unless I'm missing something the positionOfMaxArea, swapCells and sort methods are all superfluous. The compareTo method looks right, assuming that getArea is implemented correctly in all classes.

Or is this an academic exercise for you to implement sorting without using the Collections API?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic