共回答了18个问题采纳率:100%
public class Point {
private double x;
private double y;
private double z;
//无参数默认原点
public Point() {
this(0,0,0);
}
//构造方法指定坐标
public Point(double x,double y,double z) {
this.x = x;
this.y = y;
this.z = z;
}
public static void main(String args[]){
Point p = new Point(1,1,1);//构造,1,1,1点,距离远点距离是根号3,1.732.
System.out.println("Before new valued set,the point is:" + p.toString());
System.out.println("Before new valued set,the distance to (0,0,0) is:" + p.getDistance());
p.setPosition(2,2,2);//重新设置点坐标
System.out.println("After new valued set,the point is:" + p.toString());
System.out.println("After new valued set,the distance to (0,0,0) is:" + p.getDistance());
}
public void setX(double x) {//设置x坐标
this.x = x;
}
public void setY(double y) {//设置y坐标
this.y = y;
}
public void setZ(double z) {//设置z坐标
this.z = z;
}
public void setPosition(double x,double y,double z){//一次性设置三个坐标的方法
setX(x);
setX(y);
setX(z);
}
// 计算距离 x*x + y*y+z*x,然后开平方
public double getDistance(){
final int square = 2;//平方
return Math.sqrt(Math.pow(x,square) + Math.pow(y,square)+ Math.pow(z,square));
}
public String toString(){//重写输出方法
return "(x,y,z):" + x + "," + y + "," + z;
}
}
-------------
Before new valued set,the point is:(x,y,z):1.0,1.0,1.0
Before new valued set,the distance to (0,0,0) is:1.7320508075688772
After new valued set,the point is:(x,y,z):2.0,1.0,1.0
After new valued set,the distance to (0,0,0) is:2.449489742783178
1年前
1