1

I'm working on a polynomial calculator. My problem is with the equals method. Here is the relevant code:

public class Poly{
    Term[] terms;

    //Constructors-------------------------------------------
    public Poly() {}
    public Poly(ArrayList<Term> Terms) {
        terms = Terms.toArray(new Term[Terms.size()]);
        Arrays.sort(terms, new TermComparator());
    }

    //Methods-------------------------------------------------
    public boolean equals(Poly x) {
        boolean q=false;
        if(this == x){
            q=true;
        }
    return q;
    }

    //used in constructor to order terms
    class TermComparator implements Comparator<Term> {
        @Override
        public int compare(Term t1, Term t2) {
            return t2.getExp() - t1.getExp();
        }
    }
}

The equals method always returns false even when two Poly objects have the same value. Can anyone help please?

3 Answers3

5

Your Poly class equals method should be like below

@Override
public boolean equals(Object obj) {
    if (this == obj) //checking both are same instance
        return true;
    if (obj == null) // checking obj should not be null
        return false;
    if (getClass() != obj.getClass()) //checking both objects from same class
        return false;
    Poly other = (Poly) obj; 

    return Arrays.equals(terms, other.terms);  //checking all the array values
}

if you are adding Poly objects to collection you need to implement hash code method too.

    @Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + Arrays.hashCode(terms);
    return result;
}   

Please refer

Why do I need to override the equals and hashCode methods in Java?

How should equals and hashcode be implemented when using JPA and Hibernate

Community
  • 1
  • 1
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
  • Is there a typo in your equals method? I get that return false is inside the {} for the last if statement but what's up with the return true at the bottom. – user2363161 Nov 11 '13 at 09:12
  • So why not write `return Arrays.equals(terms, other.terms);` instead of confusing the reader? – Holger Nov 11 '13 at 09:21
  • 1
    Thank you for your efforts. I really appreciate it but it didn't work for me. Luckily I came across my own solution. I wrote an equals method in my Term class and then used it in the equals method in my Poly class. In the Poly equals method, I first check that the length of the two arrays are equal. Then I loop through both arrays with two Term variables to hold and compare the values of each array. I initialized a boolean at the very beginning of the method, adjust it accordingly and then return it. I'll post code later. – user2363161 Nov 11 '13 at 09:59
1

It seems you need the following 2 changes:

  1. Do not compare references using code as follows:

    if(this == x){
        q=true;
    }
    

    You need to compare the content of the object - the contents of terms in your case.

  2. When overriding the equals method, you'd better override the hashcode method as well.

user85421
  • 28,957
  • 10
  • 64
  • 87
Mengjun
  • 3,159
  • 1
  • 15
  • 21
0

My solution involved creating an equals method in the term class first. You would then use that equals method to write the equals method in the polynomial class. So here's the code for the equals method for terms:

public boolean equals(Term x){
    boolean a= false;
    int expThis = this.getExp();
    int coefThis = this.getCoeff();
    int expX = x.getExp();
    int coefX = x.getCoeff();
    if(expThis==expX && coefThis==coefX){
        a=true;
    }
    return a;
}

My polynomial constructor already organizes all terms in decreasing order. If you have polynomials in order then all you have to do is first check that the two polynomials are the same size and then loop through all the terms of the two polynomials, using the equals method from the term class to compare terms. So here's the code for the equals method for polynomials:

public boolean equals(Object obj) {
    boolean w=false;
    Poly other = (Poly) obj;
    int L1 = other.terms.length;
    int L2 = this.terms.length;
    if(L1==L2){
        for(int q=0; q<L1; q++){
            Term a=other.terms[q];
            Term b=this.terms[q];
            if(a.equals(b)==true){
                w=true;
            }
            else{
                w=false;
                break;
            }
        }
    }
    return w;
}