英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论