定义一个点类(Point),其数据成员包含横坐标和纵坐标;再定义一个距离类(Distance),用于描述两点之间

2025-01-06 06:59:23
推荐回答(1个)
回答1:

你好,程序如下:

public class Point {

double x,y;
double d;
Point(double a,double b){
x = a;
y = b;
}

void TestPoint(Point p1,Point p2){
x = (p1.x + p2.x)/2;
y = (p1.y + p2.y)/2;
System.out.println("(" + x + ","+ y + ")");
}

void Distance(Point p1,Point p2){
d = Math.sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
System.out.println(d);
}
public static void main(String[] args) {

Point p = new Point(0,0);
Point p1 = new Point(1,1);
Point p2 = new Point(2,2);
p.TestPoint(p1, p2);
p.Distance(p1, p2);
}

}