📄 sphere.java
字号:
class Sphere
{
static final double PI = 3.14; // Class variable that has a fixed value
static int count = 0; // Class variable to count objects
// Instance variables
double radius; // Radius of a sphere
double xCenter; // 3D coordinates
double yCenter; // of the center
double zCenter; // of a sphere
// Class constructor
Sphere(double theRadius, double x, double y, double z)
{
radius = theRadius; // Set the radius
// Set the coordinates of the center
xCenter = x;
yCenter = y;
zCenter = z;
++count; // Update object count
}
// Construct a unit sphere at a point
Sphere(double x, double y, double z)
{
xCenter = x;
yCenter = y;
zCenter = z;
radius = 1.0;
++count; // Update object count
}
// Construct a unit sphere at the origin
Sphere()
{
xCenter = 0.0;
yCenter = 0.0;
zCenter = 0.0;
radius = 1.0;
++count; // Update object count
}
// Static method to report the number of objects created
static int getCount()
{
return count; // Return current object count
}
// Instance method to calculate volume
double volume()
{
return 4.0/3.0*PI*radius*radius*radius;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -