2017-01-21 1 views
0

두 직교 좌표 점 사이의 거리를 찾는 "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'좌표를 인쇄해야합니다. 생성 된 모든 새로운 점이 마지막 점의 좌표를 무시하는 것처럼 보입니다.

이 문제에 대한 도움을 주시면 대단히 감사하겠습니다.

+0

x 및 y에서 static 키워드를 제거하십시오. –

답변

2

이 CartesianPoint의 속성 선언하기 전에 static 키워드를 제거

private double x; 
private double y; 

을 그럼 당신은 당신이 바로 그 속성에 액세스하고 있는지 알 수있을 것입니다 클래스의 각 인스턴스 (속성을 캡슐화). X A - 수식 SQRT ((X B을이기

또한, 당신은 두 점 사이의 거리를 얻기 위해 사용하는 공식이 잘못, 그것은

double distance = Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2)); 

를 있었어야) 2 + (Y B - Y A는) 2)
올바른 방법이 될 것이다 :

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; 
} 
0

힌트 : 거리를 계산하는 공식을 확인 (예를 들어, here 참조) 여기에 쓴 것과 비교 :

Math.sqrt(Math.pow(xDistance, 2) - Math.pow(yDistance, 2)); 

당신이 차이를 볼 수 있습니까?

힌트 # 2 : 마이너스 ???

  • 당신이주의 깊게
  • 기록 요구 사항을 확인 한 내용을 읽기 : 당신이 제대로 작동하지 않는 코드를 작성하는 경우에


    , 그것은 지불한다.

  • 도메인 지식을 확인 : "수학"이 경우
관련 문제