0

I am trying to negate a polynomial expression so that the following tests are correct with my polynomial expressions being defined as Term(coefficient, exponent). So my public Term negate() throws Overflow method passes these tests.

Term(min,2) -> expected = Overflow
Term(-7,2) -> expected = (7,2)
Term(0,2) -> expected = (0,2)
Term(7,2) -> expected = (-7,2)
Term(max,2) -> expected = (-max,2)

EDIT: I have the following method within Term:

public Term negate() throws Overflow {

}

and the following in the Term constructor:

public Term(int c, int e) throws NegativeExponent{
    if (e < 0) throw new NegativeExponent();
    coef = c;
    expo = (c == 0 && e != 0) ? 0 : e;
}

The tests above are in a seperate JUnit file, but I am trying to make the negate() method pass the tests.

germainelol
  • 3,231
  • 15
  • 46
  • 82

1 Answers1

3

I can only answer this because I answered one of your previous questions... so you might want to clarify a little more in your post.

Perhaps you want

public Term negate() throws Overflow, NegativeExponent {
    if (coef == min)
        throw new Overflow();
    return new Term(-coef, expo);
}

You might want to consider renaming Overflow to something more specific as well (so as to fully distinguish it from a StackOverflowError).

Community
  • 1
  • 1
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • 1
    +1 for constructive criticism. I remember this `Term` from yesterday. – Miserable Variable Nov 17 '12 at 00:08
  • Sorry for the poorly worded question. I wrote something similar to this code too, but eclipse told me I should also throw `NegativeExponent`, I didn't think I would need to as the tests only every want me to throw `Overflow`. – germainelol Nov 17 '12 at 00:21
  • @user1828314 It's impossible for me to know what the problem is without seeing more of the code. – arshajii Nov 17 '12 at 00:22
  • 1
    @user1828314 See my edit. You need to take `NegativeExponent` into account because you throw it in the constructor of `Term`. – arshajii Nov 17 '12 at 00:29