定義一個“點”(Point)類用來表示三維空間中的點(有三個坐標)。要求如下:
(1)可以生成具有特定坐標的點對象。
(2)提供可以設置三個坐標的方法。
(3)提供可以計算該“點”距原點距離平方的方法。
(4)編寫主類程序驗證。
package b; public class Point { private int x,y,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; } public Point(int x, int y, int z) { super(); this.x = x; this.y = y; this.z = z; } double getDistance(Point p) { return (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)+(z-p.z)*(z-p.z); } public static void main(String[] args) { Point p1=new Point(0,0,0); Point p2=new Point(1,2,4); System.out.println("距原點的距離平方是:"+p2.getDistance(p1)); System.out.println("距原點的距離平方是:"+p2.getDistance(new Point(2,4,4))); p2.setX(2); p2.setY(0); p2.setZ(0); System.out.println("修改坐標后,距任一點的距離平方是:" + p2.getDistance(p1)); } }