-1

I am trying to sort the data in the array.
I have already created a method that sorts the data but for some reason, I can not call this method correctly in the main method.

Something doesn't work. The driver class has to call up the method and send in the correct parameters. I tried to do so but it doesn't work. Something is wrong with the parameters. This program is supposed to receive data and sort it.

public class Kennel
    {
        public static <T extends Comparable<T>>
                  void selSort(T[] doggy)
        {
            //local variables
            T temp;
            Dog doggy[ ] = new Dog [MAX_DOGS];       //dog array
            int outerLoop;         //  Outer loop counter
            int innerLoop;         //Inner loop counter
            int location;          //Location of smallest value
            int n = doggy.length;                 //number of items in the array
        
            /***************************************************/
        
            
            for (outerLoop = 0; outerLoop < n - 1; outerLoop++)
            {
                //
                location = outerLoop;
        
                //
                for (innerLoop = outerLoop + 1; innerLoop < n; innerLoop++)
                {
                    if (doggy[location].compareTo((doggy[innerLoop])) > 0)
                    {
                       location = innerLoop;
        
                    }//end IF
        
                }//end FOR
        
                >swap the value of the old smallest with the value of the new smallest<
                T iSwap = doggy[location];
                doggy[location] = doggy[outerLoop];
                doggy[outerLoop] = iSwap;
        
            }//end FOR
        
        }//end selSort

main method

    public static void main(String args[])
    {
        mid = new Kennel();
        mid.selSort(mid.doggy);
    }

When I compile the code I get an error:

 117: error: method selSort in class Kennel cannot be applied to given types;
                mid.selSort(mid.doggy);
                   ^
   required: T[]
   found: Dog[]
   reason: inferred type does not conform to upper bound(s)
     inferred: Dog
     upper bound(s): Comparable<Dog>
   where T is a type-variable:
     T extends Comparable<T> declared in method <T>selSort(T[])
 1 error

Not sure how to fix this problem.

user207421
  • 305,947
  • 44
  • 307
  • 483
James
  • 1
  • 2

1 Answers1

1

A couple points.

  • your Dog class should look something like
class Dog implements Comparable<Dog> {
  // fields, constructor, etc here

  public int compareTo(Dog dog) {
     ...
     return val; // where val is an int depending on result of comparison
  }
  // other methods here.
}
  • It makes no sense to have a Dog array in the sort method and also accept one as an argument. The Dog array will be sorted in place so no internal array of dogs is required.

  • You need to populate a Dog array with Dog objects and pass that array to the sort method.

Also check how-to-use-comparator-in-java-to-sort

WJS
  • 36,363
  • 4
  • 24
  • 39
  • Thanks, I am a little confused about compareTo method. Could you please give explain more? – James Mar 16 '21 at 03:10
  • 1
    @James `Comparable` and `compareTo` have been covered many many times already on Stack Overflow. Search to learn more. And study the Java Tutorials provided free-of-cost by Oracle. – Basil Bourque Mar 16 '21 at 03:21
  • @BasilBourque yeah u right. Im sorry im dumb lol – James Mar 16 '21 at 03:27