Write a program that asks the user to enter the coordinates of two points (x_1,
ID: 672183 • Letter: W
Question
Write a program that asks the user to enter the coordinates of two points (x_1, y_2) and (x_2, y_2), calculates the distance between the points using the formula = square root (x_2 - x_1)^2 + (y_2 - y_1)^2, and displays the distance. The display produced by your program should be: The distance between the points (xxx.xx, xxx.xx) and (xxx.xx, xxx.xx) is xxx.xxxx where xxx. xx denotes that the displayed value should be placed in a field wide enough for three places to the left of the decimal point and two places to the right of it; xxx. xxxx denotes that the displayed value should be placed in a field wide enough for three places to the left of the decimal point and four places to the right of itExplanation / Answer
import java.util.Scanner;
//distance = square root of [(x2-x1)squared + (y2-y1)squared]
public class PointDistance
{
public static void main(String[] args)
{
double distance(double x1, double y1, double x2, double y2)
{
return math.sqrt((x1-x2)(x1-x2) + (y1-y2)(y1-y2));
}
double x1, x2, y, y2;
double distance;
Scanner scan = new Scanner (System.in);
System.out.println("Enter the x coordinate for point 1: ");
x1 = scan.nextDouble();
System.out.println("Enter the y coordinate for point 1: ");
y1 = scan.nextDouble();
System.out.println("Enter the x coordinate for point 2: ");
x2 = scan.nextDouble();
System.out.println("Enter the y coordinate for point 2: ");
y2 = scan.nextDouble();
distance = distance(x1,y1,x2,y2);
System.out.println("The distance between the two points is " + distance + " .");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.