根据周长计算不同形状图形的面积?计算多种图形的面积

/**
 *  根据周长计算不同形状图形的面积?计算多种图形的面积,
 *  并比较各种图形面积的最大值。正方形的面积公式为:0.0625*c*c。
 *  圆形的面积公式为:0.0796*c*c,其中,c表示图形的周长。
 */
/**
 *    根据周长计算不同形状图形的面积?计算多种图形的面积,
 *  并比较各种图形面积的最大值。正方形的面积公式为:0.0625*c*c。
 *  圆形的面积公式为:0.0796*c*c,其中,c表示图形的周长。
 */
public class AreaTest {

    public static void main(String[] args) {

        //学过了集合,尝试用集合做此题的方法:
        //定义一个集合,并添加图形元素实例的地址到集合中
        /*List<Shape> shapesList = new ArrayList<Shape>();
        shapesList.add(new Circle(1));
        shapesList.add(new Circle(15));
        shapesList.add(new Square(5));
        shapesList.add(new Square(23));*/

        //比较大小
        /* 1.数组比较大小的方法,是假设一个最大值,然后依次比较,交换最大值
         * 2.集合怎么比呢?尝试用同样的办法.
         * 3.假定第一个元素是最大值,需要先求出第一个园的面积,赋值给double max;
         * 4.遍历集合,用迭代器能交换元素么?
         * */
        /* 不管那种方法,第一步都要设定最大值 
         * 集合获取元素的方法是list.get(元素下标)
         */
        /*double MaxArea = shapesList.get(0).area();*/

        /*新循环遍历集合, 但是无法操作具体的下标,所以不知道最大图形的下标*/
        /*for(Shape shapes : shapesList){
            if(MaxArea < shapes.area()){
                MaxArea = shapes.area();
            }
        }
        System.out.println("最大面积为: "+MaxArea);*/

        /*用老循环,可以求出最大面积和下标位置.*/
        /*int indexMax = 0;
        for(int i =0;i<shapesList.size();i++){
            if(MaxArea < shapesList.get(i).area()){
                MaxArea = shapesList.get(i).area();
                indexMax = i;
            }
        }
        System.out.println("最大面积为: "+MaxArea);
        System.out.println("最大面积对应的下标为: "+indexMax);*/

        /*迭代器做法*/
        /*Iterator<Shape> it = shapesList.iterator();
        while(it.hasNext()){
            Shape shapes = it.next();
            if(MaxArea < shapes.area()){
                MaxArea = shapes.area();
            }
        }
        System.out.println("最大面积为: "+MaxArea);*/

        /* 数组做法 */
        //定义数组,存储图形对象.
        Shape[] shapes = new Shape[4];
        shapes[0] = new Circle(1);
        shapes[1] = new Circle(2);
        shapes[2] = new Square(5);
        shapes[3] = new Square(4);

        double maxArea = shapes[0].area();
        int index = 0;
        for(int i=0;i<shapes.length;i++){
            double area = shapes[i].area();
            if(maxArea < area){
                maxArea = area;
                index = i;
            }
        }
        System.out.println("最大面积为: "+maxArea);
        System.out.println("最大面积对应的下标为: "+index);
    }

}
View Code

定义各个图形的类

/*求图形面积,因为不知道具体的图形,必须定义一个抽象类,图形都有周长,但是面积求法不同,每办法写具体*/
abstract class Shape{
    protected double c;
    //图形的周长,本题为给定的数值,所以可以当做一个已知属性,直接构造方法时赋值c.
    abstract double area(); 
    //因为各种不同图形的面积公式不一样,所以用抽象方法写出,由子类根据公式的不同具体的实施.
}

class Circle extends Shape {
    /*圆形的构造方法 创建圆型实例时,赋c的值*/
    public Circle(double c){
        this.c = c;
    }

    /*根据圆的面积公式求园的面积*/
    @Override
    double area() {
        return c*c*0.0796;
    }
}

class Square extends Shape{
    /*方的构造方法 创建方型实例时,赋c的值*/
    public Square(double c){
        this.c = c;
    }

    /*根据方形的面积公式求方形的面积*/
    @Override
    double area() {
        return c*c*0.0625;
    }
}
View Code

 

posted @ 2016-05-01 09:14  安仔80  阅读(1326)  评论(0编辑  收藏  举报