java网课学习的练习题:在TestCylinder类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积

编程入门 行业动态 更新时间:2024-10-23 12:38:40

java网课学习的练习题:在TestCylinder类中创建Cylinder类的对象,设置<a href=https://www.elefans.com/category/jswz/34/1736713.html style=圆柱的底面半径和高,并输出圆柱的体积"/>

java网课学习的练习题:在TestCylinder类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积

java网课学习的练习题:在TestCylinder类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积

网课地址:=57

练习要求如上
Circle类

public class Circle {double radius;Circle() {radius = 1;}public double getRadius() {return radius;}public void setRadius(double radius) {this.radius = radius;}public double getArea() {return 3.14*radius*radius;} 
}

Cylinder类

public class Cylinder extends Circle {double length;public Cylinder() {this.length=1;}//获取圆柱的高public double getLength() {return length;}
//设置圆柱的高public void setLength(double length) {this.length = length;}//返回和计算圆柱的体积public double getVolume() {return this.getArea()*this.length;}
}

TestCylinder

public class TestCylinder {public static void main(String[] args) {Cylinder cylinder = new Cylinder();//设置圆的半径和圆柱的高都为2cylinder.setRadius(2);cylinder.setLength(2);cylinder.getRadius();cylinder.getLength();System.out.println("圆的面积:"+cylinder.getArea());System.out.println("圆柱的体积:"+cylinder.getVolume());}
}

运行结果

请大家多多指教!谢谢!

更多推荐

java网课学习的练习题:在TestCylinder类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积

本文发布于:2024-02-08 20:46:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1674856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:圆柱   底面   练习题   半径   体积

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!