Java和junit:多项式方法测试问题的导数

发布于 2024-08-26 19:12:29 字数 703 浏览 10 评论 0原文

我正在尝试完成我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深巷少女 2024-09-02 19:12:30

前两个答案暗示的是,如果 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.

往日情怀 2024-09-02 19:12:30

equals 方法是否正确实现?

Is the equals method implemented correctly?

聊慰 2024-09-02 19:12:30

您的Polynomial类是否实现了equals

否则将进行对象级别比较。这意味着两个对象的指针必须匹配才能相等。您必须实现 equals 来显示 Polynomial(a, b, c) == Polynomial(d, e, f) 的值。

我不知道多项式的数据结构是什么,但你会这样做:

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文