定义一个“点”类用来表示三维空间中的点(有三个坐标)。要求:
1、可以生成具有特定坐标的点对象,构造器可以初始化点坐标。
2、属性定义成私有,getter和setter方法方位对象属性。
3、提供可以计算该“点”距原点距离方法。
4、编写PointTest测试构造方法,变量和方法取名必须规范,有清晰注释。

设计三维空间点类并使用该类计算空间距离
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- THEBUGKING 2022-03-15 12:06关注
//测试类 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; } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报