码迷,mamicode.com
首页 > 其他好文 > 详细

求一组图形中的最大面积

时间:2015-08-14 23:58:11      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

package oo.day06;
//求一组图形中的最大面积
public class ShapeTest {
public static void main(String[] args) {
//Shape s = new Shape(); //编译错误,抽象类不能被实例化
Shape[] shapes = new Shape[4]; //创建Shape数组对象
shapes[0] = new Circle(1); //向上造型
shapes[1] = new Circle(2);
shapes[2] = new Square(1);
shapes[3] = new Square(2);
maxArea(shapes);
}
public static void maxArea(Shape[] shapes){ //求最大面积
double max = shapes[0].area(); //最大面积
int maxIndex = 0; //最大面积索引
for(int i=1;i<shapes.length;i++){
double area = shapes[i].area();
if(area>max){
max = area;
maxIndex = i;
}
}
System.out.println("最大面积为:"+max+",所在索引为:"+maxIndex);
}

}

abstract class Shape{ //抽象类
protected double c; //周长
public abstract double area(); //抽象方法
}
class Circle extends Shape{
public Circle(double c){
this.c = c;
}
public double area(){ //重写抽象方法
return 0.0796*c*c;
}
}
class Square extends Shape{
public Square(double c){
this.c = c;
}
public double area(){ //重写抽象方法
return 0.0625*c*c;
}
}

 

求一组图形中的最大面积

标签:

原文地址:http://www.cnblogs.com/xiaziteng/p/4731408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!