图形类&面积比较

题目:

      抽象类图形类Graph ,它有三个子类Circle(圆),Square(正方形),Squareness(矩形),test_1测试类用于声明类对象,并用子类对父类重写的方法完成各自图形类面积的计算。在以上基础再写一个interface,和一个接口的实现(implements)类,test_2用于找出一组图形对象里面积最大的图形,并输出其索引,打印出该图形面积。共8个类

1.Graph.class

public abstract class Graph {
	
	// 获取面积
	public abstract double getArea();
	// 获取周长
	public abstract double getGirth();
	// 打印图形信息
	public abstract String toString();
	
}

2.Circle.Class

public class Circle extends Graph{
	
	private double radius;
	private double PI;
	private double Area;
	private double Girth;
	
	public Circle(){
		
	}
	
	public double getArea(){
		return Area;
	}

	public double getGirth() {
		return Girth;
	}
	
	public Circle(double radius,double PI){
		this.radius=radius;
		this.PI=PI;
		this.Area=Count_Area(radius,PI);
		this.Girth=Count_Girth(radius,PI);
	}
	
	public double Count_Area(double radius, double PI) {
		return radius*radius*PI;
	}
	
	public double Count_Girth(double radius, double PI) {
		return 2*PI*radius;
	}

	@Override
	public String toString() {
		String str="半径为"+radius+"的圆,以圆周率为"+PI+"计算,得面积="+Area+",周长="+Girth;
		return str;
	}
	
}

 

3.Square.class

public class Square extends Graph{

	private double side_length;
	private double Area;
	private double Girth;
	
	public Square(){
		
	}
	
	public double getArea(){
		return Area;
	}

	public double getGirth() {
		return Girth;
	}
	
	public Square(double side_length){
		this.side_length=side_length;
		this.Area=Count_Area(side_length);
		this.Girth=Count_Girth(side_length);
	}
	
	public double Count_Area(double side_length) {
		return side_length*side_length;
	}
	
	public double Count_Girth(double side_length) {
		return side_length*4;
	}

	@Override
	public String toString() {
		String str="边长为"+side_length+"的正方形的面积="+Area+",周长="+Girth;
		return str;
	}

}

4.Squareness.Class

public class Squareness extends Graph{
	
	private double width;
	private double height;
	private double Area;
	private double Girth;
	
	public Squareness(){
		
	}
	
	public double getArea(){
		return Area;
	}
	
	public double getGirth() {
		return Girth;
	}
	
	public Squareness(double width, double height){
		this.width=width;
		this.height=height;
		this.Area=Count_Area(width,height);
		this.Girth=Count_Girth(width,height);
	}
	
	public double Count_Area(double width, double height) {
		return width*height;
	}
	
	public double Count_Girth(double width, double height) {
		return (width+height)*2;
	}

	@Override
	public String toString() {
		String str="长为"+height+",宽为"+width+"的矩形面积="+Area+",周长="+Girth;
		return str;
	}
}

5.test_1.class

public class test_1 {
	public static double PI=3.14;
	public static void main(String[] args) {
		// 实例化图形对象
		Graph c1=new Circle(3.0,PI);
		Graph s1=new Square(4.0);
		Graph s2=new Squareness(10.0,5.0);
		// 打印对象信息
		System.out.println(c1.toString());
		System.out.println(s1.toString());
		System.out.println(s2.toString());
	}
}

6.Comparable.Class

public interface Comparable {
	// 比较两个图形面积大小
	public int CompareTo(Graph A,Graph B);
	// 找出面积最大的图形
	public String CompareTo(Graph[] graph_Obj);
	
}

7.Comparable_Impl.Class

public class Comparable_Impl implements Comparable{
	
	@Override
	public String CompareTo(Graph[] g) {
		double ss;
		double max=0.0;
		String index="?";
		String a="[";
		for(int i=0;i<g.length;i++){
			ss=g[i].getArea();
			max=max>ss?max:ss;
			if(max==ss)
				index=Integer.toString(1+i);
			a+=" ("+Integer.toString(1+i)+"."+g[i].getClass().toString().split("ic.")[1]+")"+Double.toString(ss)+"\n";
		}
		a+=" ]\n当前第"+index+"个图形面积最大,它的面积为"+Double.toString(max);
		return a;
	}

	@Override
	public int CompareTo(Graph A, Graph B) {
		int reeult=-2;
		if(A.getArea()>B.getArea()){
			reeult=1;
		}
		if(A.getArea()==B.getArea()){
			reeult=0;
		}
		if(A.getArea()<B.getArea()){
			reeult=-1;
		}
		return reeult;
	}
	
}

8.test_2.Class

public class test_2 {
	public static void main(String[] args) {
		double PI=test_1.PI; // test_1的static PI=3.14
		// 绘制图形对象
		Circle c1=new Circle(4.5,PI);
		Square s1=new Square(4.0);
		Squareness s2=new Squareness(10.0,5.0);
		
		// 实例化
		Comparable_Impl Ci=new Comparable_Impl();
		
		// (1)比较两个图形对象面积的大小
		int see=Ci.CompareTo(c1, s2);
		if(see==1){
			System.out.println("对象前者较大");
		}else if(see==0){
			System.out.println("两个对象等大");
		}else if(see==-1){
			System.out.println("对象后者较大");
		}
		
		System.out.println("---------------------------------------------");
		
		// (2)在图形对象数组里找出面积最大的图形
		Graph[] graph={ 
					c1,
					new Circle(8.0,3.1),
					s1,
					new Squareness(25.0,8.0),
					s2,
					new Square(14.09)
				};
		System.out.println(Ci.CompareTo(graph));
		
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值