package example;
import java.text.DecimalFormat;
import java.util.Locale;


public class CartesianPoint extends Point {
	private double x;
	private double y;
	
	public CartesianPoint(double x, double y) {
		this.x = x;
		this.y = y;
	}

	@Override
	public double x() {
		return this.x;
	}

	@Override
	public double y() {
		return this.y;
	}

	@Override
	public double r() {
		return Math.hypot(this.x, this.y);
	}

	@Override
	public double theta() {
		return Math.atan2(this.y, this.x);
	}

	@Override
	public String toString() {
		// Use a DecimalFormat to print only two fractional digits.
		DecimalFormat fmt = new DecimalFormat("#.##");
		return "(" + fmt.format(this.x) + "," + fmt.format(this.y) +")";
	}
	
	public static void main(String[] args) {
		// Use ROOT locale so that numbers are displayed like Java literals
		Locale.setDefault(Locale.ROOT);
		DecimalFormat fmt = new DecimalFormat("#.##");
		Point p = new CartesianPoint(0, 1);
		System.out.println(
				"p = " + p.toString() + " = "
		      + fmt.format(p.r()) + ":" + fmt.format(Math.toDegrees(p.theta()))
		);
	}

}
