如何仅使用两个变量找到两点之间的距离,然后存储所有点并获得形状?

huangapple go评论83阅读模式
英文:

How can I find the distance between two points using only two variables and after that store all points and obtain the shape?

问题

package com.company;
import java.lang.Math;

public class Point {
    //fields
    private int x;
    private int y;

    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    //method
    //getters
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
    //setters
    public void setX(int x) {
        this.x = x;
    }
    public void setY(int y) {
        this.y = y;
    }

    public int getPoint(){

    }

    //function distance
    public void distance() {
        //**here I need somehow use only two variables instead of four**
        double res = Math.sqrt((Math.pow(getX1(), 2) - Math.pow(getX2(), 2))
                + (Math.pow(getY1(), 2) - Math.pow(getY2(), 2)));
        System.out.println(res);
    }
}

注意:代码中的 getX1、getX2、getY1 和 getY2 需要进行适当修改,以便使用正确的属性和方法。

英文:

I tried to declare two variable x and y, then create constructor for them and getters with setters. So, for this I used class Distance, while for the obtaining shape I need another class.

package com.company;
import java.lang.Math;

public class Point {
    //fields
    private int x;
    private int y;

    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    //method
        //getters
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
        //setters
    public void setX(int x) {
        this.x = x;
    }
    public void setY(int y) {
        this.y = y;
    }

    public int getPoint(){
        
    }

    //function distance
    public void distance() {
        //**here I need somehow use only two variables instead of four**
        double res = Math.sqrt((Math.pow(getX1(), 2) - Math.pow(getX2(), 2))
                + (Math.pow(getY1(), 2) - Math.pow(getY2(), 2)));
        System.out.println(res);
    }
}

答案1

得分: 3

创建一个函数,接受类型为Point的对象。该函数返回原始点与传递点之间的距离。

public void distance(Point po) {
    // **在这里,我需要想办法只使用两个变量,而不是四个**
    double res = Math.sqrt(
                     Math.pow(getX() - po.getX(), 2) +
                     Math.pow(getY() - po.getY(), 2)
    );
    System.out.println(res);
}
同时你计算距离的函数是错误的
英文:

Create a function that accepts object of type Point. The function returns the distance between the original point and passed point

public void distance(Point po) {
    //**here I need somehow use only two variables instead of four**
    double res = Math.sqrt(
                     Math.pow(getX() - po.getX(), 2) +
                     Math.pow(getY() - po.getY(), 2)
    );
    System.out.println(res);
}

Also your function to calculate distance was wrong.

huangapple
  • 本文由 发表于 2020年4月11日 13:11:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61152401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定