b

b

Saturday, February 13, 2016

Distance

Distance.java to compute the distance between two points:

//*************************************************************************
//  Distance.java
//  Computes the distance between two points
//*************************************************************************

import java.util.Scanner;

public class Distance
{

public static void main(String[] args) {

double x1, y1, x2, y2;

Scanner scan = new Scanner(System.in);

System.out.print ("Enter the coordinates of the first point " +
"(put a space between them): ");

x1 = scan.nextDouble();
y1 = scan.nextDouble();

System.out.print ("Enter the coordinates of the second point: ");
x2 = scan.nextDouble();
y2 = scan.nextDouble();

double dist;

dist = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));

System.out.print("Distance between two point is: ");
System.out.println(dist);
}
}

No comments:

Post a Comment