【问题标题】:Calculate Euclidean Distance Between Two Points Using Constructor使用构造函数计算两点之间的欧几里得距离
【发布时间】: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


    【解决方案1】:

    有几点:

    首先,您的 Point 类应该只需要一组变量。只需制作一组并构造两个点对象。

    在 distanceTo() 方法中,通过执行 q.x1、q.y1 等访问其他点的坐标。

    在main方法中,距离应该是pointOne到pointTwo的距离的两倍。 这是对我有用的代码

    class Point {
        double x1;
        double y1;
        double z1;
        public Point(double x, double y, double z){
        //each point object only needs one x, y and z
            x1 = x;
            y1 = y;
            z1 = z;
        }
    
        public double distanceTo(Point pointTwo){
            return Math.sqrt(Math.pow(pointTwo.x1-x1, 2.0) + Math.pow(pointTwo.y1-y1, 2.0) + Math.pow(pointTwo.z1-z1, 2.0));
            //formula is sqrt((p2.x-p1)^2 + (p2.y-p1.y)^2 + (p2.z - p1.z)^2
        }
    
    
        public String toString() {
            return "(" + x1 + ", " + y1 + ", " + z1 + ")";
            //we don't need a diff set of variables here
        }
    
        public static void main (String[]args){
            //make two diff points with coords
            Point pointOne = new Point(2.1, 3.0, 3.5);
            Point pointTwo = new Point(4.0,5.2, 3.5);
            double distance = pointOne.distanceTo(pointTwo);
            //uses point two as the parameter (x2, y2, z2 in formula)
            System.out.println("The first point is " + "(" + pointOne + ")");
            System.out.println("The second point is " + "(" + pointTwo + ")");
            System.out.println("Their Euclidean distance is " + distance);
        }
    }
    

    【讨论】:

    • 当我尝试编译我的代码时仍然遇到很多错误。我刚开始学习创建自己的数据类型,所以我有点迷茫。如果你能分享一些代码,那就太棒了!
    • 好的,刚刚添加了我的代码,如果你需要解释,请告诉我。
    • 感谢您的帮助!在我看到这篇文章之前,我最终弄清楚了代码。我们最终得到了几乎相同的结果。您的提示无疑帮助我完成了我的计划,非常感谢!
    猜你喜欢
    • 2020-09-25
    • 1970-01-01
    • 2020-06-10
    • 2011-01-29
    • 2016-05-11
    • 2023-03-25
    • 2013-02-12
    • 2020-07-11
    • 1970-01-01
    相关资源
    最近更新 更多