Solved programming problem

Find Distance Between Two Point in Java.

Source code

Main Method:

 

package pointdirection;

import java.util.Scanner;

/**

 *

 * @author Sabit

 */

public class TestDirection {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args)

    {

       Point a = new Point();  

       Point b = new Point();  

       Scanner S = new Scanner(System.in);

       double x,y,z;

      

        System.out.println("Insert The 1st Point Value: ");

        System.out.println("X: ");

        x = S.nextDouble();

        System.out.println("Y: ");

        y = S.nextDouble();

        System.out.println("Z: ");

        z = S.nextDouble();

        

        a = new Point (x,y,z);

        

        System.out.println("Insert The 2nd Point Value: ");

        System.out.println("X: ");

        x = S.nextDouble();

        System.out.println("Y: ");

        y = S.nextDouble();

        System.out.println("Z: ");

        z = S.nextDouble();

        b = new Point (x,y,z);

                  

       Utility u = new Utility();

       System.out.println("Utility: "+ u.getDistance(a, b));

       

    }

    

}

 

 

Point Class: 

 

package pointdirection;

 

 

 

 

 

/**

 

 *

 

 * @author Sabit

 */

public class Point {

 

    public double x, y, z;

 

    public Point() {

        x = 0;

        y = 0;

        z = 0;

    }

 

    public Point(double x, double y, double z) {

        this.x = x;

        this.y = y;

        this.z = z;

    }

 

    public Point(Point p) {

        this.x = p.x;

        this.y = p.y;

        this.z = p.z;

    }

 

    public void setPointX(double x) {

        this.x = x;

    }

 

    public void setPointY(double y) {

        this.y = y;

    }

 

    public void setPointZ(double z) {

        this.z = z;

    }

 

    public double getPointX() {

        return x;

    }

 

    public double getPointY() {

        return y;

    }

 

    public double getPointZ() {

        return z;

    }

}

 

Utility Class:

 

 

package pointdirection;

 

import static java.lang.Math.pow;

 

/**

 *

 * @author Sabit

 */

public class Utility 

{

    public double getDistance(Point a, Point b)

    {

       double dist;

         

       double x1 = a.x,x2 =b.x;

       double A = pow(x2-x1,2);

             

       double y1 = a.y,y2 =b.y;

       double B = pow(y2-y1,2);

       

       double z1 = a.z,z2 =b.z;

       double C = pow(z2-z1,2);

        

        

        dist = Math.sqrt(A+B+C);

       

        return dist;

    }

}

Output

Insert the first point value:

X

1.3

Y

3.6

Z

3.9

Insert the second point value:

X

5.3

Y

3.6

Z

3.0
Utility: 4.1

Join the discussion

Add your comment