Quote:
import java.awt.Rectangle;
/**
SelectionSorter2 class sorts an array of Rectangles
based on the x-coordinate of the top left corner of each rectangle.
It uses the selection sort algorithm.
*/
public class SelectionSorter2
{
private Rectangle[] a;
/**
Constructs the selection sorter
@param anArray the array to sort
*/
public SelectionSorter2 (Rectangle[] anArray)
{
a = anArray;
}
/**
Sorts the array managed by this selection sorter
*/
public void sort ()
{
for (int i = 0 ; i < a.length - 1 ; i++)
{
int minPos = minimumPosition (i);
swap (minPos, i);
}
}
/**
Finds the smallest element in a tail region of the array.
The elements are Rectangle objects in this case, and the
comparison is based on the x-coordinate of the top left corner.
@param from the first position of the tail region
@return the position of the smallest element in tail region
*/
private int minimumPosition (int from)
{
//Reminder: do not write or change code outside of the todo regions.
//----------------------Start here. Do not remove this comment.
// todo: Fill in this method
// approximate lines of code required: 5
int minPos = from;
for (int i = from + 1 ; i < a.length ; i++)
if (a [i].getX() < a [minPos].getX())
minPos = i;
return minPos;
//----------------------End here. Do not remove this comment.
}
/**
Swaps two entries of the array
@param i the first position to swap
@param j the second position to swap
*/
private void swap (int i, int j)
{
//Reminder: do not write or change code outside of the todo regions.
//----------------------Start here. Do not remove this comment.
// todo: fill in this method
// approximate lines of code required: 3
double temp = a [i].getX();
a [i] = a [j];
a [j] = temp;
//----------------------End here. Do not remove this comment.
}
}
the above is the class file which didnt run properly . Anyone can tell me wheres the problem?
main file
Quote:
import java.util.* ;
public class SelectionSorterTester2
{
public static void main(String[] args)
{
Rectangle[] a = randomRectangleArray(40, 100) ;
SelectionSorter2 sorter = new SelectionSorter2(a) ;
System.out.println(toString(a)) ;
sorter.sort() ;
System.out.println("----------Sorted:") ;
System.out.println(toString(a)) ;
System.out.println("--------------------------------") ;
}
/**
Returns a string representation of the array of Rectangles
@param array the array to make a string from
@return a string like [a1, a2, ..., a_n]
*/
public static String toString(Rectangle[] array)
{
String result = "[" ;
for (int i = 0 ; i < array.length - 1; i++) {
result += array[i].getX() + ", " ;
}
result += array[array.length - 1].getX() + "]" ;
return result ;
}
/**
Creates an array filled with random Rectangle values.
@param length the length of the array
@param n the number of possible random values
@return an array filled with length random values
*/
public static Rectangle[] randomRectangleArray(int length, int n)
{
Rectangle[] a = new Rectangle[length] ;
Random random = new Random(53) ;
for (int i = 0 ; i < length ; i++) {
a[i] = new Rectangle(random.nextInt(n), random.nextInt(n),
random.nextInt(n), random.nextInt(n)) ;
}
return a ;
}
}
TKS