Write the program to compute the distance between two points. Recall that the di
ID: 3869756 • Letter: W
Question
Write the program to compute the distance between two points. Recall that the distance between the two points (x1, y1) and (x2, y2) is computed by taking the square root of the quantity (x1 - x2)^2 + (y1 - y2)^2. 1. Declare variables for two points coordinates as double 2. Declare variable for the distance 3. Create Scanner object 4. Prompt first point and assign it to the variable using nextDouble() 5. Prompt second point and assign it to the variable using nextDouble() 6. Compute the distance 7. Print out the answer
Explanation / Answer
Code:
import java.util.*;
class DistanceBwPoint
{
public static void main(String arg[])
{
double x1,x2,y1,y2; //declaring point co-ordinates variables
double distance; //declaring distance
Scanner sc=new Scanner(System.in); //creating scanner object
System.out.println("enter x1 for point-1:");
x1=sc.nextDouble();
System.out.println("enter y1 for point-1:");
y1=sc.nextDouble();
System.out.println("enter x2 for point-2:");
x2=sc.nextDouble();
System.out.println("enter y2 for point-2:");
y2=sc.nextDouble();
distance=Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); // finding distance
System.out.println("distance between"+"("+x1+","+y1+"),"+"("+x2+","+y2+") is : "+distance); //printing answer
}
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.