【发布时间】:2021-12-08 15:24:02
【问题描述】:
我正在编写一个 Java 程序,该程序使用以下构造函数实现 Point 数据类型:
点(双 x,双 y,双 z)
还有以下 API:
-
双距离到(点 q) 它返回 this 和 q 之间的欧几里得距离。 (x1, y1, z1) 和 (x2, y2, z2) 之间的欧几里得距离定义为 sqrt( (x1-x2)^2 + (y1-y2)^2) + (z1-z2)^2)。
-
String toString() – 它返回点的字符串表示。一个例子是 (2.3,4.5,3.0)。
-
在用于测试它的类中编写一个 main 方法。 它应该使用用户在命令行上提供的输入创建两个 Point 对象。 然后它应该打印出两个点,然后是它们的欧几里得距离 示例运行如下。
java点2.1 3.0 3.5 4 5.2 3.5
第一个点是(2.1,3.0,3.5)
第二点是(4.0,5.2,3.5)
他们的欧几里得距离是 2.90
程序无法编译,但我不知道为什么。我是编程新手,所以我按照在线和 Codecademy 的一些步骤尝试访问构造函数中的对象,但我认为我做错了。任何建议将不胜感激。
public class Point {
double x1;
double y1;
double z1;
double x2;
double y2;
double z2;
public Point(double x, double y, double z){
x1 = x;
y1 = y;
z1 = z;
x2 = x;
y2 = y;
z2 = z;
}
public double distanceTo(Point q){
return Math.sqrt(Math.pow((x1-x2), 2.0) + Math.pow((y1-y2), 2.0) + Math.pow((z1-z2), 2.0));
}
double x3 = x1-x2;
double y3 = y1-y2;
double z3 = z1-z2;
public String toString() {
return "(" + x3 + ", " + y3 + ", " + z3 + ")";
}
public static void main (String[]args){
Point pointOne = new Point(args[0]);
Point pointTwo = new Point(args[1]);
Point distance = new distanceTo();
System.out.println("The first point is " + "(" + pointOne + ")");
System.out.println("The second point is " + "(" + pointTwo + ")");
System.out.println("Their Euclidean distance is " + distance);
}
}
【问题讨论】:
标签: java class constructor double euclidean-distance