Reputation: 23
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.
Upvotes: 0
Views: 1992
Reputation: 9652
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.
Upvotes: 0
Reputation: 38541
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.
Upvotes: 0
Reputation: 500663
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.
Upvotes: 1
Reputation: 1683
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
Upvotes: 0
Reputation: 26586
From first glance:
MyDouble
class works, but what does its tosString
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?). Upvotes: 0
Reputation: 36250
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).
Upvotes: 0
Reputation: 81704
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.
Upvotes: 1
Reputation: 1502016
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.
Upvotes: 2