3

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.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
hareendra reddy
  • 1,542
  • 2
  • 14
  • 17

3 Answers3

9

It doesn't matter if Point is immutable. What matters is that the == is doing a pointer/reference comparison. It is checking to see if middle_s and middle_t reference the same Object instance, not whether or not they are the same in terms of their internal fields.

What you probably want to do is implement/override equals() for the Point class such that it compares the x and y coordinates, and returns true if they match. Then you can compare your points by doing if (middle_s.equals(middle_t)).

When you override equals(), be sure you also override hashCode() accordingly, as well.

aroth
  • 54,026
  • 20
  • 135
  • 176
  • The exact rules are covered quite well in the [Object.equals](http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#equals%28java.lang.Object%29) javadoc. –  Jul 31 '11 at 00:56
2

It doesn't really matter whether Point is immutable... the real question still remains.

What's important is whether you care about the identity of the point objects--whether they are the same instance--or you care about the value of the point objects.

Here, clearly you care about the value of the point, not which object instance it is, so you should be using middle_s.equals(middle_t).

avh4
  • 2,635
  • 1
  • 22
  • 25
  • ya.. this is what i am thinking about...middle() method always returns a new Point object ..irrespective of equals method is overrriden , we can't use == to compare object refernces ..isn't it?? – hareendra reddy Jul 31 '11 at 01:12
  • @hareenda: You can use `==` **only** to compare the references, and the result will always be `false` for new objects (even if they correspond to the same geometric point). – Paŭlo Ebermann Jul 31 '11 at 02:37
0

From JavaDocs, you can use equals method to compare two Point object.
In additional: let take a look at Android developer's page for more explanation:

Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null

Justin
  • 4,400
  • 2
  • 32
  • 36