• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

method overloading in sort program

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
}
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic