-2

I have a Sorting.java file, with the following structure:

public class Sorting {
    public static <T> void mergeSort(T[] arr, Comparator<T> comparator) {
    (...)
    }
}

My goal is to test the mergeSort() method in a separate Driver.java file.

To that effect, here is what I included in that driver file:

import java.util.Comparator;
import java.io.*;
import java.util.*;

public class Driver {
    public static void main(String[] args) {
        int[] testMergeSort1 = { 9, 13, 5, 6, 12, 10, 3, 7, 2, 8, 0, 15, 1, 4, 14, 11 };
        mergeSort(testMergeSort1, IntegerComparator);
        System.out.println(Arrays.toString(testMergeSort1));
    }
}

And, per the accepted answer to this similar question, I added the following implementation of an Integer comparator to that same Driver.java file:

public class IntegerComparator implements Comparator {
    public int compare(Integer a, Integer b) {
        return a.intValue() - b.intValue();
    }

    public int equals(Object obj) {
        return this.equals(obj);
    }
}

However, when I compile Driver.java, I get the following error:

Driver.java:33: error: Driver.IntegerComparator is not abstract and does not override abstract method compare(Object,Object) in Comparator
    public class IntegerComparator implements Comparator {
           ^
Driver.java:38: error: equals(Object) in Driver.IntegerComparator cannot implement equals(Object) in Comparator
        public int equals(Object obj) {
                   ^
  return type int is not compatible with boolean

Any guidance on how to proceed to test the mergeSort() method from the Sorting.java file in the separate Driver.java file would be appreciated.

Thibaud Clement
  • 5,968
  • 8
  • 44
  • 96

1 Answers1

0

Try to implement a generic Comparator<Integer> and add @Override annotations, to check if you actually override the method in Comparator.

public class IntegerComparator implements Comparator<Integer> {
    @Override
    public int compare(Integer a, Integer b) {
        return a.intValue() - b.intValue();
    }
}
tobi1805
  • 77
  • 4
  • Thanks for your response. That makes sense. And then, in the call to the `mergeSort()` method, should I use IntegerComparator as parameter, or something else? – Thibaud Clement May 24 '22 at 15:30
  • 3
    I don't think overriding equals in this way does anything useful. – TomekK May 24 '22 at 15:33
  • @FedericoklezCulloca I still get the following error: `error: cannot find symbol mergeSort(testMergeSort1, IntegerComparator); symbol: variable IntegerComparator location: class Driver` Should the `IntegerComparator` class go into the Sorting.java file instead of the Driver.java file? – Thibaud Clement May 24 '22 at 15:34
  • 1
    @ThibaudClement you should call the static method with `Sorting.mergeSort(...)` in `Driver` – tobi1805 May 24 '22 at 15:38
  • @tobi1805 Thanks, this has been done. Now, I get: `Driver.java:29: error: cannot find symbol Sorting.mergeSort(testMergeSort1, IntegerComparator); symbol: variable IntegerComparator location: class Driver`. Should the IntegerComparator be implemented inside or outside of the `main()` method in the `Driver.java` file? – Thibaud Clement May 24 '22 at 15:43
  • @ThibaudClement is `IntegerComparator` in a different package? In that case you need to import it. – Federico klez Culloca May 25 '22 at 06:18