Java/JUnit-比较两个多项式对象

Java/JUnit-比较两个多项式对象,java,object,junit,equals,polynomial-math,Java,Object,Junit,Equals,Polynomial Math,我有一个Java类,叫做术语保持多项式,如下所示 public Term(int c, int e) throws NegativeExponent { if (e < 0) throw new NegativeExponent(); coef = c; expo = (coef == 0) ? 1 : e; } @Override public boolean equals(Object obj) { } 我一直在学习如何编写代码来比较这两个术语对象 在我的

我有一个Java类,叫做术语保持多项式,如下所示

public Term(int c, int e) throws NegativeExponent {
    if (e < 0) throw new NegativeExponent();
    coef = c;
    expo = (coef == 0) ? 1 : e;
}
@Override
public boolean equals(Object obj) {

}
我一直在学习如何编写代码来比较这两个术语对象

在我的JUnit测试文件中,我使用下面的测试来尝试和测试equals方法

import static org.junit.Assert.*;

import org.junit.Test;

public class ConEqTest
{
    private int min = Integer.MIN_VALUE;
    private int max = Integer.MAX_VALUE;



@Test
public void eq01() throws TError { assertTrue(new Term(-10,0).equals(new Term(-10,0))); }

@Test
public void eq02() throws TError { assertTrue(new Term(0,0).equals(new Term(0,2))); }
你怎么了

@Override
public boolean equals(Object obj) {
    if (! (obj instanceof Term))
        return false;
    Term t = (Term)obj;
    return coef == t.coef && expo == t.expo; 
}
import static org.junit.Assert.*;
导入org.junit.*;
@SuppressWarnings(“serial”)类NegativeExponentException扩展了异常{}
课堂用语{
@重写公共int hashCode(){
最终整数素数=31;
int结果=1;
结果=素数*结果+系数;
结果=素数*结果+指数;
返回结果;
}
@覆盖公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(getClass()!=obj.getClass())
返回false;
术语其他=(术语)obj;
if(系数!=其他系数)
返回false;
如果(指数!=其他指数)
返回false;
返回true;
}
公共术语(int c,int e)抛出否定的表达式异常{

如果(EYU可能想考虑RAMENG <代码>负指数< /COD> > <代码>负指数异常> /代码>,就像ASID一样。确保您有一个HASCODE()。另外,要添加@RayTayek的评论,请查看原因necessary@MiserableVariable好的,我将对此进行研究。我不需要这个来让代码正常运行,但我假设这是一个良好的实践?我已经阅读了您发布的链接,不明白为什么有必要这么做。主要是因为很多类都希望这样。例如,如果您有
HashMap
,使用一个
术语
对象
t1
作为键(使用
put
),另一个
t2
,使
t1.equals(t2)
get
中,除非它们都具有相同的
hashCode
否则
HashMap
将无法找到它。感谢A.R.S的回复,我已经找到了一种方法,即如果(!(obj instanceof Term))返回false,则开始使用
code
如果(!(obj instanceof Term))返回false;Term t=(Term)但是最后一部分对我不起作用,我没有称为coef()和expo()的方法所以我不确定如何比较这两个术语objects@user1828314
coef
expo
字段
public
?我在类的顶部将它们定义为:
final private int coef;final private int expo;
不知道如何使用它来比较对象谢谢单元测试得到了批准,但我有一个错误,第一个测试运行得很好:
public void eq01()抛出恐怖{assertTrue(新术语(-10,0).equals(新术语(-10,0));}
但是这个
public void eq02()抛出恐怖{assertTrue(新术语(0,0).equals(新术语(0,2));}
给我开了绿灯,它应该给我一个失败,因为(0,0)和(0,2)不一样…正确吗?或者我读错了吗?
术语.equals()
应该能够访问
t.coef
,即使它是
私有的
你不需要
公共的
访问器jist。
import static org.junit.Assert.*;
import org.junit.*;
@SuppressWarnings("serial") class NegativeExponentException extends Exception {}
class Term {
    @Override public int hashCode() {
        final int prime=31;
        int result=1;
        result=prime*result+coefficient;
        result=prime*result+exponent;
        return result;
    }
    @Override public boolean equals(Object obj) {
        if(this==obj)
            return true;
        if(obj==null)
            return false;
        if(getClass()!=obj.getClass())
            return false;
        Term other=(Term)obj;
        if(coefficient!=other.coefficient)
            return false;
        if(exponent!=other.exponent)
            return false;
        return true;
    }
    public Term(int c,int e) throws NegativeExponentException {
        if(e<0)
            throw new NegativeExponentException();
        coefficient=c;
        exponent=(coefficient==0)?1:e;
    }
    int coefficient,exponent;
}
public class So13408797TestCase {
    @Test public void eq01() throws Exception {
        assertTrue(new Term(-10,0).equals(new Term(-10,0)));
    }
    @Test public void eq02() throws Exception {
        assertTrue(new Term(0,0).equals(new Term(0,2)));
    }
    private int min=Integer.MIN_VALUE;
    private int max=Integer.MAX_VALUE;
}