//This program findsthe distance between 2 points using sqrt( (x2-x1)^2+(y2-y1)^
ID: 638498 • Letter: #
Question
//This program findsthe distance between 2 points using sqrt( (x2-x1)^2+(y2-y1)^2)
import java.util.Scanner;
import java.lang.Math;
public class DistanceBetweenTwoPoints
{
public static void main(String[] args)
{
int x1,x2,y1,y2;
double distanceX,distanceY,distanceX2,distanceY2,totalDistance, totalDistanceSquared;
Scanner scan = new Scanner (System.in);
System.out.print("Enter the value of X1 ");
x1=scan.nextInt();
System.out.print("Enter the value of X2 ");
x2=scan.nextInt();
System.out.print("Enter the value of Y1 ");
y1=scan.nextInt();
System.out.print("Enter the value of Y2 ");
y2=scan.nextInt();
distanceX=(x2-x1);
distanceY=(y2-y1);
distanceX2= Math.pow (distanceX,2);
distanceY2=Math.pow (distanceY,2);
totalDistance=(distanceX2+distanceY2);
totalDistanceSquared=Math.sqrt(totalDistance);
System.out.println("The distance between the points given is"+totalDistanceSquared);
}
}
****When I compile this I get 0 for the total everytime......Please Help!
Explanation / Answer
Here you go :)
I've removed unecessary things.
import java.util.Scanner;
import java.lang.Math;
public class DistanceBetweenTwoPoints
{
public static void main(String[] args)
{
int x1,x2,y1,y2;
double distanceX,distanceY,totalDistance;
Scanner scan = new Scanner (System.in);
System.out.print("Enter the value of X1 ");
x1=scan.nextInt();
System.out.print("Enter the value of X2 ");
x2=scan.nextInt();
System.out.print("Enter the value of Y1 ");
y1=scan.nextInt();
System.out.print("Enter the value of Y2 ");
y2=scan.nextInt();
distanceX=(x2-x1);
distanceY=(y2-y1);
distanceX= Math.pow (distanceX,2);
distanceY=Math.pow (distanceY,2);
totalDistance=Math.sqrt((distanceX2+distanceY2),2);
System.out.println("The distance between the points given is"+totalDistance);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.