永发信息网

用java语句写出三维空间的点,例子如下:麻烦帮我加上注释~~各位大哥大姐帮忙啦

答案:2  悬赏:0  手机版
解决时间 2021-01-30 14:39
  • 提问者网友:寂寞梧桐
  • 2021-01-29 22:08
定义一个“点”(Point)类用来表示中三维空间的点(有三个坐标)。要求如下:
(坐标的值随便)
//可以生成具有特定坐标的点对象。( 你的构造方法 能指定 他的坐标)
//提供可以设置三个坐标的方法。
//可以计算点到原点的距离
//编写程序验证上述三条。
最佳答案
  • 五星知识达人网友:狂恋
  • 2021-01-29 23:27
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
全部回答
  • 1楼网友:佘樂
  • 2021-01-29 23:38
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯