Java 圆类

来源:百度知道 编辑:UC知道 时间:2024/06/07 19:28:10
并定义如下方法成员:定义球类Sphere,数据成员xpos,ypos,zpos描述球心的x,y, z坐标,radius描述半径
1.定义构造方法:Sphere ();//将半径初始化为1,圆心为(0,0, 0)
2. 重载构造方法:Sphere (int x, int y, int z, double r );//将球心、半径初始化为(x,y,z)、r;
3. 重构方法:public String toString();//返回球心及半径的字符串描述
4. 定义main方法,生成圆心在(10,10,10),半径为20的球,输出其字符串描述信息。

public class Sphere
{
private int xpos, ypos, zpos;
private double radius;

public Sphere ()
{
this.radius = 1.0;
this.xpos = 0;
this.ypos = 0;
this.zpos = 0;
}

public Sphere (int x, int y, int z, double r)
{
this.radius = r;
this.xpos = x;
this.ypos = y;
this.zpos = z;
}

public String toString ()
{
return "x_position is: " + this.xpos +
"\ny_position is: " + this.ypos +
"\nz_position is: " + this.zpos;
}

public static void main (String[] args)
{
System.out.println (new Sphere(10, 10, 10, 20.0).toString());
}
}