早教吧 育儿知识 作业答案 考试题库 百科 知识分享

Java练习寻求帮助!2.编写一个表示平面上点的类MyPoint,成员变量和成员方法为doublex,y,表示x和y坐标;MyPoint();MyPoint(doublex,doubley);doubledistance(MyPointmp),求两点之间的距离。

题目详情
Java练习 寻求帮助!
2.编写一个表示平面上点的类MyPoint,成员变量和成员方法为
double x, y, 表示x和y坐标;
MyPoint();
MyPoint (double x, double y) ;
double distance (MyPoint mp),求两点之间的距离。
▼优质解答
答案和解析
public class MyPoint {
private double x;
private double y;


public MyPoint() {
}
public MyPoint(double x, double y) {
this.x = x;
this.y = y;
}

public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public static double distance (MyPoint mp){
double distance = Math.sqrt(mp.getX()*mp.getX()+mp.getY()*mp.getY());
return distance;
}
public static void main(String[] args) {
MyPoint my1 = new MyPoint(3, 4);
MyPoint my2 = new MyPoint();
double dis =distance(my1);
System.out.println("两点距离:"+dis);

}
}
看了 Java练习寻求帮助!2.编...的网友还看了以下: