Skip to main content

Checkpoint: The Point Class

Challenge on lesson: Checkpoint: The Point Class

The Point class — a coordinate with rules. Inside Solution, write static class Point with: 1. private final int x; private final int y; — an **immutable** point. 2. A constructor taking (int x, int y). 3. Getters getX(), getY(). 4. public Point translate(int dx, int dy) — returns a NEW Point moved by the delta (the original must be untouched — immutability!). 5. public double distanceTo(Point other) — Euclidean distance (Math.hypot(dx, dy) is fine). 6. static Point origin() — a factory returning new Point(0, 0). 7. public boolean equalsPoint(Point other) — same x AND same y. (The real equals/hashCode contract 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.

Difficulty: beginner

Back to lesson: Checkpoint: The Point Class