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 a开发者_如何学运维ppreciated.
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.
Well, the first thing that jumps out at me: for the positive terms, nothing is creating the "+" sign linking them to the rest of the expression.
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.
You're forgetting the + or the -.
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:
- For positive terms, I don't think you are adding a "+" in between the terms.
- If all the terms are zeros, you should return "0", but you are returning "".
- 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.
- Not sure how your
MyDouble
class works, but what does itstosString
return for values like 23.000000001? 23? What does your tests expect? (i.e. 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.
精彩评论