求java简单编程,谢谢?

编程实现:定义一个圆柱体类,包含的内容如下:
成员变量----底面半径、高和圆周率;
成员方法----底面积、体积;
编程实现这个类,并在方法main( )中创建相应的对象,然后计算底面半径为2.5,高为5时圆柱体的底面积和体积。

public class CircularCylinder {

double z = 3.14;
double r;//半径
double h;//高

public CircularCylinder(double r,double h) {
this.r = r;
this.h = h;
}

public double getBottomArea(){//底面积
return z*r*r;
}

public double getVolume(){//体积
return getBottomArea()*h;
}

public static void main(String[] args) {
CircularCylinder circularCylinder = new CircularCylinder(5,12);//实例化半径为5高为12的圆柱
System.out.println("半径为5高为12的圆柱的底面积为:"+circularCylinder.getBottomArea());
System.out.println("半径为5高为12的圆柱的体积为:"+circularCylinder.getVolume());
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-10-31
public class Cylinder {
private double radius;
private double height;
private double pi = Math.PI;

public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}

private double area() {
return this.pi * this.radius * this.radius;
}

private double volume() {
return this.area() * this.height;
}

public static void main(String[] args) {
Cylinder cylinder = new Cylinder(2.5, 5);
System.out.println("底面积:" + cylinder.area());
System.out.println("体积:" + cylinder.volume());
}
}