Java doesnt understand how to compare point s by

Info icon This preview shows pages 22–31. Sign up to view the full content.

View Full Document Right Arrow Icon
Java doesn't understand how to compare  Point s by default.
Image of page 22

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
23 Flawed  equals  method We can change this behavior by writing an  equals  method. Ours will  override  the default behavior from class  Object . The method should compare the state of the two objects and  return  true  if they have the same x/y position. A flawed implementation: public boolean equals(Point other) { if (x == other.x && y == other.y) { return true; } else { return false; } }
Image of page 23
24 Flaws in our method The body can be shortened to the following: // boolean zen return x == other.x && y == other.y; It should be legal to compare a  Point  to any object (not just other  Point s): // this should be allowed Point p = new Point(7, 2); if ( p.equals("hello") ) { // false ... equals  should always return  false  if a non- Point  is passed.
Image of page 24

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
25 equals  and  Object public boolean equals( Object name ) { statement(s) that return a boolean value   ; } The parameter to  equals  must be of type  Object . Object  is a general type that can match any object. Having an  Object  parameter means  any  object can be passed. If we don't know what type it is, how can we compare it?
Image of page 25
26 Another flawed version Another flawed  equals  implementation: public boolean equals(Object o) { return x == o.x && y == o.y; } It does not compile: Point.java:36: cannot find symbol symbol : variable x location: class java.lang.Object return x == o.x && y == o.y ; ^ The compiler is saying, " o  could be any object. Not every object has an  x  field."
Image of page 26

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
27 Type-casting objects Solution:  Type-cast   the object parameter to a  Point . public boolean equals(Object o) { Point other = (Point) o; return x == other.x && y == other.y; } Casting objects is different than casting primitives. Really casting an  Object  reference into a  Point  reference. Doesn't actually change the object that was passed. Tells the compiler to  assume  that  o  refers to a  Point  object.
Image of page 27
28 Casting objects diagram Client code: Point p1 = new Point(5, 3); Point p2 = new Point(5, 3); if ( p1.equals(p2) ) { System.out.println("equal"); } public boolean equals(Object o) { Point other = (Point) o; return x == other.x && y == other.y; } x 5 y 3 p1 p2 ... x 5 y 3 o other
Image of page 28

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
29 Comparing different types Point p = new Point(7, 2); if ( p.equals("hello") ) { // should be false ... } Currently our method crashes on the above code: Exception in thread "main" java.lang.ClassCastException: java.lang.String at Point.equals(Point.java:25) at PointMain.main(PointMain.java:25) The culprit is the line with the type-cast: public boolean equals(Object o) {
Image of page 29
30 The  instanceof  keyword if ( variable instanceof type ) { statement(s) ; }
Image of page 30

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 31
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern