Equals should always return false if a non point is

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

View Full Document Right Arrow Icon
– 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; } 3 y 5 x p1 p2 ... 3 y 5 x 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) { Point other = (Point) o;
Image of page 29
30 The instanceof keyword if ( variable instanceof type ) { statement(s) ; } Asks if a variable refers to an object of a given type. Used as a boolean test. String s = "hello"; Point p = new Point(); false null instanceof Object false p instanceof String true p instanceof Object false null instanceof String true s instanceof Object true p instanceof Point true s instanceof String false s instanceof Point result expression
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
31 Final equals method // Returns whether o refers to a Point object with // the same (x, y) coordinates as this Point. public boolean equals(Object o) { if (o instanceof Point) { // o is a Point; cast and compare it Point other = (Point) o; return x == other.x && y == other.y; } else { // o is not a Point; cannot be equal return false; } }
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