7.编写一个表示立体上点的类MyPoint,成员变量和成员方法为: double x,y,z,表示x?
double x,y,z,表示x、y和z坐标
MyPoint( )
MyPoint(double x,double y,double z)
double distance(MyPoint p),求两点之间的距离
创建两个MyPoint对象,坐标分别为(3,4,5)和(8,9,10),并通过对象求这两点之间的距离。 展开
package test20210112;
public class MyPoint {
// double x,y,z,表示x、y和z坐标
double x,y,z;
// MyPoint( )
public MyPoint(){}
态吵 // MyPoint(double x,double y,double z)
指携 public MyPoint(double x,double y,double z){
this.x = x;
this.y = y;
this.z = z;
}
帆逗侍 // double distance(MyPoint p),求两点之间的距离
double distance(MyPoint p){
double x1 = this.x - p.x;
double y1 = this.y - p.y;
double z1 = this.z - p.z;
double tmp = x1*x1 + y1*y1 +z1*z1;
return Math.sqrt(tmp);
}
}
package test20210112;
public class TestMyPoint {
public static void main(String[] args) {
//创建两个MyPoint对象,坐标分别为(3,4,5)和(8,9,10),并通过对象求这两点之间的距离。
MyPoint p1 = new MyPoint(3, 4, 5);
MyPoint p2 = new MyPoint(8, 9, 10);
System.out.println("两点之间的距离为: ±"+p2.distance(p1));
}
}