Computing Distance java program Need help! No time to do this right now and its
ID: 3557578 • Letter: C
Question
Computing Distance java program
Need help! No time to do this right now and its due today
Computing Distance
Write a 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. You need to use method(s) of the Math class to compute the distance and then use DecimalFormat class to format the fractional part of the output rounded to 3 digits. Test your program using the following data: The distance between the points (3.5, 17) and (8.5, 10) is 8.602; the distance between (-33,49) and (-9, -15) is 68.352
The general outline of your main function would be as follows:
Explanation / Answer
/*****name file as Solution.java****/
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;
class Points{
double X,Y;
Points(){
X=0;
Y=0;
}
Points(double X1,double Y1){
X=X1;
Y=Y1;
}
}
class Solution {
public static double Distance(Points P1,Points P2){
double result=Math.sqrt((P1.X-P2.X)*(P1.X-P2.X)+(P1.Y-P2.Y)*(P1.Y-P2.Y));
return result;
}
public static void main(String[] args) {
double X1,Y1;
DecimalFormat df=new DecimalFormat("0.000");
Scanner in=new Scanner(System.in);
System.out.println("Enter First Point (X Y separated by space)");
X1=in.nextDouble();
Y1=in.nextDouble();
in.nextLine();
Points P1=new Points(X1,Y1);
System.out.println("Enter Second Point (X Y separated by space)");
X1=in.nextDouble();
Y1=in.nextDouble();
in.nextLine();
Points P2=new Points(X1,Y1);
System.out.print("Distance between two Points is :");
System.out.print(df.format(Distance(P1,P2))+" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.