# Thread: Distance Between Two Points

1. Member Join Date
Aug 2011
Posts
14
Rep Power
0

## 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:
Java 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:
Java 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.  Reply With Quote

2. ## Re: Distance Between Two Points

The getDistance method has four doubles as parameters yet you are passing a single Point when you call it.  Reply With Quote

3. Member Join Date
Sep 2011
Posts
11
Rep Power
0

## 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:
Java 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.
Last edited by Taikei_no_Yuurei; 09-08-2011 at 06:21 AM.  Reply With Quote

4. Member Join Date
Aug 2011
Posts
14
Rep Power
0

## 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.  Reply With Quote

5. ## 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?  Reply With Quote

6. Member Join Date
Sep 2011
Posts
11
Rep Power
0

## 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).  Reply With Quote

7. Member Join Date
Sep 2011
Posts
11
Rep Power
0

## Re: Distance Between Two Points 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.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•