java编写一个测试类,要求: 创建5个点,即5个Point对象 输出X坐标最大的点的坐标。 输出点关于原点的对称点。 分别输出两点间距离最近和最远的距离。
时间: 2023-04-10 18:04:00 浏览: 123
Java 编写Point类,构造方法,计算两个点之间的距离,键盘输入两个点,格式以“,”隔开,如:1,1即为(1,1)点。
5星 · 资源好评率100%
好的,以下是我给出的答案:
```java
import java.util.Arrays;
public class TestPoint {
public static void main(String[] args) {
Point[] points = new Point[5];
points[0] = new Point(1, 2);
points[1] = new Point(3, 4);
points[2] = new Point(5, 6);
points[3] = new Point(7, 8);
points[4] = new Point(9, 10);
// 输出X坐标最大的点的坐标
Arrays.sort(points, (a, b) -> b.x - a.x);
System.out.println("X坐标最大的点的坐标:" + points[0]);
// 输出点关于原点的对称点
for (Point point : points) {
System.out.println(point + "关于原点的对称点:" + point.getSymmetricPoint());
}
// 分别输出两点间距离最近和最远的距离
double minDistance = Double.MAX_VALUE;
double maxDistance = Double.MIN_VALUE;
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
double distance = points[i].getDistance(points[j]);
if (distance < minDistance) {
minDistance = distance;
}
if (distance > maxDistance) {
maxDistance = distance;
}
}
}
System.out.println("两点间距离最近的距离:" + minDistance);
System.out.println("两点间距离最远的距离:" + maxDistance);
}
}
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point getSymmetricPoint() {
return new Point(-x, -y);
}
public double getDistance(Point other) {
return Math.sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}
```
希望我的回答能够帮到你!
阅读全文