面积对比与面积计算

Comparable

//package zuoye3;
//
图形接口
//
//public abstract class Graph implements Comparable{
//
//	public abstract double area();
//
//	public abstract double girth();
//
//	@Override
//
//	public int compareTo(Graph graph) {
//
//		if(this.area()>graph.area()){
//
//			return 1;
//
//		}else if (this.area()<graph.area()) {
//
//			return -1;
//
//		}else {
//
//			return 0;	
//
//		}
//
//	}
//
//	
//
//}

Circle

package zuoye3;


public class Circle extends Graph{

	private double raidus;

	

	public Circle(double d) {

		// TODO Auto-generated constructor stub

		this.raidus=d;

	}

 

	public double getRaidus() {

		return raidus;

	}

 

	public void setRaidus(double raidus) {

		this.raidus = raidus;

	}

 

	@Override

	public double area() {

		// TODO Auto-generated method stub

		return Math.PI*raidus*raidus;

	}

 

	@Override

	public double girth() {

		// TODO Auto-generated method stub

		return Math.PI*raidus*2;

	}

 

}

Graph

package zuoye3;

public abstract class Graph {

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

Retangle

package zuoye3;


public class Rectangle extends Graph{

	private double length;

	private double width;

	public Rectangle(double d, double e) {

		this.length=d;

		this.width=e;

	}

 

	public double getLength() {

		return length;

	}

 

	public void setLength(double length) {

		this.length = length;

	}

 

	public double getWidth() {

		return width;

	}

 

	public void setWidth(double width) {

		this.width = width;

	}

 

	

	

	@Override

	//计算面积

	public double area() {

		// TODO Auto-generated method stub

		return length*width;

	}

 

	@Override

	public double girth() {

		// TODO Auto-generated method stub

		return 2*(length+width);

	}

 

}

Square

package zuoye3;


public class Square extends Graph{

	private double length;

	

	public Square(double d) {

		this.length=d;

	}

 

	public double getLength() {

		return length;

	}

 

	public void setLength(double length) {

		this.length = length;

	}

 

	@Override

	public double area() {

		// TODO Auto-generated method stub

		return length*length;

	}

 

	@Override

	public double girth() {

		// TODO Auto-generated method stub

		return length*4;

	}

 

}

TestGraph

package zuoye3;

public class TestGraph {
	public static void main(String[] args){
		Graph circle=new Circle(3.0);
		Graph square=new Square(4.0);
		Graph rectangle=new Rectangle(3.0,4.0);
		System.out.println("圆的面积为:"+circle.area());
		System.out.println("圆的周长为:"+circle.girth());
		System.out.println("长方形的面积为:");
		System.out.println("长方形的周长为:");
		System.out.println("正方形的面积为:");
		System.out.println("正方形的周长为:");
	}

}

TestGraphs

package zuoye3;


public class TestGraphs {

	public static void main(String[] args) {

		Graph[] gra=new Graph[10];

		for (int i = 0; i < 3; i++){

			Circle circle=new Circle(2.0+i);

			gra[i]=circle;

		}

		for (int i = 3; i < 7; i++) {

			Square square=new Square(3.0+i);

			gra[i]=square;

		}

		for (int i = 7; i < 10; i++) {

			Rectangle rectangle=new Rectangle(3.0+i, 4.0+i);

			gra[i]=rectangle;

		}

		Graph max=gra[0];

		for (int i=0;i<gra.length;i++){

			if (gra[i].area()>max.area()) {

				max=gra[i];

			}

		}

		System.out.println(max.area());

	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值