这个sphere在求什么
- 教育综合
- 2024-12-31 07:57:42
Java 类Sphere求 3D球体积,用Testpheres测试?
public class TestSphere {
public static void main(String[] args) {
//0
System.out.println("count="+Sphere.getCount());
//1
Sphere s1=new Sphere(0,5,6,7);
System.out.println("count="+Sphere.getCount());
//2
Sphere s2=new Sphere(10,3,7,5);
System.out.println("count="+Sphere.getCount());
//volume
System.out.println("volume="+s1.volume());
System.out.println("volume="+s2.volume());
}
}
class Sphere{
static final double PI=3.14;
static int count;
private double xCenter,yCenter,zCenter,radius;
Sphere(double theRadius,double x,double y,double z){
this.xCenter=x;
this.yCenter=y;
this.zCenter=z;
if(theRadius<=0) {
this.radius=getRadius();
}else {
this.radius=theRadius;
}
count++;
}
public static int getCount() {
return count;
}
public double volume() {
return getNum((4.0/3.0)*(Math.pow(radius, 3))*PI);
}
private double getRadius() {
this.radius=getNum(Math.sqrt(getDou(xCenter)+getDou(yCenter)+getDou(zCenter)));
return this.radius;
}
private double getDou(double num) {
return Math.pow(num, 2);
}
private double getNum(double num) {
return new java.math.BigDecimal(num).setScale(3, 4).doubleValue();
}
}
c++新手编程题,在线等,求解,谢谢大神!
表面积、体积,分两个函数,分别返回
或者这样 ~