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:

Code Java:

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

Code :

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

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.

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.

Re: Distance between 2 points

Code :

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 :D