JAVA编写一个测试类,要求: 创建5个点,即5个Point对象 输出X坐标最大的点的坐标。 输出点关于原点的对称点。 分别输出两点间距离最近和最远的距离。
时间: 2023-04-12 14:01:09 浏览: 94
好的,以下是我给出的答案:
```
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坐标最大的点的坐标
Point maxX = points[0];
for (int i = 1; i < points.length; i++) {
if (points[i].getX() > maxX.getX()) {
maxX = points[i];
}
}
System.out.println("X坐标最大的点的坐标为:" + maxX.toString());
// 输出点关于原点的对称点
for (int i = 0; i < points.length; i++) {
Point symmetrical = new Point(-points[i].getX(), -points[i].getY());
System.out.println(points[i].toString() + "关于原点的对称点为:" + symmetrical.toString());
}
// 分别输出两点间距离最近和最远的距离
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].distance(points[j]);
if (distance < minDistance) {
minDistance = distance;
}
if (distance > maxDistance) {
maxDistance = distance;
}
}
}
System.out.println("两点间距离最近的距离为:" + minDistance);
System.out.println("两点间距离最远的距离为:" + maxDistance);
}
}
```
希望我的回答能够帮到你!
阅读全文