3

I have a Java class called Term holding polynomials like below

public Term(int c, int e) throws NegativeExponent {
    if (e < 0) throw new NegativeExponent();
    coef = c;
    expo = (coef == 0) ? 1 : e;
}

I also have an equals method in the same class like below

@Override
public boolean equals(Object obj) {

}

I am stuck with how to code how to compare these 2 Term objects

Within my JUnit test file I am using the test below to try and test the equals method

import static org.junit.Assert.*;

import org.junit.Test;

public class ConEqTest
{
    private int min = Integer.MIN_VALUE;
    private int max = Integer.MAX_VALUE;



@Test
public void eq01() throws TError { assertTrue(new Term(-10,0).equals(new Term(-10,0))); }

@Test
public void eq02() throws TError { assertTrue(new Term(0,0).equals(new Term(0,2))); }
germainelol
  • 3,231
  • 15
  • 46
  • 82
  • 1
    You might want to consider rnaming `NegativeExponent` to `NegativeExponentException` instead, just as an aside. – arshajii Nov 16 '12 at 00:50
  • make sure you have a hashcode() also. – Ray Tayek Nov 16 '12 at 02:03
  • To add the comment by @RayTayek See [this explanation](http://stackoverflow.com/a/27609/18573) why it is necessary – Miserable Variable Nov 16 '12 at 02:07
  • @MiserableVariable Okay I will look into this. I don't NEED this for the code to function, but I'm assuming it is good practice? I have read the link you posted and don't see why it is necessary that's all. – germainelol Nov 16 '12 at 02:47
  • Primarily because a lot of classes expect it to be so. For example, if you have a `HashMap`, and you use one `Term` object `t1` as key (using `put`) and another `t2` such that `t1.equals(t2)` in `get` then *unless* they both have the same `hashCode` the `HashMap` will not be able to find it. – Miserable Variable Nov 16 '12 at 03:14

2 Answers2

5

What's wrong with

@Override
public boolean equals(Object obj) {
    if (! (obj instanceof Term))
        return false;
    Term t = (Term)obj;
    return coef == t.coef && expo == t.expo; 
}
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • Thanks for the reply A.R.S I have worked out that the way to start it is indeed with `code`if (! (obj instanceof Term)) return false; Term t = (Term)obj; But the last part doesn't work for me, I have no methods called coef() and expo() so I'm not exactly sure how to go about comparing the two Term objects – germainelol Nov 16 '12 at 00:59
  • @user1828314 Are the `coef` and `expo` fields `public`? – arshajii Nov 16 '12 at 01:00
  • I have defined them in the top of the class as: `final private int coef; final private int expo;` Not sure how to go about using this to compare the objects at all – germainelol Nov 16 '12 at 01:04
  • Thanks. My JUnit tests are getting the green light but I have 1 error, the first test works just fine which is:`public void eq01() throws TError { assertTrue(new Term(-10,0).equals(new Term(-10,0))); }` but then this one `public void eq02() throws TError { assertTrue(new Term(0,0).equals(new Term(0,2))); }` is giving me a green light, it should be giving me a failure as (0,0) is not the same as (0,2)...correct? Or am I reading this wrong – germainelol Nov 16 '12 at 01:14
  • Huh? `Term.equals()` should be able access `t.coef` even though it is `private`. You don't need the `public` accessor jist for that. – Miserable Variable Nov 16 '12 at 01:18
  • @MiserableVariable Oh my mistake, not sure what I was thinking. See the edit. – arshajii Nov 16 '12 at 01:21
  • @user1828314 MiserableVariable is correct, you don't need those methods if you *only* plan on accessing the `private` fields within `equals`. – arshajii Nov 16 '12 at 01:22
  • Okay I have that all working, it works to an extent, do you have any idea about the Test cases I put above @A.R.S ? It seems to work for comparing the first one, but should be giving a failure on the second Test case – germainelol Nov 16 '12 at 01:25
  • @user1828314 Seems you've only included one test case in your post. – arshajii Nov 16 '12 at 01:26
  • @A.R.S. Sorry my mistake, just added the second one. – germainelol Nov 16 '12 at 01:27
  • @user1828314 Those two (in the 2nd test case) *should* be equal, because you assign `expo` to `1` *whenever* `coef == 0` (regardless of what is passed as `expo` in the constructor). – arshajii Nov 16 '12 at 01:30
  • @A.R.S. Ah yes, you are correct, I just don't understand the logic behind that code I suppose. It's saying that if `coef == 0` then the expo must be 1, which makes no sense to me. Because of course you can have a polynomial which is x^3 for example. – germainelol Nov 16 '12 at 01:39
  • @user1828314 Well if `coef == 0` then the whole polynomial will always be `0` anyway (`0x^n = 0`). – arshajii Nov 16 '12 at 01:41
  • @A.R.S. Yep, I am reading into it too much, I think I've been staring at the screen too long, I was reading it as if it were 1x. Thanks for your help. – germainelol Nov 16 '12 at 01:42
1
import static org.junit.Assert.*;
import org.junit.*;
@SuppressWarnings("serial") class NegativeExponentException extends Exception {}
class Term {
    @Override public int hashCode() {
        final int prime=31;
        int result=1;
        result=prime*result+coefficient;
        result=prime*result+exponent;
        return result;
    }
    @Override public boolean equals(Object obj) {
        if(this==obj)
            return true;
        if(obj==null)
            return false;
        if(getClass()!=obj.getClass())
            return false;
        Term other=(Term)obj;
        if(coefficient!=other.coefficient)
            return false;
        if(exponent!=other.exponent)
            return false;
        return true;
    }
    public Term(int c,int e) throws NegativeExponentException {
        if(e<0)
            throw new NegativeExponentException();
        coefficient=c;
        exponent=(coefficient==0)?1:e;
    }
    int coefficient,exponent;
}
public class So13408797TestCase {
    @Test public void eq01() throws Exception {
        assertTrue(new Term(-10,0).equals(new Term(-10,0)));
    }
    @Test public void eq02() throws Exception {
        assertTrue(new Term(0,0).equals(new Term(0,2)));
    }
    private int min=Integer.MIN_VALUE;
    private int max=Integer.MAX_VALUE;
}
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90