Sathish
Sathish

Reputation: 23

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.

Upvotes: 0

Views: 1992

Answers (9)

RonK
RonK

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

Amir Afghani
Amir Afghani

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

bspoel
bspoel

Reputation: 1577

You're forgetting the + or the -.

Upvotes: 1

NPE
NPE

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

Alina Danila
Alina Danila

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

MAK
MAK

Reputation: 26586

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? (i.e. what level of precision/accuracy does your class handle and what level is expected by the tests?).

Upvotes: 0

user unknown
user unknown

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

Ernest Friedman-Hill
Ernest Friedman-Hill

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

Jon Skeet
Jon Skeet

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

Related Questions