编写一个表示二维平面上的点的类MyPoint,满足以下条件:
(1)定义private的成员变量x和y,表示点的x和y坐标,类型为整数。
(2)定义两个MyPoint的构造方法,一个构造方法不带参数,且x和y的初始值为0,另一个构造方法有两个参数,参数名为x和y,类型为整数,用这两个参数分别作为初始x和y的坐标。
(3)定义一个getD方法,功能为返回两个坐标点(由MyPoint定义)距离,值为float类型。
(4)编写main方法,打印坐标点(2,3)到点(4,5)的距离。运行结果:2.828427
注:可以使用Math.sqrt()方法计算平方根。
import java.lang.*;
public class MyPoint{
private int x,y;
public MyPoint(){
this(0,0);}
public MyPoint(int x, int y){
this.x=x;
this.y=y;
}
public static float getD(MyPoint p,MyPoint q){
return (float)Math.pow(((float)Math.pow(q.x-p.x,2)+(float)Math.pow(q.y-p.y,2)),0.5);}
public static void main(String[] args){
MyPoint p =new MyPoint(2,3);
MyPoint q =new MyPoint(4,5);
System.out.println((float)MyPoint.getD(p,q));
}
}