7. 编写一个具有求圆面积功能的类,并应用之求圆柱体的体积。

用java语言编写啊。
2025-01-31 18:47:27
推荐回答(2个)
回答1:

public class Test2 {

public static void main(String[] args) {

double r = 2;
double h = 5;

double volume = Cylinder.cubage(r, h);

System.out.println("Volumen is: " + volume);
}

}

class Circle {//圆
private double r;

public Circle(double radius) {
this.r = radius;
}

public double area() {
return Math.PI * Math.pow(r, 2);
}

}

class Cylinder {//圆柱体

public static double cubage(double r, double h) {//求体积
Circle baseCircle = new Circle(r);//底面圆
return baseCircle.area() * h;
}

}
时双崽崽!

回答2:

public class area
{

private:
float r;
public:
float s( r);
}
public float area:s(r)
{
float s;
s=3.1415926*r*r;
return s;
}

main()
{
float v;
float h;
scanf("%f,%f",h,r);
area carea;
v=carea::s(r)*h;
printf("%f",v);
}