我正在编写代码并遵循给予我的所有指示。当我运行程序但是相同的方法时,所有的代码和方法看起来都很好!基于这些说明,当测试询问点(a,b)---(c,d)是否等于(e,f)---(g,h)时,我应该得到假,但是我得到了真正。谁能让我知道我做错了什么? 提前谢谢!
/**
* The equals method should return true if the given object is equal to the
* the Object passed in. Note that this method receives an Object; there is
* a particular way that this method should be implemented (see notes from class).
*
* Notice that two Segments are equal even if their endpoints are swapped
* i.e.: (1, 2)---(3, 4) == (3, 4)---(1, 2)
*/
public boolean equals(Object obj) {
//if (obj instanceof Segment) {
//Segment other = (Segment) obj;
//return p1 == other.getP1() && p2 == other.getP2();
//}
//else {
//throw new IllegalArgumentException("undefined");
//}
if(obj == null)
return false;
if(this == obj)
return true;
if(!(obj instanceof Segment))
return false;
else if(obj.getClass() != this.getClass()){
return false;
}
else
{
Segment S = (Segment)obj;
if(this.getP1() == S.getP1() &&
this.getP2() == S.getP2());
return true;
//else if(obj.getP1() != this.getP1() &&
// obj.getP2() != this.getP2());
// return false;
}
}
答案 0 :(得分:5)
if(this.getP1() == S.getP1() &&
this.getP2() == S.getP2());
^
取出这个分号。
如果不满足if
语句,您还需要返回一个值。
修改的
目前,您的if
声明无用。取出分号,以便if
限定以下return语句。然后添加return false
,如果不符合if
语句,将会应用{。}}。
像这样:
if (this.getP1()==S.getP1() && this.getP2()==S.getP2()) {
return true;
}
return false;
或者,更简单地说:
return (this.getP1()==S.getP1() && this.getP2()==S.getP2());