The Java Program: Point2D.java

  1 // Point2D.java -- immutable two-dimensional double point
  2 
  3 class Point2D {
  4 
  5    // "instance variables" or "member fields"
  6    final double x, y;
  7 
  8    // constructors
  9    Point2D ()                   { this (0.0, 0.0); }
 10    Point2D (double x, double y) { this.x=x; this.y=y; }
 11 
 12    // methods
 13    double distance (Point2D p) {
 14       return Math.hypot (p.x-x, p.y-y);
 15    }
 16 
 17    public String toString () {
 18       return String.format ("(%d,%d)", x, y);
 19    }
 20 }