Java和junit:多项式方法测试问题的导数
我正在尝试完成我的 junit 测试,以查找多项式方法的导数,但在使其工作时遇到了一些麻烦。这是方法:
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;
}
这是 junit 测试:
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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
前两个答案暗示的是,如果 Polynomial 类没有实现 equals(),那么您将在测试中使用 Object.equals() 。当您想要验证 p1.derivative() 和 p2 具有相同的值时,Object.equals() 正在检查 p1.derivative() 和 p2 是否是同一个对象(它们显然不是)......
通常的解决方案将实现 Polynomial.equals(Polynomial rhs),这将确保每一侧的三个 MyDoubles 都是 equals()。当然,您还必须确保 MyDoubles.equals(MyDouble rhs) 做正确的事情。
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.
equals
方法是否正确实现?Is the
equals
method implemented correctly?您的
Polynomial
类是否实现了equals
?否则将进行对象级别比较。这意味着两个对象的指针必须匹配才能相等。您必须实现
equals
来显示Polynomial(a, b, c)
==Polynomial(d, e, f)
的值。我不知道多项式的数据结构是什么,但你会这样做:
Does your
Polynomial
class implementequals
?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 ofPolynomial(a, b, c)
==Polynomial(d, e, f)
.I don't know what the data structure of Polynomial is, but you would do something like: