画图计算面积周长,宽高

在画图软件中,可以画出不同大小或颜色的圆形、矩形等几何图形。几何图形之间有许多共同的特征,如它们可以是用某种颜色画出来的,可以是填充的或者不填充的。此外还有些不同的特征,比如,圆形都有半径,可以根据半径计算圆形的面积和周长矩形都有宽和高,可以根据宽高来计算矩形的面积和周长。

public abstract class Graph implements Comparable {// 定义一个抽象类
	public abstract double area();

	public abstract double girth();// 创建两个方法

	public int compareTo(Graph graph) {
		int i = 0;
		if (this.area() > graph.area()) {
			return 1;
		} else if (this.area() < graph.area()) {
			return -1;
		} else {
			return i;

		}
	}

}
public abstract class Oblong extends Graph{
	
	private double length;
	public double getLength() {
		return length;
	}
	public void setLength(double length) {
		this.length = length;
	}

	private double width;

	public double getWidth() {
		return width;
	}
	public void setWidth(double width) {
		this.width = width;
	}
	
	public Oblong(double length, double width) {
		super();
		this.length = length;
		this.width = width;
	}
	public double getlength() {
		return length;
	}
	public void setlength(double length) {
		this.length = length;
		
	}
	

	public double area() {
	
		return length*width;
	}

	@Override
	public String toString() {
		return "Square []";
	}

	@Override
	public double girth() {
		// TODO Auto-generated method stub
		return length*2+width*2;
	}


}

计算面积

public abstract class Round extends  Graph{
	private double raidus;
	//构造方法,初始化参数
	public Round(double raidus) {
		super();
		this.raidus = raidus;
	}
	public double getRaidus() {
		return raidus;
	}
	public void setRaidus(double raidus) {
		this.raidus = raidus;
	}
	/*
	 * 计算圆的面积
	 */
	@Override
	public double area() {
		
		return 3.14*raidus*raidus;
	}
	/*
	 * 计算圆的周长
	 */
	@Override
	public double girth() {
		
		return 2*3.14*raidus;
	}
  


}

计算周长

public abstract class Square extends Graph{
	
	private double length;
	public double getLength() {
		return length;
	}
	
    public Square(double length) {
		super();
		this.length = length;
	}
	public double getlength() {
		return length;
	}
	public void setlength(double length) {
		this.length = length;
		
	}
	

	@Override
	public double girth() {
		
		return length*4;
	}

	@Override
	public double area() {
		
		return length*length;
	}

}
public class TestGraph {

	public static void main(String[] args) {
		//使用多态的形式实例化图形对象
		Graph r=new Round(3.0);
		System.out.println(r.area());
		System.out.println(r.girth());
		//圆
		
		
		
		Graph d=new Oblong(4.0,5.0);
		System.out.println(d.area());
		System.out.println(d.girth());
		//长方形
		
		Graph s=new Square(5.0);
		System.out.println(s.area());
		System.out.println(s.girth());
		//正方形
	}

}

接口中设计compareTo()方法,用来比较对象

@Override
public double compareTo(Graphic g) {
	int i=0;
	if(this.GetArea()>g.GetArea()){
		return i+1;
	}
	else if(this.GetArea()==g.GetArea()){
		return i;
	}
	else 
		return i-1;
	
	
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值