While i am solving the questions from the book "cracking the coding interview"... i got a doubt.the question is:
Given two squares on a two dimensional plane, find a line that would cut these two squares in half.
Solution:Any line that goes through the center of a rectangle must cut it in half. Therefore, if you drew a line connecting the centers of the two squares, it would cut both in half.
public class Square {
public double left;
public double top;
public double bottom;
public double right;
public Square(double left, double top, double size) {
this.left = left;
this.top = top;
this.bottom = top + size;
this.right = left + size;
}
public Point middle() {
return new Point((this.left + this.right) / 2,
(this.top + this.bottom) / 2);
}
public Line cut(Square other) {
Point middle_s = this.middle();
Point middle_t = other.middle();
if (middle_s == middle_t) {
return new Line(new Point(left, top),
new Point(right, bottom));
} else {
return new Line(middle_s, middle_t);
}
}
}
But now the doubt is '==' operator in cut method to check whether they are points of same square. Is Point immutable?? kindly help me... Thanks in advance.