我有以下Java代码
我能够以printDistance
方法打印计算出的值。
输出:
4.0
3.0
但是我需要以下格式的输出(预期输出)。请帮助
3D距离= 4.0
2D距离= 3.0
public class Point2D {
double x;
double y;
public double getX() {
return x;
}
public double getY() {
return y;
}
public Point2D(double x, double y) {
this.x = x;
this.y = y;
}
public double dist2D(Point2D p) {
x = (int) Math.ceil(Math.sqrt(Math.pow((p.getX() - x), 2) + Math.pow((p.getY() - y), 2)));
return x;
}
public static void printDistance(double d) {
System.out.println(d);
}
static public void main(String args[]) {
Point3D obj1 = new Point3D(1.0, 2.0, 3.0);
Point3D obj2 = new Point3D(3.0, 4.0,5.0);
printDistance(obj1.dist3D(obj2));
printDistance(obj1.dist2D(obj2));
}
}
class Point3D extends Point2D {
double z;
public double getZ() {
return z;
}
public Point3D(double x, double y, double z) {
super(x, y);
this.z = z;
}
public double dist3D(Point3D p) {
return (int) Math.ceil(Math.sqrt(Math.pow((p.getX() - x), 2) + Math.pow((p.getY() - y), 2) + +Math.pow((p.getZ() - z), 2)));
}
}
您可以执行以下操作:
public class Point2D {
double x;
double y;
public double getX() {
return x;
}
public double getY() {
return y;
}
public Point2D(double x, double y) {
this.x = x;
this.y = y;
}
public String dist2D(Point2D p) {
x = (int) Math.ceil(Math.sqrt(Math.pow((p.getX() - x), 2) + Math.pow((p.getY() - y), 2)));
return "2D Distance = "+x;
}
public static void printDistance(String d) {
System.out.println(d);
}
static public void main(String args[]) {
Point3D obj1 = new Point3D(1.0, 2.0, 3.0);
Point3D obj2 = new Point3D(3.0, 4.0,5.0);
printDistance(obj1.dist3D(obj2));
printDistance(obj1.dist2D(obj2));
}
}
class Point3D extends Point2D {
double z;
public double getZ() {
return z;
}
public Point3D(double x, double y, double z) {
super(x, y);
this.z = z;
}
public String dist3D(Point3D p) {
return "3D Distance = "+ Math.ceil(Math.sqrt(Math.pow((p.getX() - x), 2) + Math.pow((p.getY() - y), 2) + +Math.pow((p.getZ() - z), 2)));
}
}
输出:
3D Distance = 4.0
2D Distance = 3.0