Programming Exercise #13: In a 3D space, the location of a given point can be expressed as (x, y, z) by the so-called “Cartesian coordinate system”. The distance between any two points, (x1, yl, zl) and (x2, y2, z2), in that 3D space is calculated by:

34 0

Get full Expert solution in seconds

\$1.97 ONLY

import javax.swing.JOptionPane;

// Point3D class

class Point3D{

private int x, y, z;

// No argument constructor

Point3D(){

this.x = 0;

this.y = 0;

this.z = 0;

}

// all argument constructor

Point3D(int x, int y, int z){

this.x = x;

this.y = y;

this.z = z;

}

// calculate distance from point (0,0,0) to point given

public double distance(Point3D point3d){

int x1 = point3d.x;

int y1 = point3d.y;

int z1 = point3d.z;

return Math.sqrt(x1*x1 + y1*y1 + z1*z1);

}

// calculates distance between point 1 and point 2

public double distance(Point3D point1, Point3D point2){

int x1 = point1.x;

int y1 = point1.y;

int z1 = point1.z;

int x2 = point2.x;

int y2 = point2.y;

int z2 = point2.z;

return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2));

}

}

// main class or driver class

class MyProg13{

public static void main(String[] args){

Point3D p1 = new Point3D();

Point3D p2 = new Point3D(126, 44, 39);

Point3D p3 = new Point3D(25, 219, 74);

// input dialog boxes

String x = JOptionPane.showInputDialog(“Enter x: “);

String y = JOptionPane.showInputDialog(“Enter y: “);

String z = JOptionPane.showInputDialog(“Enter z: “);

// create point point p4

Point3D p4 = new Point3D(Integer.parseInt(x),Integer.parseInt(y),Integer.parseInt(z));

// saving all the result/message in string result

String result = “p1 to (31, 127, 86): “+ p1.distance(p1, new Point3D(31, 127, 86))+”\n”+

“p1 to p2: “+ p1.distance(p2)+”\n”+

“p2 to p3: “+ p1.distance(p2, p3)+”\n”+

“p3 to p4: “+ p1.distance(p3, p4);

// message dialog box

JOptionPane.showMessageDialog(null,result);

// exit the system

System.exit(0);

}

}