简体   繁体   中英

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. 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.

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

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).

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 "".
  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.
  4. Not sure how your MyDouble class works, but what does its tosString return for values like 23.000000001? 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"

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

Not to mention you have a bug when {a=0, b=0, c=0} , you return "" instead of 0

Write some JUnit tests - take them from the test team if you are not sure what to do. You can create a few dozens of different scenarios here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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