my dog learned polymorphism*
The moose likes Beginning Java and the fly likes method overloading in sort program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method overloading in sort program" Watch "method overloading in sort program" New topic
Author

method overloading in sort program

Kelly Frasure
Greenhorn

Joined: Mar 03, 2001
Posts: 15
I need to add an overloaded method to this Sort program. The overloaded method is to be used to sort objects. I'm not quite sure how to write the overloaded method. This is what I have so far. It does not compile. It say int[] is already defined.
-----------------------------------------------------------------
public class Sorts
{
//-------------------------------------------------------------
// Sorts the specified array of integers using the selection
//sort algorigthm.
//-------------------------------------------------------------
public static void selectionSort (int[] numbers)
{
int min, temp;
for (int index = 0; index < numbers.length-1; index++)
{
min = index;
for (int scan = index+1; scan < numbers.length; scan++)
if (numbers[scan] < numbers[min])
min = scan;
//Swap the values
temp = numbers[min];
numbers[min] = numbers[index];
numbers[index] = temp;
}
}
//-------------------------------------------------------------
// Sorts the specified array of objects using the selection
// sort algorithm.
//-------------------------------------------------------------
public static void selectionSort (int[] objects)
{
int min, temp;
for (int index = 0; index < objects.length-1; index++)
{
min = index;
for (int scan = index+1; scan < objects.length; scan++)
if (objects[scan] < objects[min])
min = scan;
//Swap the values
temp = objects[min];
objects[min] = objects[index];
objects[index] = temp;
}
}
//-------------------------------------------------------------
// Sorts the specified array of integers using the insertion
// sort algorithm
//------------------------------------------------------------- public static void insertionSort (int[] numbers)
{
for (int index = 1; index < numbers.length; index++)
{
int key = numbers[index];
int position = index;
//Shift larger values to the right
while (position > 0 && numbers[position-1] > key)
{
numbers[position] = numbers[position-1];
position--;
}
numbers[position] = key;
}
}
//-------------------------------------------------------------
//Sorts the specified array of objects using the insertion
//algorithm.
//-------------------------------------------------------------
public static void insertionSort (Comparable[] objects)
{
for (int index = 1; index < objects.length; index++)
{
Comparable key = objects[index];
int position = index;
while (position > 0 && objects[position-1].compareTo(key) > 0)
{
objects[position] = objects[position-1];
position--;
}
objects[position] = key;
}
}
}
Rikard Qvarforth
Ranch Hand

Joined: Jul 10, 2001
Posts: 107
When you overload a function you must have a diffrent argumentlist, or else can't the compiler deside witch function to use.
ex. xxx base(int[] xxx )
xxx base(long[] xxx)
thats what the compiler message
"int [] already in use" means..
excuse me for my bad english.
hope this helps !!

[This message has been edited by Rikard Qvarforth (edited July 16, 2001).]
Paul Selby
Ranch Hand

Joined: Oct 20, 2000
Posts: 41

I would suggest having another look at the error message. It looks as if it is really compaining about the method selectionSort() being duplicated. (Both selectionSort() methods have the same int[] parameters.)
The overriding of the method insertionSort() is valid. So, you look as if you are on the right track, and just need to fix the selectionSort() as you have done insertionSort(). It looks as if you have simply been thrown off track by the error message.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method overloading in sort program