Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 array sort (rectangle x top left coordinate )
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
nelsonkkyy




PostPosted: Tue Nov 22, 2011 8:57 pm   Post subject: array sort (rectangle x top left coordinate )

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
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Tue Nov 22, 2011 9:15 pm   Post subject: Re: array sort (rectangle x top left coordinate )

nelsonkkyy @ Tue Nov 22, 2011 8:57 pm wrote:

the above is the class file which didnt run properly . Anyone can tell me wheres the problem?

in which way did it not run properly? What was the output? What output did you expect?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
nelsonkkyy




PostPosted: Tue Nov 22, 2011 9:17 pm   Post subject: Re: array sort (rectangle x top left coordinate )

output should be a set of a number sorted from the smallest to the largest number according to the rectangle top left x coordinates
Tony




PostPosted: Tue Nov 22, 2011 9:33 pm   Post subject: RE:array sort (rectangle x top left coordinate )

and you get what instead?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
nelsonkkyy




PostPosted: Tue Nov 22, 2011 9:35 pm   Post subject: Re: array sort (rectangle x top left coordinate )

notings printed out cuz it has an error.
You cannot assign a "double" expression to a "java.awt.Rectangle" variable
Tony




PostPosted: Tue Nov 22, 2011 9:38 pm   Post subject: RE:array sort (rectangle x top left coordinate )

That does sound like a problem. Something like
code:

Rectangle a_rectangle = 42;

doesn't make much sense (this is just an example of what might cause that error. Check the line number to see what you are doing there).
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: