求Java大神编写程序
1.可以生成带初始坐标的点对象。Point p = new Point(1,2,0);
2.提供可以设置三个坐标的方法。setX、setY、setZ
3. 提供可以获取三个坐标的方法。getX、getY、getZ
3.提供可以计算某个“点”距原点距离平方的方法。p.distance( );
编写程序验证上述1-3条。
输出结果类似:
x:1.0
y:2.0
z:3.0
distance:14.0
change x y z
x:5.0
y:4.0
z:0.0
distance:41.0
p to q distance:17.0 展开
Java参考源代码:
public class Test {
public static void main(String[] args) {
Point p = new Point(1, 2, 3);
Point p0 = new Point(0, 0, 0);
Point q = new Point(1, 3, 0);
System.out.println("x:" + p.getX());
System.out.println("y:" + p.getY());
System.out.println("z:" + p.getZ());
System.out.println("distance:" + p.distance(p0));
p.setX(5);
p.setY(4);
p.setZ(0);
System.out.println("change x y z");
System.out.println("x:" + p.getX());
System.out.println("y:" + p.getY());
System.out.println("z:" + p.getZ());
System.out.println("distance:" + p.distance(p0));
System.out.println("p to q distance:" + p.distance(q));
}
}
class Point {
protected double x;
protected double y;
protected double z;
public Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public void setX(double x) {
this.x = x;
}
public double getX() {
return this.x;
}
public void setY(double y) {
this.y = y;
}
public double getY() {
return this.y;
}
public void setZ(double z) {
this.z = z;
}
public double getZ() {
return this.z;
}
public double distance(Point p) {
return (this.x - p.getX()) * (this.x - p.getX())
+ (this.y - p.getY()) * (this.y - p.getY())
+ (this.z - p.getZ()) * (this.z - p.getZ());
}
}
运行结果: