我有一个 Term 类来定义多项式:
public class Term
{
final private int coef;
final private int expo;
private static Term zero, unit;
static
{
try
{
zero = new Term(0, 0); // the number zero
unit = new Term(1, 0); // the number one
}
catch (Exception e)
{
// constructor will not throw an exception here anyway
}
}
/**
*
* @param c
* The coefficient of the new term
* @param e
* The exponent of the new term (must be non-negative)
* @throws NegativeExponent
*/
public Term(int c, int e) throws NegativeExponent
{
if (e < 0)
throw new NegativeExponent();
coef = c;
expo = (coef == 0) ? 1 : e;
}
final public static Term Zero = zero;
final public static Term Unit = unit;
public boolean isConstant()
{
boolean isConst = false;
if (this.expo == 0)
{
isConst = true;
}
return isConst;
}
}
我的 JUnit 测试如下:
/*
* const1 isConstant(zero) => true (0,0)
* const2 isConstant(unit) => true (1,0)
* const3 isConstant(0,5) => true
* const4 isConstant(5,1) => false
*/
@Test
public void const1() throws TError { assertTrue(Term.Zero.isConstant()); }
@Test
public void const2() throws TError { assertTrue(Term.Unit.isConstant()); }
@Test
public void const3() throws TError { assertTrue(new Term(0,5).isConstant()); }
@Test
public void const4() throws TError { assertFalse(new Term(5,1).isConstant()); }
测试 2 和 4 应有的通过,但测试 1 和 3 失败,我不知道为什么,“零”将多项式定义为 (0,0),另一个将其定义为 (0,5) . 所以根据我的想法,第一个测试应该给出一个绿色的勾号,第三个测试应该给出一个红十字,因为它的指数是 5。
有任何想法吗?