作业帮 > 综合 > 作业

用java语句写出三维空间的点,例子如下:麻烦帮我加上注释~

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/07/04 20:22:37
用java语句写出三维空间的点,例子如下:麻烦帮我加上注释~
定义一个“点”(Point)类用来表示中三维空间的点(有三个坐标).要求如下:
(坐标的值随便)
//可以生成具有特定坐标的点对象.( 你的构造方法 能指定 他的坐标)
//提供可以设置三个坐标的方法.
//可以计算点到原点的距离
//编写程序验证上述三条.
public class Point {
private double x;
private double y;
private double z;
//无参数默认原点
public Point() {
this(0,0,0);
}
//构造方法指定坐标
public Point(double x,double y,double z) {
this.x = x;
this.y = y;
this.z = z;
}
public static void main(String args[]){
Point p = new Point(1,1,1);//构造,1,1,1点,距离远点距离是根号3,1.732.
System.out.println("Before new valued set,the point is:" + p.toString());
System.out.println("Before new valued set,the distance to (0,0,0) is:" + p.getDistance());
p.setPosition(2,2,2);//重新设置点坐标
System.out.println("After new valued set,the point is:" + p.toString());
System.out.println("After new valued set,the distance to (0,0,0) is:" + p.getDistance());
}
public void setX(double x) {//设置x坐标
this.x = x;
}
public void setY(double y) {//设置y坐标
this.y = y;
}
public void setZ(double z) {//设置z坐标
this.z = z;
}
public void setPosition(double x,double y,double z){//一次性设置三个坐标的方法
setX(x);
setX(y);
setX(z);
}
// 计算距离 x*x + y*y+z*x,然后开平方
public double getDistance(){
final int square = 2;//平方
return Math.sqrt(Math.pow(x,square) + Math.pow(y,square)+ Math.pow(z,square));
}
public String toString(){//重写输出方法
return "(x,y,z):" + x + "," + y + "," + z;
}
}
-------------
Before new valued set,the point is:(x,y,z):1.0,1.0,1.0
Before new valued set,the distance to (0,0,0) is:1.7320508075688772
After new valued set,the point is:(x,y,z):2.0,1.0,1.0
After new valued set,the distance to (0,0,0) is:2.449489742783178