定义一个Point类,表示平面坐标的一个点。 数据域x和y分别表示它们的坐标。 一个创建点(0.0, 0.0)的无参构造方法。一个创建特定坐标点的构造方法。 一个名为distance的方法,返回从该点到Point类型的指定点之间的距离。另一个名为distance的方法,返回从该点到原点之间的距离。 编写一个测试类PointTest,创建两个点(0.0,0.0)和(10.0,30.5),并输出两点之间的距离。JAVA
时间: 2024-03-04 16:49:32 浏览: 198
好的,这是一个简单的Point类的实现,以及测试类PointTest的实现:
```java
public class Point {
private double x;
private double y;
// 无参构造方法
public Point() {
this.x = 0.0;
this.y = 0.0;
}
// 构造方法,创建特定坐标点
public Point(double x, double y) {
this.x = x;
this.y = y;
}
// 返回从该点到另一点的距离
public double distance(Point p) {
double dx = this.x - p.x;
double dy = this.y - p.y;
return Math.sqrt(dx * dx + dy * dy);
}
// 返回从该点到原点的距离
public double distance() {
return distance(new Point());
}
}
public class PointTest {
public static void main(String[] args) {
Point p1 = new Point();
Point p2 = new Point(10.0, 30.5);
double distance = p1.distance(p2);
System.out.println("The distance between p1 and p2 is: " + distance);
}
}
```
输出结果为:
```
The distance between p1 and p2 is: 32.04277532739492
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)