0

I am writing a method in the class "CartesianPoint" that finds the distance between two Cartesian points. Whenever I call this, the distance that is printed out is always zero, no matter which points I use. I believe that the new point I create to find the distance is somehow overriding my instance variables in point, but I don't know how to correctly code this.

Here is the CartesianPoint Class:

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

Here is the method call in my tester class:

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

And this is the output I am getting:

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

To clarify, Cartesian and Polar should be printing out the coordinates of 'p', not 'a' like they are doing right now. It seems like every new point created is overriding the coordinates of the last point.

Any help on this is greatly appreciated!

2 Answers2

2

Remove the static keyword before declaring CartesianPoint's properties:

private double x;
private double y;

Then you'll be sure you're accessing the right properties to each instance of the class (encapsulating the properties).

Also, the formula you're using to get the distance between the two points is incorrect, it should have been

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

As the formula is sqrt((xb - xa)2 + (yb - ya)2), the correct method would be:

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;
}
Grzegorz Górkiewicz
  • 4,496
  • 4
  • 22
  • 38
Vinicius Victor
  • 348
  • 1
  • 10
0

Hint: check the formula for calculating the distance (e.g. see here) and compare it with what you have written here:

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

Do you see the difference?

Hint #2: Minus???


When you write some code that doesn't work correctly, it pays to:

  • Read what you have written carefully
  • Check the requirements.
  • Check your domain knowledge: in this case "the math"
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216