I am writing a code and am following all instructions given to me. All the codes
ID: 3568978 • Letter: I
Question
I am writing a code and am following all instructions given to me. All the codes and methods look fine when I run the program but the equal method! Based on the instructions, I am supposed to get false when the tested asks if point (a, b)---(c, d) is equals to (e, f)---(g, h), but I get true. Can Anyone give me an idea where I am doing wrong? Thank you in advance!
Here is my code:
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(this.getP1() != S.getP1() &&
this.getP2() != S.getP2())
return true;
return false;
}
//else if(obj.getP1() != this.getP1() &&
// obj.getP2() != this.getP2());
// return false;
}
Explanation / Answer
try this one but if doesnot work can you pls attach all the files so i can test it !! thank you and pls rate if it helps :)
public boolean equals(Object obj) {
if(obj == null)
return false;
else {
Segment S = (Segment)obj;
if (this.getP1() == S.getP1() &&
this.getP2() == S.getP2())
return true;
else
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.