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



我试图声明两个变量x和y,然后为它们创建构造函数,并使用setter创建getter。所以,对于这个,我使用了类Distance,而对于获得形状,我需要另一个类。

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);
}
}
创建一个接受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);
}

此外,你计算距离的函数也是错误的。

最新更新