๐ WAYPOINT LESSON
Checkpoint: The Point Class
โญ beginnerโณ 45 min read๐ Lesson 34 of 180
An immutable coordinate type: final fields, factory method, translating without mutation, and geometric equality.
The Point class โ a coordinate with rules.
Inside Solution, write static class Point with:
private final int x; private final int y;โ an immutable point.- A constructor taking
(int x, int y). - Getters
getX(),getY(). public Point translate(int dx, int dy)โ returns a NEW Point moved by the delta (the original must be untouched โ immutability!).public double distanceTo(Point other)โ Euclidean distance (Math.hypot(dx, dy)is fine).static Point origin()โ a factory returningnew Point(0, 0).public boolean equalsPoint(Point other)โ same x AND same y. (The realequals/hashCodecontract arrives in Module 9; this method-side version keeps the focus on fields.)
Immutability is the heart of this checkpoint: after
Point p2 = p1.translate(5, 0);, p1 must still have its original
coordinates, and p2 must be a different object with the new ones.