Java 加法多项式
我需要一些帮助。我正在做测试驱动开发。以下是测试:Java 加法多项式,java,unit-testing,junit,tdd,polynomial-math,Java,Unit Testing,Junit,Tdd,Polynomial Math,我需要一些帮助。我正在做测试驱动开发。以下是测试: @Test public void add01() throws TError { assertEquals(new Term(10,5), new Term(4,5).add(new Term(6,5))); } @Test public void add02() throws TError { assertEquals(new Term(6,5), new Term(0,5).add(new Term(6,5))); } @Test
@Test public void add01() throws TError { assertEquals(new Term(10,5), new Term(4,5).add(new Term(6,5))); }
@Test public void add02() throws TError { assertEquals(new Term(6,5), new Term(0,5).add(new Term(6,5))); }
@Test public void add03() throws TError { assertEquals(new Term(2,5), new Term(-4,5).add(new Term(6,5))); }
@Test public void add04() throws TError { assertEquals(new Term(10,0), new Term(4,0).add(new Term(6,0))); }
@Test public void add05() throws TError { assertEquals(new Term(4,5), new Term(4,5).add(new Term(0,5))); }
@Test public void add06() throws TError { assertEquals(new Term(-2,5), new Term(4,5).add(new Term(-6,5))); }
@Test (expected = IncompatibleTerms.class)
public void add07() throws TError { t = new Term(4,5).add(new Term(6,0)); }
@Test (expected = CoefficientOverflow.class)
public void add08() throws TError { t = new Term(min,4).add(new Term(-1,4)); }
@Test public void add09() throws TError { assertEquals(new Term(min,4), new Term(min+1,4).add(new Term(-1,4))); }
@Test public void add10() throws TError { assertEquals(new Term(-11,4), new Term(-10,4).add(new Term(-1,4))); }
@Test public void add11() throws TError { assertEquals(new Term(-2,4), new Term(-1,4).add(new Term(-1,4))); }
@Test public void add12() throws TError { assertEquals(new Term(-1,4), new Term(0,4).add(new Term(-1,4))); }
@Test public void add13() throws TError { assertEquals(Term.Zero, new Term(1,4).add(new Term(-1,4))); }
@Test public void add14() throws TError { assertEquals(new Term(9,4), new Term(10,4).add(new Term(-1,4))); }
@Test public void add15() throws TError { assertEquals(new Term(max,4), new Term(max-1,4).add(new Term(1,4))); }
@Test (expected = CoefficientOverflow.class)
public void add16() throws TError { t = new Term(max,4).add(new Term(1,4)); }
//Using domain-specific knowledge: addition should be commutative
@Test public void add17() throws TError { assertEquals(new Term(-1,2).add(new Term(1,2)), new Term(1,2).add(new Term(-1,2))); }
@Test public void add18() throws TError { assertEquals(new Term(min+1,2).add(new Term(-1,2)), new Term(-1,2).add(new Term(min+1,2))); }
@Test public void add19() throws TError { assertEquals(new Term(min,2).add(Term.Zero), Term.Zero.add(new Term(min,2))); }
@Test public void add20() throws TError { assertEquals(new Term(min,0).add(Term.Unit), Term.Unit.add(new Term(min,0))); }
@Test public void add21() throws TError { assertEquals(new Term(max,0).add(Term.Zero), Term.Zero.add(new Term(max,0))); }
@Test public void add22() throws TError { assertEquals(new Term(max-1,2).add(new Term(1,2)), new Term(1,2).add(new Term(max-1,2))); }
@Test public void add23() throws TError { assertEquals(new Term(max,2).add(new Term(min,2)), new Term(min,2).add(new Term(max,2))); }
这是我到目前为止在“Andrej Gajduk”的帮助下添加的方法
这些测试失败=2,5,8,12,16,19,21。但我对第17-23次考试有疑问;我知道他们中的一些人通过了,但我认为他们不应该通过,因为这就像x+y=y+x,但我不知道如何实现它
我需要一些帮助/指导来解释为什么测试失败
对于测试8和16,我尝试了下面的代码。它完成了任务,但在其他测试中会产生更多错误。所以所有的失败加起来变成2,5,8,12,16,19,21+20,23(两个额外的失败)
此链接显示检测溢出的另一种方法: 我认为这是因为当coef为0时,无论是这个还是整个语句都假设为零,所以你需要检查这个,并使用零的替代方案
if(this.coef ==0)
{//return Term t = new Term(that.coef, that.expo);}
else
{//return Term t = new Term(this.coef, this.expo);}
}
我希望这有帮助您的
equals
方法不正确。请更新如下:
@Override
public boolean equals(Object that) {
if (that == null) {
return false;
}else if (this.getClass() != that.getClass()) {
return false;
}else if (this == that) {
return true;
}else{
Term term = (Term) that;
if ((term.coef) == (this.coef) && (term.expo) == (this.expo)) {
return true;
}
}
return false;
}
public Term add(Term that) throws IncompatibleTerms, CoefficientOverflow{
//handle addition with term zero
if(this.equals(Term.Zero)){
return that;
}else if(Term.Zero.equals(that)){
return this;
}
if (this.expo != that.expo) throw new IncompatibleTerms();
if((this.coef>= 0 && that.coef >= 0 && this.coef + that.coef <0) ||
(this.coef<= 0 && that.coef <= 0 && this.coef + that.coef >0)){
throw new CoefficientOverflow();
}
return new Term (this.coef + that.coef,expo);
}
要使大多数测试用例通过,请更新add
方法,如下所示:
@Override
public boolean equals(Object that) {
if (that == null) {
return false;
}else if (this.getClass() != that.getClass()) {
return false;
}else if (this == that) {
return true;
}else{
Term term = (Term) that;
if ((term.coef) == (this.coef) && (term.expo) == (this.expo)) {
return true;
}
}
return false;
}
public Term add(Term that) throws IncompatibleTerms, CoefficientOverflow{
//handle addition with term zero
if(this.equals(Term.Zero)){
return that;
}else if(Term.Zero.equals(that)){
return this;
}
if (this.expo != that.expo) throw new IncompatibleTerms();
if((this.coef>= 0 && that.coef >= 0 && this.coef + that.coef <0) ||
(this.coef<= 0 && that.coef <= 0 && this.coef + that.coef >0)){
throw new CoefficientOverflow();
}
return new Term (this.coef + that.coef,expo);
}
public Term add(抛出不兼容rms、系数overflow的术语){
//处理零项加法
如果(此项等于(项零)){
归还;
}else如果(项0等于(that)){
归还这个;
}
如果(this.expo!=that.expo)抛出新的不兼容rms();
如果((this.coef>=0&&that.coef>=0&&this.coef+that.coef),您可以发布您的术语.equals()
method?这不是测试驱动开发。这不是测试驱动开发吗?@Lax:我在答案中做了一些更新。如果这没有帮助,请检查并让我知道。感谢您的帮助,它确实通过了大多数测试,除了7、8和16。我试图找出y 7失败,因为不同指数的条件是correct@Lax:对于me、 所有的测试用例都通过了。你有什么特殊的构造函数方法吗?公共项(int c,int e)抛出NegativeExponent{if(e<0)抛出新的NegativeExponent();coef=c;expo=(coef==0)?1:e;}@Lax:我使用了相同的方法,所有测试用例都通过了。只是想检查一下,您是否使用了答案中更新的equals
方法。有一个问题,我在某个时候更新了它。