这道Java题怎么做?

定义一个”点“(Point)类用来表示三维空间中的点(有三个坐标)。要求如下:

1.     可以生成具有特定坐标的点对象

2.     提供可以设置三个坐标的方法

3.     提供可以计算该”点(1,2,3)“距另外点(5,0,0)距离的平方的方法

4.     编写程序验证上述三条

首先定义Point类
public class Point {
private int x;
private int y;
private int z;

//无参构造
public Point(){}
//带参数的构造函数,用于初始化坐标
public Point(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public int getZ() {
return z;
}

public void setZ(int z) {
this.z = z;
}
}
编写Main方法,执行,验证
import java.lang.Math;
public class Main {
public static double distancePOW(Point p1,Point p2)
{
return Math.pow(p1.getX()-p2.getX(),2)+Math.pow(p1.getY()-p2.getY(),2)+Math.pow(p1.getZ()-p2.getZ(),2);
}
public static void main(String[] args){
Point point1 = new Point(1,2,3);
Point point2 = new Point(5,0,0);
double distancePOW = distancePOW(point1,point2);
System.out.println("两个三维坐标点之间的距离的平方为"+distancePOW);
}
}
输出结果:两个三维坐标点之间的距离的平方为29.0
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答