圆柱的底面半径和高,并输出圆柱的体积"/>
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类的对象,设置圆柱的底面半径和高,并输出圆柱的体积
发布评论