In: Computer Science
**Java Programming Question**
A class that implements a data type, “Point,” which has the following constructor:
Point(double x, double y, double z)
A sample run would be as follows.
>java Point 2.1 3.0 3.5 4 5.2 3.5
The first point is (2.1,3.0,3.5)
The second point is (4.0,5.2,3.5)
Their Euclidean distance is 2.90
**Java Programming Question**
// do comment if any problem arises
//code
class Point {
double x, y, z;
Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return "(" + x + "," + y + "," + z + ")";
}
// this function finds sqareroot without using library function
public double sqrt(double input) {
double n;
// value to return
double ret = input / 2;
do {
n = ret;
ret = (n + (input / n)) / 2;
} while ((n - ret) != 0);
return ret;
}
// this function returns euclidian distance between this and q
double distanceto(Point q) {
double distance = (x - q.x) * ((x - q.x)) + (y - q.y) * ((y - q.y)) + (z - q.z) * ((z - q.z));
Point temp=new Point(0,0,0);
return temp.sqrt(distance);
}
public static void main(String[] args) {
double x, y, z;
x = Double.parseDouble(args[0]);
y = Double.parseDouble(args[1]);
z = Double.parseDouble(args[2]);
// create first point
Point first = new Point(x, y, z);
x = Double.parseDouble(args[3]);
y = Double.parseDouble(args[4]);
z = Double.parseDouble(args[5]);
// create second point
Point second = new Point(x, y, z);
// print first point
System.out.println("The first point is " + first);
// print second point
System.out.println("The second point is " + second);
System.out.print("Their Euclidian distance is ");
// print distance
System.out.printf("%.2f", first.distanceto(second));
}
}
Output:
The first point is (2.1,3.0,3.5) The second point is (4.0,5.2,3.5) Their Euclidian distance is 2.91