2017-01-21 8 views
0

Ich schreibe eine Methode in der Klasse "CartesianPoint", die den Abstand zwischen zwei kartesischen Punkten findet. Wann immer ich das anrufe, ist der Ausdruck, der ausgedruckt wird, immer Null, egal welche Punkte ich verwende. Ich glaube, dass der neue Punkt, den ich erstelle, um die Entfernung zu finden, meine Instanzvariablen irgendwie überschreibt, aber ich weiß nicht, wie ich das richtig schreiben soll. HierJava: Abstand zwischen zwei Punkten immer Null

ist die CartesianPoint Klasse:

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

Hier ist der Aufruf der Methode in meiner Tester Klasse:

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

Und dies ist die Ausgabe erhalte ich:

Cartesian: (6.0, 7.0) 
Polar: (9.219544457292887, 0.8621700546672264) 
Distance: 0.0 

Um zu verdeutlichen, sollten Cartesian und Polar die Koordinaten von 'p', nicht 'a', wie sie es gerade tun, ausdrucken. Es scheint so, als ob jeder neue Punkt die Koordinaten des letzten Punktes überschreibt.

Jede Hilfe zu diesem wird sehr geschätzt!

+0

Entfernen Sie das statische Schlüsselwort von x und y –

Antwort

2

das static Schlüsselwort entfernen, bevor CartesianPoint Eigenschaften erklärt:

private double x; 
private double y; 

Dann werden Sie sicher, dass Sie die richtigen Eigenschaften zuzugreifst zu jeder Instanz der Klasse (Kapselung der Eigenschaften).

Auch die Formel, die Sie den Abstand zwischen den beiden Punkten zu bekommen verwenden ist falsch, sollte es

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

Wie lautet die Formel sqrt ((x b - x ein) + (y b - y a )), wäre die richtige Methode:

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

Hinweis: überprüfen Sie die Formel für die Entfernung der Berechnung (z here sehen) und vergleichen Sie es mit dem, was Sie hier geschrieben haben:

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

Sehen Sie den Unterschied?

Hinweis # 2: Minus ???


Wenn Sie einen Code schreiben, der nicht richtig funktioniert, lohnt es sich zu:

  • Lesen Sie, was Sie sorgfältig
  • die Anforderungen prüfen geschrieben haben.
  • Ihre Domain Wissen überprüfen: in diesem Fall „die Mathematik“
Verwandte Themen