Search code examples
javainterfacebooleancomparator

How can I implement a comparator to compare two numbers?


I have a class OffbyOne where I declare an interface as a comparator called compare where I'm supposed to take two integers and return True if the difference between two integers is 1, and false if not.

Here is my code so far:

public static class OffbyOne {
        public interface Compare {
            int x;
            int y;
            if ((x-y) == 1) {
                return true;
            } else if ((y-x)==1) {
                return true;
            }
            return false
            public boolean equalChars(char x, char y); {
                if (Compare(x,y) == true) {
                    return true;
                }
                return false;
            }

I'm struggling to understand how comparators work in Java and what do I have to do. If anyone can please help me with this and then provide explanations for how it is supposed to be done, it would be great for me.

Thank you.


Solution

  • Comparator already exists in java - java.util.Comparator. It is something completely different from what you describe: Java's own comparator is a thing that you give 2 objects, and the comparator tells you which of the two 'comes earlier' in a sorting. Such an oracle is all you need to efficiently sort stuff.

    It's allowed, but a bad idea, to name types the same as core java types. You can make your own String, which is different from java's own String in all ways (your String does not in any way replace java's string), it's just a confusing name, is all. You're doing the same thing with Comparator here. Bad idea. I'd call it OffByOne or similar.

    declare an interface as a comparator called compare where I'm supposed to take two integers and return True if the difference between two integers is 1, and false if not.

    This makes no sense whatsoever. You must have misunderstood the assignment. An interface describes the what and does not describe the how, whereas what you just said is describing the how. That's just not what interfaces do - they don't get to define the how. They only define the what. You're describing an implementation, not an interface.

    public interface Compare {
                int x;
               int y;
                if ((x-y) == 1) {
    

    You can't stick code in types like this. You can stick only methods, fields, constructors, and other types in there. You can stick code in a method and then stick the method in a type, if you want. In addition, given that it is an interface, you can't stick code in one at all - interfaces define what a class can do, not how it does it (there is the default mechanism. That's definitely not what this assignments wants you to do so, so it doesn't apply here).

    This would be an interface:

    public interface DifferenceOfOne<T> {
      public boolean isDifferenceOfOne(T a, T B);
    }
    

    This says: There is such a thing as a 'DifferenceOfOne' implementation for any given type. Such a class would implement the method isDifferenceOfOne, which takes in 2 parameters, both of that given type, and which then returns a boolean.

    You can then make an implementation for the Integer type:

    class IntDiffOfOne implements DifferenceOfOne<Integer> {
      public boolean isDifferenceOfOne(Integer a, Integer b) {
        return (a - 1 == b || b - 1 == a);
      }
    }
    
    DifferenceOfOne<Integer> intDiffOfOne = new IntDiffOfOne();
    

    Or in more common, modern java syntax:

    DifferenceOfOne<Integer> intDiffOfOne = (a, b) -> (a - 1 == b || b - 1 == a);
    

    And someone else can write a DiffOfOne implementation that, I dunno, tells you if any 2 LocalDate instances differ by exactly 1 day, perhaps, that would be a DifferenceOfOne<LocalDate>.

    If this all sounds confusing to you - go back to whomever gave you this assignment, as either the assignment makes no sense, or you misheard/misunderstood it.