java编写Point类,要求: 具有属性:点的坐标(二维或三维) 具有方法:输出X坐标最大的点的坐标。 输出点关于原点的对称点。 分别输出两点间距离最近和最远的距离。
时间: 2023-04-12 14:00:27 浏览: 95
java 程序代码 point类
3星 · 编辑精心推荐
好的,这是一个编程类的问题,我可以回答。以下是 Java 编写的 Point 类的代码:
```
public class Point {
private double x;
private double y;
private double z;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
public Point getMaxXPoint(Point[] points) {
Point maxXPoint = points[0];
for (int i = 1; i < points.length; i++) {
if (points[i].getX() > maxXPoint.getX()) {
maxXPoint = points[i];
}
}
return maxXPoint;
}
public Point getSymmetricPoint() {
if (z == 0) {
return new Point(-x, -y);
} else {
return new Point(-x, -y, -z);
}
}
public double getDistance(Point p) {
double dx = x - p.getX();
double dy = y - p.getY();
double dz = z - p.getZ();
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
public double getMinDistance(Point[] points) {
double minDistance = Double.MAX_VALUE;
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
double distance = getDistance(points[i], points[j]);
if (distance < minDistance) {
minDistance = distance;
}
}
}
return minDistance;
}
public double getMaxDistance(Point[] points) {
double maxDistance = 0;
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
double distance = getDistance(points[i], points[j]);
if (distance > maxDistance) {
maxDistance = distance;
}
}
}
return maxDistance;
}
}
```
其中,getMaxXPoint 方法可以输出 X 坐标最大的点的坐标,getSymmetricPoint 方法可以输出点关于原点的对称点,getMinDistance 和 getMaxDistance 方法分别可以输出两点间距离最近和最远的距离。
阅读全文