java面积

package area;

import java.util.Scanner;

class test extends Exception {

	public test(String message) {

		super(message);

	}

}

interface Shape {

	void TriangleArea() throws test;

	void CircleArea();

}

class Calculation implements Shape {

	private double radius;

	private double height;

	public Calculation(double radius, double height) {

		this.radius = radius;

		this.height = height;

	}

	@Override

	public void TriangleArea() throws test {

		if (height > radius) {

			throw new test("三角形的高不应该大于圆半径!");

		}

		double base = radius * 2;

		double area = (base * height) / 2;

		System.out.println("三角形面积:: " + area);

	}

	@Override

	public void CircleArea() {

		double area = Math.PI * (radius * radius);

		System.out.println("圆面积::" + area);

	}

}

public class Main {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		System.out.print("输出圆的半径::");

		double radius = scanner.nextDouble();

		System.out.print("输出三角形的高::");

		double height = scanner.nextDouble();

		Shape shape = new Calculation(radius, height);

		try {

			shape.TriangleArea();

			shape.CircleArea();

		} catch (test e) {

			System.out.println("错误: " + e.getMessage());

		}
		scanner.close();

	}

}