CompareTo Method: Return Type: GREATER, EQUAL, LESS

68 views Asked by At

I'm receiving these 4 errors: character expected errors character expected errors I'm doing a LinkedList java project that requires the implementation of the CompareTo method. I am rusty on Java and would like some help regarding what I am doing wrong regarding this implementaion. Should I use integers instead of enums? Should I use switch/case/break instead of if/else?

Here's my Java class file for class ItemType

import java.util.Objects;

public class ItemType {
    private int value;

    public ItemType() {
        // Constructor body can be empty if there's no initialization code needed
    }

    public enum compareTo(ItemType item) {
        if (this.getValue() < item.getValue()) {
            return LESS;
        } else if (this.getValue() > item.getValue()) {
            return GREATER;
        } else {
            return EQUAL;
        }
    }

    public int getValue() {
        return value;
    }

    public void initialize(int num) {
        value = num;
    }

    // Assuming Comparison is an enum, you would also need to define it in Java
    //public enum Comparison {
        //LESS,
        //GREATER,
        //EQUAL
    //}
}

Here's an example of the SortedLinkedList.java class using the ItemType class method compareTo()

while (location.next != null && brake == 0) {
      switch (item_.compareTo(location.item)) {
            case ItemType.GREATER:
                preLoc = location;
                location = location.next;
                break;
            case ItemType.EQUAL:
                System.out.println("\nError: Duplication\n");
                return;
            case ItemType.LESS:
                brake = 1;
                break;
            }
}
3

There are 3 answers

7
Romario On BEST ANSWER

You actually do not need your enum. Try to implement Comparable interface. You can take a look how to do it here.

Then rework your switch case to work with integer like:

    switch (item_.compareTo(location.item)) {
        case 1:
            preLoc = location;
            location = location.next;
            break;
        case 0:
            System.out.println("\nError: Duplication\n");
            return;
        case -1:
            brake = 1;
            break;
    }

Happy coding!

UPD

Example how to implement (be aware it is not final version and you can provide your own implementation):

    public class ItemType implements Comparable<ItemType> {
    private int value;

    public ItemType() {
        // Constructor body can be empty if there's no initialization code needed
    }

    @Override
    public int compareTo(ItemType o) {
        if (this.value > o.value) {
            return 1;
        }

        if (this.value == o.value) {
            return 0;
        }

        return -1;
    }
}
0
Jens On

you use the keyword enum as return type insteat of the Enum itself, that's why you get the error message:

public Comparison compareTo(ItemType item)

But I would use the in value, because it is the common way to use compareTo in java

0
Radek Wroblewski On

There is so much wrong here...

public enum compareTo(ItemType item)...

incorrectly declares an internal enum called compareTo, not a method returning an enum.

If you for whatever reason want to return this enum you need to declare it

public enum Comparison {
    LESS, GREATER, EQUAL
}

then declare it as a return type of your method and refer the enum type:

public Comparison compareTo(ItemType item)...

and then in your SortedLinkedList implementation refer to Comparison.LESS, etc, not ItemType.LESS.

Or you could just make ItemType implement Comparable interface, implement the compareTo method and use built in mechanisms.