1、将笛卡尔坐标系上的点定义为一个服务类point,point类求坐标系上两点间的距离。设计测试point服务类的应用程序主类,显示输出已创建对象间的距离。
时间: 2024-05-04 16:18:03 浏览: 124
以下是实现point类和测试应用程序主类的示例代码:
```java
// point类
public class point {
private double x;
private double y;
public point(double x, double y) {
this.x = x;
this.y = y;
}
public double distanceTo(point p) {
double dx = this.x - p.x;
double dy = this.y - p.y;
return Math.sqrt(dx * dx + dy * dy);
}
}
// 测试应用程序主类
public class TestPoint {
public static void main(String[] args) {
point p1 = new point(1, 1);
point p2 = new point(4, 5);
double distance = p1.distanceTo(p2);
System.out.println("The distance between (" + p1.x + ", " + p1.y + ") and (" + p2.x + ", " + p2.y + ") is " + distance);
}
}
```
运行测试应用程序主类后输出结果为:
```
The distance between (1.0, 1.0) and (4.0, 5.0) is 5.0
```
这表明point类成功地计算出了两点之间的距离。
阅读全文