/** We represent as a class points in the cartesian plane.
*/

public class Point
{
    // instance data members
    private double _x;
    private double _y;

    // accessors
    public double getX() { return _x;}
    public double getY() { return _y;}

    // setters
    public void setX(double x) { _x = x; }
    public void setY(double y) { _y = y; }

    // constructors
    public Point() {} // default constructor
    public Point(double x, double y) {
	_x = x;
	_y = y;
    }

    /** @param a the point being compared to this
	@return true iff a and this have same content
    */
    public boolean equals(Point a) {
	return _x == a._x && _y == a._y;
    }

    /** @return a string of the form (_x, _y) 
    */
    public String toString() {
	return "(" + _x + ", " + _y + ")";
    }

    /** @param the Point a we want to add, coordinate wise, to this
	@return a point with coordinates that are the sum of the corresponding
		coordinate of this and a
    */
    public Point add(Point a) {
	return new Point(_x + a._x, _y + a._y);
    }

    /** 
	@return the distance from this to (0.0, 0.0)
    */
    public double distance() {
	return Math.sqrt( _x * _x + _y * _y );
    }

    /** @param p a Point
	@return the distance from p to (0.0, 0.0)
    */
    public double distance(Point p) {
	double dx = _x - p._x;
	double dy = _y - p._y;
	return Math.sqrt( dx * dx + dy * dy );
    }

    public static void main(String[] args) {
	Point a = new Point();
	System.out.println("a = " + a); // same as "a = " + a.toString()
	Point b = new Point(2.3, 3.1);
	System.out.println("b = " + b);
	Point c = new Point(2.2, 1.1);
	System.out.println("c = " + c);
	Point d = b;
	System.out.println("d = " + d);
	b.setX(1.4);
	System.out.println("d = " + d);
	Point e = new Point(2.1, 1.1);
	System.out.println("b == d is " + (b == d));
	System.out.println( "b.equals(d) is " + (b.equals(d)) );
	System.out.println( "b.add(c) is " + b.add(c));
	System.out.println( "b.distance() = " + b.distance() );
	System.out.println( "b.distance(c) = " + b.distance(c) );
	System.exit(0);
    }
/* The output is
a = (0.0, 0.0)
b = (2.3, 3.1)
c = (2.2, 1.1)
d = (2.3, 3.1)
d = (1.4, 3.1)
b == d is true
b.equals(d) is true
b.add(c) is (3.6, 4.2)
b.distance() = 3.4014702703389896
b.distance(c) = 2.154065922853802
*/
}
