zoukankan      html  css  js  c++  java
  • 求一组图形中的最大面积

    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;
    }
    }

  • 相关阅读:
    Add Two Numbers
    Same Tree
    Single Number
    题目1190:大整数排序
    题目1182:统计单词
    题目1181:遍历链表
    题目1180:对称矩阵
    题目1179:阶乘
    题目1206:字符串连接
    HTML事件
  • 原文地址:https://www.cnblogs.com/xiaziteng/p/4731408.html
Copyright © 2011-2022 走看看