/** demonstrate class structure, including public, protected, and 
    private instance variables, class (static) variable, use of 
    public "access" methods. */
public class TwoDPoint {
  private int x;  // accessible only within this class
  private int y;
  protected static int countPoints = 0;  /* accessible within this 
              and any subclasses, static means a class variable */

  /* several overloaded constructors, i.e. same name with 
     different "signatures" */
  public TwoDPoint() {
    x = y = 0;
    ++countPoints;
  }
  public TwoDPoint(int r, int s) {
    x = r; y = s;
    ++countPoints;
  }
  public TwoDPoint(TwoDPoint p) {
    // call own constructor, "this" refers to own object
    this(p.getX(), p.getY());  
  }

  // provide some publicly accessible methods
  public int getX() {
    return x;
  }
  public int getY() {
    return y;
  }
  public void setX(int r) {
    x = r;
  }
  public void setY(int s) {
    y = s;
  }

  // override the existing toString() method
  public String toString() {
    return new String("x coordinate = " + x + ", y coordinate = " + y);
  }
  public double distance(TwoDPoint p) {
    /* call the Math class's method sqrt() prefixing with the 
       class name Math */
    return Math.sqrt((this.getX() - p.getX())*
                    (this.getX() - p.getX()) + (this.getY() - p.getY())*
                    (this.getY() - p.getY()));
  }
}

