/* An instance of this class represents an immutable point in two-space ** (i.e., on the cartesian plane). */ public class PointIn2Space { // instance variables // ------------------ private double x, y; // constructor // ----------- /* Establishes this point as having the given x- and y-coordinates. */ public PointIn2Space(double xCoord, double yCoord) { x = xCoord; y = yCoord; } // observers // --------- /* Returns the x-coordinate of this point. */ public double getX() { return x; } /* Returns the y-coordinate of this point. */ public double getY() { return y; } /* Returns the distance between this point and the given one. */ public double distanceTo(PointIn2Space p) { return Math.sqrt(squareOf(x - p.getX()) + squareOf(y - p.getY())); } /* Returns a string describing this point, in the standard notation ** (e.g., "(5.2,3.7)"). */ public String toString() { return "(" + x + "," + y + ")"; } // private (utility) // ----------------- /* Returns the square of the given number. */ private double squareOf(double z) { return z*z; } }