uhhsoup
uhhsoup

Reputation: 11

Using comparable in a method in the main method

I am trying to check the values of a comparable method and assign the 1 and -1 value to true and false. I made a simple if else statement that does just that but I want to make it inside of a method so I can use it multiple times in my main method. When I try to do this I get an error that my compareTo method (in another class) is "undefined for the type Object".

Here is my code for both the compareTo method and my attempt of using this in my test class.

public int compareTo(FPNumber o) {
    if ((exp == o.exp) && (fraction - o.fraction < SMALL))
        return 1;
    else if ((exp == o.exp) || (fraction - o.fraction > SMALL))
        return -1;
    else
        return 0;
}
public String compare(Object FP1, Object FP2) {
    if (FP1.compareTo(FP2) == 1)
        System.out.println("true");
    else if (FP1.compareTo(FP2) == -1)
        System.out.println("false");
    else
        System.out.println("error");
}

Upvotes: 1

Views: 350

Answers (1)

hfontanez
hfontanez

Reputation: 6168

Let me start by a simple example using raw values, and then expand it to use objects.

Suppose you have two variables x and y that hold integer values. If I ask you, how do you know if the values for these variables are equal? The question is answered by simple math: if the values of the two variables are equal, the difference between the two must be zero. For example, 5 - 5. In this case, the difference is zero because both variables hold the value of positive 5.

What if they are different? Let x = 5 and y = 13.

  1. x - y = -8 (this means that x < y)
  2. y - x = 8 (same as above)

As you can see, when the values are different, it is not always going to be 1 or -1. This is important when you are comparing more than two values. Let's introduce z = 20. If comparing x to y and x to z and the result was -1 on both comparisons, the implication is that y and z must be equal but they are not.

What about when comparing objects? It is the same principle. Even when an object holds multiple variables, you must decide a hierarchy to determine which variable is more or less important in the comparison. Consider the following example

public class Person implements Comparable<Person> {
    private String firstName;
    private String lastName;
    private int age;
    ...
    public int compareTo(Person other) {...}
}

I can decide that, for my system, when comparing two Person objects, I must check first the last name, then the first name, and lastly the age.

public int compareTo(Person other) {
    int i = lastName.compareTo(other.lastName);
    if (i != 0) return i;

    i = firstName.compareTo(other.firstName);
    if (i != 0) return i;

    return Integer.compare(age, other.age);
}

Basically, if the last names are the same (i == 0), it will compare the first names. Then if the first names are the same, it will compare the ages. At any point, if the values are different, the difference will be returned. Again, not a 1 or -1. So basically, to convert the results to boolean, the logic is

public boolean compare(Person person, Person other) {
    if (person.compareTo(other) == 0) return true;
    else return false;
}

By the way, your original code has a compilation error because your compare method should return a String and it returns void. Instead of using System.out.print() inside your method, like you have now, you should print out the output of the method.

public String compare(Object FP1, Object FP2) {
        if (FP1.compareTo(FP2) == 1)
            return "true";
        else if (FP1.compareTo(FP2) == -1)
            return "false";
        else
            return "error";
}

...

System.out.println(compare(FP1, FP2));

UPDATE: I forgot to mention before that, essentially, the compare function I included here is serving basically the same function as the equals() method. Also, because this function is provided by a third party, it is sort of what a Comparator should do. Because of that, it should be done following best practices of what a Comparator should do. For my Person comparator, you may have two Comparators: one that compares age and one that compare names.

Upvotes: 2

Related Questions