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


public class PolarPoint extends Point {
	private double r;
	private double theta;
	
	public PolarPoint(double r, double theta) {
		this.r = r;
		this.theta = theta;
	}

	@Override
	public double x() {
		return this.r * Math.cos(this.theta);
	}

	@Override
	public double y() {
		return this.r * Math.sin(this.theta);
	}

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

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

	@Override
	public String toString() {
		// Use a DecimalFormat to print only two fractional digits.
		DecimalFormat fmt = new DecimalFormat("#.##");
		return "(" + fmt.format(Math.toDegrees(this.theta)) + ":" + fmt.format(this.r) +")";
	}
	
	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 PolarPoint(1, Math.toRadians(45));
		System.out.println(
				"p = (" + fmt.format(p.x()) + "," + fmt.format(p.y()) + ") = "
		       + p.toString()
		);
	}

}
