1 public class Point {
2 private double x;
3 private double y;
4 private double z;
5
6 public double getX() {
7 return x;
8 }
9
10 public void setX(double x) {
11 this.x = x;
12 }
13
14 public double getY() {
15 return y;
16 }
17
18 public void setY(double y) {
19 this.y = y;
20 }
21
22 public double getZ() {
23 return z;
24 }
25
26 public void setZ(double z) {
27 this.z = z;
28 }
29
30 public Point(double x, double y, double z) {
31 this.x = x;
32 this.y = y;
33 this.z = z;
34 }
35
36 public void getJuLi() {
37 System.out.println("點(" + x + "," + y + "," + z + ")距離原點的平方=" + (x * x + y * y + z * z));
38 }
39
40 public static void main(String[] args) {
41 Point p = new Point(5, 3, 8);
42 p.setY(-2.5);
43 p.getJuLi();
44
45 Point p1 = new Point(-1, 3, 5);
46 p1.getJuLi();
47
48 }
49
50 }
定義一個“點”(Point)類用來表示三維空間中的點(有三個坐標)。要求如下:
(1)可以生成具有特定坐標的點對象。
(2)提供可以設置三個坐標的方法。
(3)提供可以計算該“點”距原點距離平方的方法。
(4)編寫主類程序驗證。
