# Distance Between Two Points

• 09-08-2011, 05:09 AM
HSKrustofsky
Distance Between Two Points
Been at this for a while, and I have no idea how to get to the end result. Here is what I have so far:
Point Class:
Code:

```public class Point {         private int x;         private int y;                 public Point() {         }                 public Point(int x, int y) {                 this();                 this.x = x;                 this.y = y;         }                 public int getX() {                 return x;         }                 public int getY() {                 return y;         }                 public void double getDistance(double x1, double x2, double y1, double y2) {                 double d = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));         } }```
TestPoint Class:
Code:

```public class PointTest {         public static void main(String[] args) {                 Point p1 = new Point(0,0);                 Point p2 = new Point(10,20);                 System.out.println("The distance between p1 and p2 is: " p1.getDistance(p2));         } }```
I know that printout is wrong, but I wouldn't know how to get the two points in there.

• 09-08-2011, 05:13 AM
Junky
Re: Distance Between Two Points
The getDistance method has four doubles as parameters yet you are passing a single Point when you call it.
• 09-08-2011, 05:30 AM
Taikei_no_Yuurei
Re: Distance Between Two Points
Lets see. Your first problem is that you have your getDistance returning both void and double. You want an answer, so remove the void part.

Second problem is that you don't actually return anything in your getDistance method.

Your third problem is that you need a '+' between elements in your println:
Code:

`System.out.println("The distance between p1 and p2 is: " + p1.getDistance(p2));`
Your fourth problem is that you are providing your getDistance method with a Point object as its augment instead of four doubles. There are several ways you could fix this. You could change the augments the getDistance method wants, or you can change the augments you are providing it. And there are again several ways to do that.
• 09-08-2011, 05:34 AM
HSKrustofsky
Re: Distance Between Two Points
Taikei_no_Yuurei,

I was about to post that I just did that. Thanks for your help. The main reason I wanted to post was to see if there is a difference between having p1.getDistance(...) and p2.getDistance(...)? It seems like it's bringing out the same output, but wasn't exactly sure.
• 09-08-2011, 05:35 AM
Junky
Re: Distance Between Two Points
I have to question why would you retrieve the x and y values from the Point object only to pass them back into the object as parameters?
• 09-08-2011, 05:39 AM
Taikei_no_Yuurei
Re: Distance Between Two Points
It isn't exactly the most finessed way to write the program, but it works. Ideally you would have the getDistance method simply use the numbers for its own point and only require you to pass on the two numbers from the other point (or the point itself and let the method do the extraction).
• 09-08-2011, 06:23 AM
Taikei_no_Yuurei
Re: Distance Between Two Points
Quote:

Originally Posted by HSKrustofsky
Taikei_no_Yuurei,

I was about to post that I just did that. Thanks for your help. The main reason I wanted to post was to see if there is a difference between having p1.getDistance(...) and p2.getDistance(...)? It seems like it's bringing out the same output, but wasn't exactly sure.

No, the way you have things set up using getDistance from p1 and p2 are identical.