두 직교 좌표 점 사이의 거리를 찾는 "CartesianPoint"클래스의 메서드를 작성하고 있습니다. 내가 이것을 호출 할 때마다, 내가 사용하는 어떤 점에 관계없이 인쇄 된 거리는 항상 0이다. 저는 거리를 찾기 위해 만드는 새로운 포인트가 어떻게 든 내 인스턴스 변수를 무시한다고 생각합니다. 그러나 정확하게 이것을 코드화하는 법을 모르겠습니다. 여기 Java : 두 점 사이의 거리가 항상 0을 반환합니다.
public class CartesianPoint implements Point {
private static double x;
private static double y;
public CartesianPoint(double xCoord, double yCoord){
x = xCoord;
y = yCoord;
}
public double xCoordinate(){
return x;
}
public double yCoordinate(){
return y;
}
public double radius(){
double radius = Math.sqrt(Math.pow(xCoordinate(), 2)+Math.pow(yCoordinate(), 2));
return radius;
}
public double angle(){
double angle = Math.acos(xCoordinate()/radius());
return angle;
}
public double distanceFrom(Point other){
//System.out.println("x coordinate of this: " + xCoordinate());
//System.out.println("x coordinate of other: " + other.xCoordinate());
double xDistance = x - other.xCoordinate();
double yDistance = y - other.yCoordinate();
double distance = Math.sqrt(Math.pow(xDistance, 2) - Math.pow(yDistance, 2));
return distance;
}
//not currently being used
public Point rotate90(){
Point rotatedPoint = new CartesianPoint(0, 0);
return rotatedPoint;
}
}
내 테스터 클래스에서 메서드 호출입니다 : 여기
는 CartesianPoint 클래스입니다
public class tester{
public static void main(String[] args){
Point p = new CartesianPoint(3, 4);
Point a = new CartesianPoint(6, 7);
System.out.println("Cartesian: (" + p.xCoordinate() + ", " + p.yCoordinate() + ")");
System.out.println("Polar: (" + p.radius() + ", " + p.angle() + ")");
System.out.println("Distance: " + p.distanceFrom(a));
}
}
을 그리고 이것은 내가 무엇입니까 출력입니다 :
Cartesian: (6.0, 7.0)
Polar: (9.219544457292887, 0.8621700546672264)
Distance: 0.0
명확히하기 위해, 직교 좌표계와 극 좌표계는 '지금'하고있는 것처럼 'a'좌표가 아닌 'p'좌표를 인쇄해야합니다. 생성 된 모든 새로운 점이 마지막 점의 좌표를 무시하는 것처럼 보입니다.
이 문제에 대한 도움을 주시면 대단히 감사하겠습니다.
x 및 y에서 static 키워드를 제거하십시오. –