Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Distance between 2 points

1. ## Distance between 2 points

So I've been given a practice test for one of my CSC courses that is asking me this:

Provide the definition for the Java class Point so that it supports
the three Java statements that appear below. Your Point class may not extend any
other Java class. Recall that the distance between two points (x1,y1)and (x2,y2)is given by
sqrt((x1−x2)2+(y1−y2)2)

Point p1 = new Point(3,4);
Point p2 = new Point(16,-6);
double distance = p1.distanceTo(p2);

//Complete the Point class below
public class Point {

My problem is I'm having trouble figuring out how to set up the equation. here's my code so far:

```public class Point {
Point p1, p2;
int x, y;

public Point(int a, int b) {
this.x = a;
this.y = b;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public double distanceTo(Point p) {
//where my equation would go
return p;
}

public static void main(String[] args) {
Point p1 = new Point(3,4);
Point p2 = new Point(16,-6);
double distance = p1.distanceTo(p2);
System.out.print(distance); //just to test
}
}```

I was thinking something like this
`p = (p1.getX() - p2.getX())^2 + (p1.getY() - p2.getY())^2;`
but that throws the Type mismatch error (from Point to int) so I can't do it that way

any suggestions/help would be great. thanks

2. ## Re: Distance between 2 points

Perhaps you could post the actual code and compiler message. The line looks OK, but the devil with compiling lies in the detail.

-----

In the class definition x and y should be private because there's no reason for them not to be. And p1 and p2 should not be there at all since there is nothing (obvious) in the state of a point that consists of a pair of points.

3. ## Re: Distance between 2 points

that is my code. the only error I'm getting is a type mismatch where I would have been putting the equation that I posted above. I understand why its giving me that, but I'm not sure what the correct way to set it up is.

4. ## Re: Distance between 2 points

```public class Point {
Point p1, p2;
int x, y;

public Point(int a, int b) {
this.x = a;
this.y = b;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public double distanceTo(Point p) {
//where my equation would go
double lengthA = (double)(this.getX() - p.getX());
double lengthB = (double)(this.getY() - p.getY());
double distance = 0.0;

//FINISH FORMULA

return distance;
}

public static void main(String[] args) {
Point p1 = new Point(3,4);
Point p2 = new Point(16,-6);
double distance = p1.distanceTo(p2);
System.out.print(distance); //just to test
}
}```
I changed your distanceTo formula to access points how you looked like you wanted to.
since you called it with p1, when inside your method "this" refers to object p1.
since you pass it p2, use p2 by name to access its getter methods to.
they both have ints, and to keep precision you want to cast/change(?I forget if it is called casting with primitive types) it from into to double.

note: you will need to use the Math class to fix your formula for distance