Java program to calculate the distance between two points. The code has been written in five different formats using standard values, taking inputs through scanner class, command line arguments, while loop and, do while loop, creating a separate class. If you nay doubts related to the information that we shared do leave a comment here at the end of the post.
How to calculate the distance between two points?
The distance between two points formula derived from the Pythagorean Theorem.
What is the formula to find the distance between two points?
A: Here is the formula to find the distance between two points:
To find the distance between two points (x1,y1) and (x2,y2), all that you need to do is use the coordinates of these ordered pairs and apply the formula pictured below.
1.Java Program using standard values
[wp_ad_camp_3]
There you go formula-based program with sample output.
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.lang.Math.*; class DistanceBwPoint { public static void main(String arg[]) { int x1,x2,y1,y2; double dis; x1=1;y1=1;x2=4;y2=4; dis=Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); System.out.println("distancebetween"+"("+x1+","+y1+"),"+"("+x2+","+y2+")===>"+dis); } } |
output:
1 |
distancebetween(1,1),(4,4)===>4.242640687119285 |
2. Taking inputs through scanner class
Sample code #2: Taking inputs through scanner class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.Scanner; class DistanceBwPoint { public static void main(String arg[]) { int x1,x2,y1,y2; double dis; Scanner sc=new Scanner(System.in); System.out.println("enter x1 point"); x1=sc.nextInt(); System.out.println("enter y1 point"); y1=sc.nextInt(); System.out.println("enter x2point"); x2=sc.nextInt(); System.out.println("enter y2 point"); y2=sc.nextInt(); dis=Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); System.out.println("distancebetween"+"("+x1+","+y1+"),"+"("+x2+","+y2+")===>"+dis); } } |
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
Output:1 C:\Users\goutham\Desktop\E>java DistanceBwPoint enter x1 point 1 enter y1 point 2 enter x2point 1 enter y2 point 2 distancebetween(1,2),(1,2)===>0.0 Output:2 C:\Users\goutham\Desktop\E>java DistanceBwPoint enter x1 point 4 enter y1 point 4 enter x2point 1 enter y2 point 1 distancebetween(4,4),(1,1)===>4.242640687119285 Output:3 C:\Users\goutham\Desktop\E>java DistanceBwPoint enter x1 point 5 enter y1 point 3 enter x2point 2 enter y2 point 3 distancebetween(5,3),(2,3)===>3.0 |
3. Using command line arguments
[wp_ad_camp_3]
Taking inputs through command line arguments. Do check out the complete guide on command line arguments here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Scanner; class DistanceBwPoint { public static void main(String args[]) { long x1,x2,y1,y2; double dis; x1=Long.parseLong(args[0]); y1=Long.parseLong(args[1]); x2=Long.parseLong(args[2]); y2=Long.parseLong(args[3]); dis=Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); System.out.println("distancebetween"+"("+x1+","+y1+"),"+"("+x2+","+y2+")===>"+dis); } } |
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Output:1 C:\Users\goutham\Desktop\E>javac DistanceBwPoint.java C:\Users\goutham\Desktop\E>java DistanceBwPoint 1 1 1 1 distancebetween(1,1),(1,1)===>0.0 Output:2 C:\Users\goutham\Desktop\E>java DistanceBwPoint 11 5 12 5 distancebetween(11,5),(12,5)===>1.0 Output:3 C:\Users\goutham\Desktop\E>java DistanceBwPoint 11 11 5 5 distancebetween(11,11),(5,5)===>8.48528137423857 Output:4 C:\Users\goutham\Desktop\E>java DistanceBwPoint 100 4 200 5 distancebetween(100,4),(200,5)===>100.00499987500625 |
4. User Define Method
By using a user-defined method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.util.Scanner; class DistanceBwPoint { public static void main(String args[]) { int x1,x2,y1,y2; double dis; Scanner sc=new Scanner(System.in); System.out.println("enter x1 point"); x1=sc.nextInt(); System.out.println("enter y1 point"); y1=sc.nextInt(); System.out.println("enter x2point"); x2=sc.nextInt(); System.out.println("enter y2 point"); y2=sc.nextInt(); dis=DistanceBwPoint.calDis(x1,y1,x2,y2); System.out.println("distancebetween"+"("+x1+","+y1+"),"+"("+x2+","+y2+")===>"+dis); } static double calDis(int x1,int y1,int x2,int y2) { return (Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))); } } |
output:
1 2 3 4 5 6 7 8 9 |
enter x1 point 1 enter y1 point 2 enter x2point 3 enter y2 point 4 distancebetween(1,2),(3,4)===>2.8284271247461903 |
5. Creating a separate class
Through creating a separate class( ) and taking inputs through scanner class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.util.Scanner; class CalDis { double dis; CalDis(int x1,int y1,int x2,int y2) { dis=(Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))); } } class DistanceBwPoint { public static void main(String args[]) { int x1,x2,y1,y2; Scanner sc=new Scanner(System.in); System.out.println("enter x1 point"); x1=sc.nextInt(); System.out.println("enter y1 point"); y1=sc.nextInt(); System.out.println("enter x2point"); x2=sc.nextInt(); System.out.println("enter y2 point"); y2=sc.nextInt(); CalDis cd=new CalDis(x1,y1,x2,y2); System.out.println("distancebetween"+"("+x1+","+y1+"),"+"("+x2+","+y2+")===>"+cd.dis); } } |
output:
1 2 3 4 5 6 7 8 9 |
enter x1 point 11 enter y1 point 5 enter x2point 6 enter y2 point 5 distancebetween(11,5),(6,5)===>5.0 |