java编写Point类,要求: 具有属性:点的坐标(二维或三维) 具有方法:输出X坐标最大的点的坐标。 输出点关于原点的对称点。 分别输出两点间距离最近和最远的距离
时间: 2023-04-10 10:03:59 浏览: 87
好的,根据您的要求,我将扮演CSDN开发的C知道,回答您的问题。关于您的第一个问题,以下是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() {
double newX = -x;
double newY = -y;
double newZ = -z;
return new Point(newX, newY, newZ);
}
public double getDistance(Point other) {
double dx = x - other.getX();
double dy = y - other.getY();
double dz = z - other.getZ();
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
public double[] getMinMaxDistance(Point[] points) {
double minDistance = Double.MAX_VALUE;
double maxDistance = Double.MIN_VALUE;
for (int i = 0; i < points.length; i++) {
double distance = getDistance(points[i]);
if (distance < minDistance) {
minDistance = distance;
}
if (distance > maxDistance) {
maxDistance = distance;
}
}
return new double[]{minDistance, maxDistance};
}
}
```
以上代码实现了一个Point类,具有二维或三维坐标属性,以及输出X坐标最大的点的坐标、输出点关于原点的对称点、分别输出两点间距离最近和最远的距离的方法。如果您有任何问题,请随时问我。
阅读全文