224k views
1 vote
Determine the distance between point (x1, y1) and point (x2, y2), and assign the result to points Distance. The calculation is:

Distance = √((x^2-x^1)^2 + (y^2 - y^1)^2)

You may declare additional variables.

User KPandian
by
4.0k points

1 Answer

3 votes

Question:

Snapshot of the question has been attached to this response.

Answer:

import java.util.Scanner;

import java.lang.Math;

public class CoordinateGeometry{

public static void main(String []args){

double x1 = 1.0;

double y1 = 2.0;

double x2 = 1.0;

double y2 = 5.0;

double pointsDistance = 0.0;

//Declare a variable to hold the result of (x2 - x1)²

double x;

//Solve (x2 - x1)^2 and store result in the variable x

x = Math.pow(x2 - x1, 2);

//Declare a variable to hold the result of (y2 - y1)²

double y;

//Solve (y2 - y1)^2 and store result in the variable y

y = Math.pow(y2 - y1, 2);

//Now pointsDistance = SquareRootOf(x + y)

pointsDistance = Math.sqrt(x + y);

System.out.println("Points distance: ");

System.out.println(pointsDistance);

return;

}

}

Sample Output:

Points distance:

3.0

Step-by-step explanation:

The above code has been written in Java and it contains comments explaining important lines of the code. Please go through the comments.

The snapshots of the program and a sample output have been attached to this response.

Determine the distance between point (x1, y1) and point (x2, y2), and assign the result-example-1
Determine the distance between point (x1, y1) and point (x2, y2), and assign the result-example-2
Determine the distance between point (x1, y1) and point (x2, y2), and assign the result-example-3
User HCL
by
4.3k points