定义一个“点”类用来表示三维空间中的点(有三个坐标)。要求:
1、可以生成具有特定坐标的点对象,构造器可以初始化点坐标。
2、属性定义成私有,getter和setter方法方位对象属性。
3、提供可以计算该“点”距原点距离方法。
4、编写PointTest测试构造方法,变量和方法取名必须规范,有清晰注释。
//测试类
public class PointTest {
public static void main(String[] args) {
//原点
Coordinate coordinate1 = new Coordinate(0, 0, 0);
//坐标点
Coordinate coordinate2 = new Coordinate(1, 1, 1);
//计算方法
double address = Coordinate.getAddress(coordinate1, coordinate2);
//打印结果
System.out.println(address);
}
}
//坐标类
public class Coordinate {
//x轴
private int x;
//y轴
private int y;
//z轴
private int z;
//构造方法
public Coordinate(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
//New一个对象(0,0,0)即可算出到原点距离
public static double getAddress(Coordinate coordinate1, Coordinate coordinate2) {
int x1 = coordinate1.getX();
int y1 = coordinate1.getY();
int z1 = coordinate1.getZ();
int x2 = coordinate2.getX();
int y2 = coordinate2.getY();
int z2 = coordinate2.getZ();
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
}
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;
}
}