编写一个比较器比较实现了Areable接口的任意图形的面积,Circle,Rectangle等等。编写一个测试类 AreaComparatorTest来测试任意图形面积的比较结果。

public interface Areable {
	double getArea();
}
public class Circle implements Areable {
	protected double r;

	@Override
	public double getArea() {
		return r * r * Math.PI;
	}

	public double getR() {
		return r;
	}

	public void setR(double r) {
		this.r = r;
	}

	public Circle(double r) {
		super();
		this.r = r;
	}

}
public class Rectanle implements Areable {
	protected double x;
	protected double y;

	@Override
	public double getArea() {
		// TODO Auto-generated method stub
		return x * y;
	}

	public Rectanle(double x, double y) {
		super();
		this.x = x;
		this.y = y;
	}

	public double getX() {
		return x;
	}

	public void setX(double x) {
		this.x = x;
	}

	public double getY() {
		return y;
	}

	public void setY(double y) {
		this.y = y;
	}

}
public class AreaComparator {
	public String compare(Areable a1, Areable a2) {
		if (a1.getArea() > a2.getArea()) {
			return "CircleArea > RectangleArea";
		} else if (a1.getArea() < a2.getArea()) {
			return "CircleArea < RectangleArea";
		} else {
			return "CircleArea = RectangleArea";
		}
	}
}
public class AreaCompatorTest {
	public static void main(String[] args) {
		Areable a1 = new Circle(2.5);
		Areable a2 = new Rectanle(2.0, 3.0);

		String result = new AreaComparator().compare(a1, a2);

		System.out.println(result);

	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值