Develop proper algorithm for solving a given program. The instructions in the al
ID: 3709718 • Letter: D
Question
Develop proper algorithm for solving a given program. The instructions in the algorithm will serve as comments in your program
Turn the algorithm into code
Computing Distances
Given two points, the formula for computing the distance is
Write a program that prompts the user to enter two points and computes the distance between them. Note that each point is characterized by two float values, representing the x-axis and y-axis.
Sample Run
Enter x1 and y1 for Point 1: 1.5, -3.4
Enter x2 and y2 for Point 2: 4, 5
The distance between (1.5, -3.4) and (4, 5) is 8.8
Explanation / Answer
DistanceTest.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class DistanceTest {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("0.00");
Scanner scan = new Scanner(System.in);
System.out.println("Enter x1 and y1 for Point 1:");
float x1 = scan.nextFloat();
float y1 = scan.nextFloat();
System.out.println("Enter x2 and y2 for Point 2:");
float x2 = scan.nextFloat();
float y2 = scan.nextFloat();
float distance = (float)Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
System.out.println("The distance between ("+x1+", "+y1+") and ("+x2+", "+y2+") is "+df.format(distance));
}
}
Output:
Enter x1 and y1 for Point 1:
1.5 -3.4
Enter x2 and y2 for Point 2:
4
5
The distance between (1.5, -3.4) and (4.0, 5.0) is 8.76
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.