java编写一个测试类,要求: 创建5个点,即5个Point对象 输出X坐标最大的点的坐标。 输出点关于原点的对称点。 分别输出两点间距离最近和最远的距离。
时间: 2023-04-10 13:04:00 浏览: 122
使用java编写的测试类
好的,以下是我给出的答案:
```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 + ")";
}
}
```
希望我的回答能够帮到你!
阅读全文