JAVA面对对象程序设计基础练习题

有若干个直柱体(底面和柱面垂直),其底面可能是圆形、矩形和三角形。已知柱体的高度,定义圆的半径、矩形的长度和宽度及三角形的三个边长(假设能构成三角形),计算柱体的体积和表面积。(包括两个底面积)

public class Practice5 {
    public static void main(String[] args) {
class  Shape {
            static final double height=10;
            double radii;
            double length ;
            double width ;
            double l1;
            double l2;
            double l3;
//圆的半径构造方法
            public Shape(double radii) {
                this.radii = radii;
            }

            //矩形构造方法
            public Shape(double length, double width) {
                this.length = length;
                this.width = width;
            }
//三角形三条边构造方法
            public Shape(double l1, double l2, double l3) {
                this.l1 = l1;
                this.l2 = l2;
                this.l3 = l3;
            }

            public String s(){
                System.out.println();
return "计算结束";

            }
        }
     //底面为圆形
        class Circle extends Shape{
            double circumference;//周长
            static  final double PI = 3.14;//定义为常量,不能改变
            public Circle(double radii) {
                super(radii);
                this.radii=radii;
            }
            @Override
            public String s() {
                double s1 = PI * radii * radii;     //一个底面积
                circumference = 2 * PI * radii;           //周长
                double s2 = circumference * height;    //侧面积
                double area = 2 * s1 + s2;           //表面积
                double volume = s1 * height;     //体积
                System.out.println("圆的面积为:" + area + ",圆的体积为:" + volume);

                return "";
            }
        }
 //矩形
        class Rectangle extends Shape{
            public Rectangle(double length ,double width) {
                super(length,width);
            }


            @Override
            public String s() {
                double s1 = (length*width)*2;   //上下面积
                double s2 = (length*height+width*length)*2;   //矩形侧面积
                double area = s1 + s2;
                double volume = s1*height;
                System.out.println("矩形的表面积为:"+area+",体积为:"+volume);

                return "";
            }
        }


        //三角形
        class Triangle extends Shape {


            public Triangle(double l1, double l2,double l3) {
                super(l1, l2, l3);
            }

            @Override
            public String s() {
                double p = (l1+l2+l3)/2;
                double x = p*(p-l1)*(p-l2)*(p-l3);
               double area = Math.sqrt(x);
                double volume = area * height;
                System.out.println("三角形的面积为:"+area+",体积为:"+volume);

                return "";
            }
        }
        Shape shape;
        shape = new Circle(2);
        System.out.println(shape.s());
        shape = new Triangle(3,4,5);
        System.out.println(shape.s());
        shape = new Rectangle(4,8);
        System.out.println(shape.s());


    }
    }

运行结果为:

圆的面积为:150.72,圆的体积为:125.60

三角形的面积为:6.0,体积为:60.0

矩形的表面积为:208.0,体积为:640.0

你可能感兴趣的:(Java语言,java,开发语言)