views:

102

answers:

3

Hello all, im trying to finish up my junit testing for finding the derivative of a polynomial method and im having some trouble making it work. here is the method:

    public Polynomial derivative() {
  MyDouble a = new MyDouble(0);
  MyDouble b = this.a.add(this.a);
  MyDouble c = this.b;
  Polynomial poly = new Polynomial (a, b, c);
  return poly;
 } 

and here is the junit test:

    public void testDerivative() {
  MyDouble a = new MyDouble(2), b = new MyDouble(4), c = new MyDouble(8);
  MyDouble d = new MyDouble(0), e = new MyDouble(4), f = new MyDouble(4);

  Polynomial p1 = new Polynomial(a, b, c);
  Polynomial p2 = new Polynomial(d,e,f);
  assertTrue(p1.derivative().equals(p2));
 }

im not too sure why it isnt working...ive gone over it again and again and i know im missing something. thank you all for any help given, appreciate it

A: 

Is the equals method implemented correctly?

lexicore
A: 

Does your Polynomial class implement equals?

Otherwise it is going to an Object level comparison. Meaning that the pointers of the two objects must match for it to be equal. You have to implement equals to show that the values of Polynomial(a, b, c) == Polynomial(d, e, f).

I don't know what the data structure of Polynomial is, but you would do something like:

public boolean equals(Polynomial p) 
{
    // where a b and c are private MyDouble variables 
    if (p.a == this.a && p.b == this.b && p.c == this.c) 
        return true;
    else 
        return false;
}
Bryan Denny
+2  A: 

What the previous two answers are hinting at is that, if the Polynomial class doesn't implement equals(), then you are using Object.equals() in the test. Object.equals() is checking that p1.derivative() and p2 are the same object (which they clearly are not) when you want to verify that p1.derivative() and p2 have the same value....

The usual solution would be to implement Polynomial.equals(Polynomial rhs), which would make sure that the three MyDoubles on each side are equals(). Of course, you'll also have to ensure that MyDoubles.equals(MyDouble rhs) does the Right Thing.

VoiceOfUnreason
For example, the _JScience_ class `Polynomial` has the correct implementation. http://jscience.org/api/org/jscience/mathematics/function/Polynomial.html
trashgod