2012-11-17 33 views
0

我创建一个方法乘2次多项式在一起,这样:Java的Junit的:乘2次多项式

3x^5 * 2x^3 = 6x^8 - >其中系数相乘,指数相加。

我的测试案例,这将看起来像下面

@Test 
public void times01() throws TError { 
    assertEquals(Term.Zero, Term.Zero.times(Term.Zero)); 
} 

我还要补充一点,Term.Zero = (0,0)Term.Unit = (1,0),所以,只要乘以Term.ZeroTerm.Zero,任何事情乘以Term.Unit回报本身Term.Unit实际上是1。

public Term times(Term that) throws CoefficientOverflow, ExponentOverflow, NegativeExponent { 
    return null; 
} 

这是times方法。我正在寻求一些编码times方法的帮助?我发现的问题是如何处理3个术语对象Term1,Term2Term3而不是使用无穷无尽的if-statements

+0

任何想法家伙? – germainelol

回答

0

我设计到目前为止以下pseduo代码:

Term1 == Term.Zero OR Term2 == Term.Zero => Term3 = Term.Zero 
Term1 == Term.Unit => Term3 = Term2 
Term2 == Term.Unit => Term3 = Term1 

Term1.coef * Term2.coef = Term3.coef 
Term1.expo * Term2.expo = Term3.expo 

用下面的代码

@SuppressWarnings("null") 
public Term times(Term that) throws CoefficientOverflow, ExponentOverflow, NegativeExponent { 
    Term term1 = new Term(coef,expo); 
    Term term2 = that; 
    Term term3 = null; 

    if(term1 == Zero || term2 == Zero) { 
     term3 = Zero; 
    } else if(term1 == Unit) { 
     term3 = term2; 
    } else if(term2 == Unit) { 
     term3 = term1; 
    } else if(term1.coef == 2 && term1.expo == 0) { 
     term3.coef = term2.coef * 2; 
     term3.expo = term2.expo; 
    } else if(term2.coef == 2 && term2.expo == 0) { 
     term3.coef = term1.coef * 2; 
    term3.expo = term1.expo; 
    } else { 
     term3.coef = term1.coef * term2.coef; 
     term3.expo = term1.expo + term2.expo; 
    } 



    return term3; 
} 

但是,这让我不得不改变在期限类我“COEF /博览会”变量从

final private int coef; 

private int coef; 

这给出了下面的测试错误...

@Test 
public void times09() throws TError { assertEquals(new Term(min,2), new Term(hmin,2).times(new Term(2,0))); } 

任何想法?