java - Calculating the distance between 2 Points in 2D and 3D -
i'm trying solve simple problem, have minor isses. hope can out beginner real quick ;)
i have 2 classes 'point' , 'point3d' that:
public class point { protected double x; protected double y; point(double xcoord, double ycoord){ this.x = xcoord; this.y = ycoord; } public double getx(){ return x; } public double gety(){ return y; } public static double distance(point a, point b) { double dx = a.x - b.x; double dy = a.y - b.y; return math.sqrt(dx * dx + dy * dy); } public static void main(string[] args) { point p1 = new point(2,2); point p2 = new point(5,6); system.out.println("distance between them " + point.distance(p1, p2)); } }
and this:
public class point3d extends point { protected double z; point3d(double x, double y, double zcoord){ super(x, y); this.z = zcoord; } public double getz(){ return z; } public static double distance(point p1, point p2){ double dx = p1.x - p2.x; double dy = p1.y - p2.y; double dz = p1.z - p2.z; return math.sqrt(dx * dx + dy * dy + dz *dz); } public static void main(string[] args) { point3d p1 = new point3d(-4,2,5); point3d p2 = new point3d(1,3,-2); system.out.println("distance between them " + point3d.distance(p1, p2)); } }
my problem following: if keep code this, eclipse saying 'z cannot resolved field' , possible solution should create in class 'point'. after doing class 'point3d' compiles doesn't calculate right answer..
greetings,
change signature of point3d distance method to:
public static double distance(point3d p1, point3d p2){
you had point
types arguments, don't have z
.
Comments
Post a Comment