pinkop
12-13-2006, 04:25 AM
hello i have been working on this for a little while now and i thought i had it but i somehow got everything mixed up and changed some stuff :confused:
ok what i needed it to do was sort an array of runner objects by the place they finished in a race.
my runner class is supposed to implement the Comparable interface, have equals and compareTo methods
i am trying to use insertion sorting to do this
diggy is my driver class
public class diggy
{
//-----------------------------------------------------------------
// Creates an array of Runner objects, sorts them, then prints
// them.
//-----------------------------------------------------------------
public static void main (String[] args)
{
Runner[] people = new Runner[10];
people[0] = new Runner ("Jack Sprat",10);
people[1] = new Runner ("Cecil Litwack",8 );
people[2] = new Runner ("Dina Might", 3);
people[3] = new Runner ("Seymour Butts",4 );
people[4] = new Runner ("Ima Reject", 1);
people[5] = new Runner ("Ivan Earache",9 );
people[6] = new Runner ("Ace Ventura",6 );
people[7] = new Runner ("Joan O'farc",7 );
people[8] = new Runner ("Jack O'Lantern", 5);
people[9] = new Runner ("Jim Nasium", 2);
Sorting.insertionSort(people[].getPlace());
for (Runner friend : people)
System.out.println (friend);
}
}
i really need help with the compareto and equals methods
public class Runner implements Comparable
{
private String Name;
int place;
//-----------------------------------------------------------------
// Constructor: Sets up this contact with the specified data.
//-----------------------------------------------------------------
public Runner (String first, int place)
{
Name = first;
this.place=place;
}
//-----------------------------------------------------------------
// Returns a description of this contact as a string.
//-----------------------------------------------------------------
public String toString ()
{
return Name + "\t" + place;
}
//-----------------------------------------------------------------
// Returns a description of this contact as a string.
//-----------------------------------------------------------------
public boolean equals (Object other)
{
return ();
}
//-----------------------------------------------------------------
// Uses place to determine ordering.
//-----------------------------------------------------------------
public int compareTo (Object other)
{
int result=0;
int placer = ((Runner)other).getPlace();
if (place>placer)
result =1;
if (place<placer)
result = -1;
return result;
}
//-----------------------------------------------------------------
// First name accessor.
//-----------------------------------------------------------------
public String getName ()
{
return Name;
}
//-----------------------------------------------------------------
// place accessor.
//-----------------------------------------------------------------
public int getPlace()
{
return place;
}
}
would i need to change the insertion sort for this to work right? if so what would that change be
public class Sorting
{
//-----------------------------------------------------------------
// Sorts the specified array of objects using the selection
// sort algorithm.
//-----------------------------------------------------------------
public static void selectionSort (Comparable[] list)
{
int min;
Comparable temp;
for (int index = 0; index < list.length-1; index++)
{
min = index;
for (int scan = index+1; scan < list.length; scan++)
if (list[scan].compareTo(list[min]) < 0)
min = scan;
// Swap the values
temp = list[min];
list[min] = list[index];
list[index] = temp;
}
}
//-----------------------------------------------------------------
// Sorts the specified array of objects using the insertion
// sort algorithm.
//-----------------------------------------------------------------
public static void insertionSort (Comparable[] list)
{
for (int index = 1; index < list.length; index++)
{
Comparable key = list[index];
int position = index;
// Shift larger values to the right
while (position > 0 && key.compareTo(list[position-1]) < 0)
{
list[position] = list[position-1];
position--;
}
list[position] = key;
}
}
}
thank you so much for any help
(this is homework and normally i wouldnt ask for help but I'm stuck ><)
ok what i needed it to do was sort an array of runner objects by the place they finished in a race.
my runner class is supposed to implement the Comparable interface, have equals and compareTo methods
i am trying to use insertion sorting to do this
diggy is my driver class
public class diggy
{
//-----------------------------------------------------------------
// Creates an array of Runner objects, sorts them, then prints
// them.
//-----------------------------------------------------------------
public static void main (String[] args)
{
Runner[] people = new Runner[10];
people[0] = new Runner ("Jack Sprat",10);
people[1] = new Runner ("Cecil Litwack",8 );
people[2] = new Runner ("Dina Might", 3);
people[3] = new Runner ("Seymour Butts",4 );
people[4] = new Runner ("Ima Reject", 1);
people[5] = new Runner ("Ivan Earache",9 );
people[6] = new Runner ("Ace Ventura",6 );
people[7] = new Runner ("Joan O'farc",7 );
people[8] = new Runner ("Jack O'Lantern", 5);
people[9] = new Runner ("Jim Nasium", 2);
Sorting.insertionSort(people[].getPlace());
for (Runner friend : people)
System.out.println (friend);
}
}
i really need help with the compareto and equals methods
public class Runner implements Comparable
{
private String Name;
int place;
//-----------------------------------------------------------------
// Constructor: Sets up this contact with the specified data.
//-----------------------------------------------------------------
public Runner (String first, int place)
{
Name = first;
this.place=place;
}
//-----------------------------------------------------------------
// Returns a description of this contact as a string.
//-----------------------------------------------------------------
public String toString ()
{
return Name + "\t" + place;
}
//-----------------------------------------------------------------
// Returns a description of this contact as a string.
//-----------------------------------------------------------------
public boolean equals (Object other)
{
return ();
}
//-----------------------------------------------------------------
// Uses place to determine ordering.
//-----------------------------------------------------------------
public int compareTo (Object other)
{
int result=0;
int placer = ((Runner)other).getPlace();
if (place>placer)
result =1;
if (place<placer)
result = -1;
return result;
}
//-----------------------------------------------------------------
// First name accessor.
//-----------------------------------------------------------------
public String getName ()
{
return Name;
}
//-----------------------------------------------------------------
// place accessor.
//-----------------------------------------------------------------
public int getPlace()
{
return place;
}
}
would i need to change the insertion sort for this to work right? if so what would that change be
public class Sorting
{
//-----------------------------------------------------------------
// Sorts the specified array of objects using the selection
// sort algorithm.
//-----------------------------------------------------------------
public static void selectionSort (Comparable[] list)
{
int min;
Comparable temp;
for (int index = 0; index < list.length-1; index++)
{
min = index;
for (int scan = index+1; scan < list.length; scan++)
if (list[scan].compareTo(list[min]) < 0)
min = scan;
// Swap the values
temp = list[min];
list[min] = list[index];
list[index] = temp;
}
}
//-----------------------------------------------------------------
// Sorts the specified array of objects using the insertion
// sort algorithm.
//-----------------------------------------------------------------
public static void insertionSort (Comparable[] list)
{
for (int index = 1; index < list.length; index++)
{
Comparable key = list[index];
int position = index;
// Shift larger values to the right
while (position > 0 && key.compareTo(list[position-1]) < 0)
{
list[position] = list[position-1];
position--;
}
list[position] = key;
}
}
}
thank you so much for any help
(this is homework and normally i wouldnt ask for help but I'm stuck ><)