简体   繁体   English

Java:为多项式类创建toString方法

[英]Java: Creating a toString method for Polynomial class

public String toString(){
    String mytoString="";
    if(!a.equals(0)){
        mytoString = a.toString() + "x^2";
    }
    if(!b.equals(0)){
        mytoString += b.toString() + "x";
    }
    if(!c.equals(0)){
        mytoString += c.toString(); 
    }
    return mytoString;
}

This is the code I have. 这是我的代码。 The release tests for the project says I'm failing toStringPositive. 该项目的发布测试表明我无法实现StringPositive。 I'm having trouble figuring out what exactly is wrong with my code. 我在弄清楚代码到底出了什么问题时遇到了麻烦。 Any help is appreciated. 任何帮助表示赞赏。

Well, one obvious problem - your code never adds any "+" signs as far as I can see... 好吧,一个明显的问题-就我所知,您的代码从不添加任何“ +”号...

It sounds like you have to submit this for automated testing - I suggest you create your own unit tests to see what happens in various situations. 听起来您必须提交此文件以进行自动化测试-我建议您创建自己的单元测试,以查看在各种情况下会发生什么。 (You might want to start with each of the examples in the requirements.) (您可能想从需求中的每个示例开始。)

That way you'll be able to see exactly what's wrong with the actual output compared with the expected output, rather than just knowing it's "toStringPositive" which has failed. 这样一来,您将能够准确地看到实际输出与预期输出的出问题,而不仅仅是知道失败的“ toStringPositive”。

好吧,第一件事突然出现在我身上:对于肯定的术语,没有任何东西可以创建将它们链接到表达式其余部分的“ +”号。

I think the best way for you to test this is by feeding a bunch of diverse test inputs into the function and seeing whether what comes out meets the spec. 我认为最好的测试方法是将大量不同的测试输入输入函数,然后查看结果是否符合规范。

One obvious problem is that you never include any plus signs into your string, which in all probability would fail quite a lot of tests. 一个明显的问题是您永远不会在字符串中包含任何加号,这很可能会使很多测试失败。

您忘记了+或-。

Yes, no + or - sign. 是的,没有+-号。 You should test your method for simple, common values of a, b, c, starting by 1, 2, 3 over more sophisticated values like (0, 1, 1), (0, 0, 0) and (1, 0, 0) and (-1, -2, -3) to rocket-science: (0, -1, -0.0). 您应该测试方法的a,b,c的简单,通用值,以1,2,3开头,而不是(0,1,1),(0,0,0)和(1,0, 0)和(-1,-2,-3)转化为火箭科学:(0,-1,-0.0)。

From first glance: 乍看之下:

  1. For positive terms, I don't think you are adding a "+" in between the terms. 对于积极的用语,我认为您不会在这些用语之间添加“ +”。
  2. If all the terms are zeros, you should return "0", but you are returning "". 如果所有项均为零,则应返回“ 0”,但返回“”。
  3. Not really sure if this would trip your tests, but when the coefficient is 1, output should be like "x" or "x^2" instead of "1x" or "1x^2" for a and b. 不确定是否会触发测试,但是当系数为1时,a和b的输出应类似于“ x”或“ x ^ 2”,而不是“ 1x”或“ 1x ^ 2”。
  4. Not sure how your MyDouble class works, but what does its tosString return for values like 23.000000001? 不确定MyDouble类的工作方式,但是对于tosString的值,其tosString返回什么? 23? 23? What does your tests expect? 您的测试期望什么? (ie what level of precision/accuracy does your class handle and what level is expected by the tests?). (例如,您的班级处理的精度/准确度是多少,测试期望的水平是什么?)。

maybe this helps you 也许这对您有帮助

public String toString(){
    String mytoString="";
    if(!a.equals(0)){
        mytoString = a.toString() + "x^2";
    }
    if(!b.equals(0)){
        if (b > 0)
              mytoString += "+";
        mytoString += b.toString() + "x";
    }
    if(!c.equals(0)){
        if (c > 0)
              mytoString += "+";
        mytoString += c.toString(); 
    }
    return mytoString;
}

you should pay attention on adding the sign for positive numbers 您应该注意加正号的符号

One requirement your program will not fullfill: 您的程序无法满足的一项要求:

If all 3 coefficients are 0, then the string should be "0" 如果所有3个系数均为0,则字符串应为“ 0”

and as pointed out above, you're not appending plus or minus to the terms. 并且如上所述,您没有在条款后加或减。

You do not add a sign to any coefficients. 请勿在任何系数上添加符号。

So if for example your input is: {a=1, b=2, c=3} , your output will be: 1x^22x3 , instead of the expected 1x^2+2x+3 因此,例如,如果您的输入是: {a=1, b=2, c=3} ,那么您的输出将是: 1x^22x3 ,而不是预期的1x^2+2x+3

Not to mention you have a bug when {a=0, b=0, c=0} , you return "" instead of 0 更不用说当{a=0, b=0, c=0} ,您返回""而不是0时遇到错误

Write some JUnit tests - take them from the test team if you are not sure what to do. 编写一些JUnit测试-如果不确定该怎么做,请从测试团队那里获取。 You can create a few dozens of different scenarios here. 您可以在此处创建数十种不同的方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM