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

这段Java代码定义了两个类,Circle和Rectangle,分别表示圆形和矩形柱体,以及它们的子类CircleyCylinder和Rectcylinder表示圆柱和矩形柱体。代码中包含了计算体积和表面积的方法,并在main函数中进行了实例化和打印结果。
摘要由CSDN通过智能技术生成

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

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值