有若干个直柱体(底面与柱面垂直),其底面可能是圆形、矩形。已知柱体的高度、圆的半径、矩形的宽和高。计算柱体的体积和表面积。

public class test {
    public static void main(String[] args) {
        Rectcylinder rectcylinder=new Rectcylinder(3,4,5);
        CircleyCylinder cylinder=new CircleyCylinder(3,4);
        System.out.println(rectcylinder.toString());
        System.out.println("该矩形体的体积为:"+ rectcylinder.v()+",面积为:"+rectcylinder.s());
        System.out.println(cylinder.toString());
        System.out.println("该圆柱体的体积为:"+ cylinder.v()+",面积为:"+cylinder.s());
    }
}
 class Circle {
    private int r;
    private static double PI=3.14;

    public Circle() {
    }

    public Circle(int r) {
        this.r = r;
    }


    public int getR() {
        return r;
    }


    public void setR(int r) {
        this.r = r;
    }

    public String toString() {
        return "Circle{r = " + r + "}";
    }

    public double s(){
        return PI*r*r;
    }

    public double c(){
        return 2*PI*r;
    }
}
 class Rectangle {
    private int length;
    private int width;

    public Rectangle() {
    }

    public Rectangle(int length, int width) {
        this.length = length;
        this.width = width;
    }


    public int getLength() {
        return length;
    }


    public void setLength(int length) {
        this.length = length;
    }


    public int getWidth() {
        return width;
    }


    public void setWidth(int width) {
        this.width = width;
    }

    public String toString() {
        return "Rectangle{length = " + length + ", width = " + width + "}";
    }

    public int s(){
        return this.length*this.width;
    }

    public  int c(){
        return 2*(width+length);
    }
}
 class CircleyCylinder extends Circle{
    private int height;

    public CircleyCylinder() {
    }

    public CircleyCylinder(int r, int h) {
        setR(r);
        this.height = h;
    }

    public int getH() {
        return height;
    }

    public void setH(int h) {
        this.height = h;
    }

    public String toString() {
        return super.toString()+"CircleyCylinder{h = " + height + "}";
    }

    public double v(){
        return super.s()*height;
    }
    public double s(){
        return super.s()*2+super.c()*height;
    }
}
 class Rectcylinder extends Rectangle{
    private int height;

    public Rectcylinder() {
    }

    public Rectcylinder(int length,int width, int h) {
        setLength(length);
        setWidth(width);
        this.height = h;
    }

    public int getH() {
        return height;
    }

    public void setH(int h) {
        this.height = h;
    }

    public String toString() {
        return super.toString()+ "Rectcylinder{h = " + height + "}";
    }

    public int s(){
        return super.s()*2+super.c()*height;
    }

    public int v(){
        return super.s()*height;
    }

}

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐