Checkpoint: The Point Class
beginner45 min readLesson 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.