SlideShare a Scribd company logo
1 of 2
Download to read offline
Write a Java program that calculates the distance between two points when a user types the
coordinates.
Solution
DistBetPoints.java
import java.util.Scanner;//package for keyboard inputting
//distance = square root of [(a2-a1)squared + (b2-b1)squared]
public class DistBetPoints
{//main class
static double distance(double a1, double b1, double a2, double b2)
{
return Math.sqrt((a2-a1)*(a2-a1) + (b2-b1)*(b2-b1));
}
public static void main(String[] args)
{ //main function
double a2, a1, b2, b1;
double distance;
Scanner scan = new Scanner (System.in);
System.out.println("Enter the x coordinate for point 1: ");
a1 = scan.nextDouble();//keyboard inputting
System.out.println("Enter the y coordinate for point 1: ");
b1 = scan.nextDouble();//keyboard inputting
System.out.println("Enter the x coordinate for point 2: ");
a2 = scan.nextDouble();//keyboard inputting
System.out.println("Enter the y coordinate for point 2: ");
b2 = scan.nextDouble();//keyboard inputting
distance = distance(a1,b1,a2,b2);//calling function
System.out.println("The distance between the two points is " + distance + " .");
}
}
output
Enter the x coordinate for point 1:
0
Enter the y coordinate for point 1:
0
Enter the x coordinate for point 2:
2
Enter the y coordinate for point 2:
2
The distance between the two points is 2.8284271247461903 .

More Related Content

Similar to Write a Java program that calculates the distance between two points.pdf

Similar to Write a Java program that calculates the distance between two points.pdf (9)

Hems
HemsHems
Hems
 
Programs of java
Programs of javaPrograms of java
Programs of java
 
The sample program for above series in JAVA will be like belowimpo.pdf
The sample program for above series in JAVA will be like belowimpo.pdfThe sample program for above series in JAVA will be like belowimpo.pdf
The sample program for above series in JAVA will be like belowimpo.pdf
 
The sample program for above series in JAVA will be like belowimpo.pdf
The sample program for above series in JAVA will be like belowimpo.pdfThe sample program for above series in JAVA will be like belowimpo.pdf
The sample program for above series in JAVA will be like belowimpo.pdf
 
Image Recognition with Neural Network
Image Recognition with Neural NetworkImage Recognition with Neural Network
Image Recognition with Neural Network
 
programming for Calculator in java
programming for Calculator in javaprogramming for Calculator in java
programming for Calculator in java
 
Applets
AppletsApplets
Applets
 
Java Programming Assignment
Java Programming AssignmentJava Programming Assignment
Java Programming Assignment
 
JAVASQL Hello can I please get an OUTPUT screen shot of the.pdf
JAVASQL Hello can I please get an OUTPUT screen shot of the.pdfJAVASQL Hello can I please get an OUTPUT screen shot of the.pdf
JAVASQL Hello can I please get an OUTPUT screen shot of the.pdf
 

More from poblettesedanoree498

Give examples of two zoonotic diseases. Explain how these zoonotic d.pdf
Give examples of two zoonotic diseases. Explain how these zoonotic d.pdfGive examples of two zoonotic diseases. Explain how these zoonotic d.pdf
Give examples of two zoonotic diseases. Explain how these zoonotic d.pdf
poblettesedanoree498
 
Describe different flow control valves with figures. Include referenc.pdf
Describe different flow control valves with figures. Include referenc.pdfDescribe different flow control valves with figures. Include referenc.pdf
Describe different flow control valves with figures. Include referenc.pdf
poblettesedanoree498
 
Compare and contrast transmit and receive diversity. Describe how ea.pdf
Compare and contrast transmit and receive diversity. Describe how ea.pdfCompare and contrast transmit and receive diversity. Describe how ea.pdf
Compare and contrast transmit and receive diversity. Describe how ea.pdf
poblettesedanoree498
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
poblettesedanoree498
 
–PLS write program in c++ thanxLinked List Sorting and Reversing.pdf
–PLS write program in c++ thanxLinked List Sorting and Reversing.pdf–PLS write program in c++ thanxLinked List Sorting and Reversing.pdf
–PLS write program in c++ thanxLinked List Sorting and Reversing.pdf
poblettesedanoree498
 
The systemic circuit delivers oxygenated blood to and returns blood t.pdf
The systemic circuit delivers oxygenated blood to and returns blood t.pdfThe systemic circuit delivers oxygenated blood to and returns blood t.pdf
The systemic circuit delivers oxygenated blood to and returns blood t.pdf
poblettesedanoree498
 

More from poblettesedanoree498 (20)

Give examples of two zoonotic diseases. Explain how these zoonotic d.pdf
Give examples of two zoonotic diseases. Explain how these zoonotic d.pdfGive examples of two zoonotic diseases. Explain how these zoonotic d.pdf
Give examples of two zoonotic diseases. Explain how these zoonotic d.pdf
 
Create the variables, and methods needed for this classA DicePlay.pdf
Create the variables, and methods needed for this classA DicePlay.pdfCreate the variables, and methods needed for this classA DicePlay.pdf
Create the variables, and methods needed for this classA DicePlay.pdf
 
Describe different flow control valves with figures. Include referenc.pdf
Describe different flow control valves with figures. Include referenc.pdfDescribe different flow control valves with figures. Include referenc.pdf
Describe different flow control valves with figures. Include referenc.pdf
 
Consider a sample with data values of 10, 20, 12, 17, and 16.Compute.pdf
Consider a sample with data values of 10, 20, 12, 17, and 16.Compute.pdfConsider a sample with data values of 10, 20, 12, 17, and 16.Compute.pdf
Consider a sample with data values of 10, 20, 12, 17, and 16.Compute.pdf
 
Compare and contrast transmit and receive diversity. Describe how ea.pdf
Compare and contrast transmit and receive diversity. Describe how ea.pdfCompare and contrast transmit and receive diversity. Describe how ea.pdf
Compare and contrast transmit and receive diversity. Describe how ea.pdf
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
 
Blood in the femoral artery will flow into what artery next Bl.pdf
Blood in the femoral artery will flow into what artery next Bl.pdfBlood in the femoral artery will flow into what artery next Bl.pdf
Blood in the femoral artery will flow into what artery next Bl.pdf
 
Assume that 96 of ticket holders show up for an airline flight. If .pdf
Assume that 96 of ticket holders show up for an airline flight. If .pdfAssume that 96 of ticket holders show up for an airline flight. If .pdf
Assume that 96 of ticket holders show up for an airline flight. If .pdf
 
Although some male crab spiders find and mate with adult female spide.pdf
Although some male crab spiders find and mate with adult female spide.pdfAlthough some male crab spiders find and mate with adult female spide.pdf
Although some male crab spiders find and mate with adult female spide.pdf
 
7. At October 1, Smithson Enterprises reported owners equity of $.pdf
7. At October 1, Smithson Enterprises reported owners equity of $.pdf7. At October 1, Smithson Enterprises reported owners equity of $.pdf
7. At October 1, Smithson Enterprises reported owners equity of $.pdf
 
5. A repeated measures analysis of variane shows, F(4,20)=14.56. How.pdf
5. A repeated measures analysis of variane shows, F(4,20)=14.56. How.pdf5. A repeated measures analysis of variane shows, F(4,20)=14.56. How.pdf
5. A repeated measures analysis of variane shows, F(4,20)=14.56. How.pdf
 
–PLS write program in c++ thanxLinked List Sorting and Reversing.pdf
–PLS write program in c++ thanxLinked List Sorting and Reversing.pdf–PLS write program in c++ thanxLinked List Sorting and Reversing.pdf
–PLS write program in c++ thanxLinked List Sorting and Reversing.pdf
 
Which of the following light waves has the most amount of energyH.pdf
Which of the following light waves has the most amount of energyH.pdfWhich of the following light waves has the most amount of energyH.pdf
Which of the following light waves has the most amount of energyH.pdf
 
What is the wobble position What is the consequence of its pres.pdf
What is the wobble position What is the consequence of its pres.pdfWhat is the wobble position What is the consequence of its pres.pdf
What is the wobble position What is the consequence of its pres.pdf
 
What is an advantage of having homologous chromosomes (compared to h.pdf
What is an advantage of having homologous chromosomes (compared to h.pdfWhat is an advantage of having homologous chromosomes (compared to h.pdf
What is an advantage of having homologous chromosomes (compared to h.pdf
 
What are oncogenic viruses Which of the following groups of viruses.pdf
What are oncogenic viruses  Which of the following groups of viruses.pdfWhat are oncogenic viruses  Which of the following groups of viruses.pdf
What are oncogenic viruses Which of the following groups of viruses.pdf
 
Unlike the biological species concept, the morphospecies concept rel.pdf
Unlike the biological species concept, the morphospecies concept rel.pdfUnlike the biological species concept, the morphospecies concept rel.pdf
Unlike the biological species concept, the morphospecies concept rel.pdf
 
1.) Which macronutrient do we consume in large amounts, but which we.pdf
1.) Which macronutrient do we consume in large amounts, but which we.pdf1.) Which macronutrient do we consume in large amounts, but which we.pdf
1.) Which macronutrient do we consume in large amounts, but which we.pdf
 
11.1.2 Entered Answer Preview 28 28 128 128 At least one of the an.pdf
11.1.2 Entered Answer Preview 28 28 128 128 At least one of the an.pdf11.1.2 Entered Answer Preview 28 28 128 128 At least one of the an.pdf
11.1.2 Entered Answer Preview 28 28 128 128 At least one of the an.pdf
 
The systemic circuit delivers oxygenated blood to and returns blood t.pdf
The systemic circuit delivers oxygenated blood to and returns blood t.pdfThe systemic circuit delivers oxygenated blood to and returns blood t.pdf
The systemic circuit delivers oxygenated blood to and returns blood t.pdf
 

Recently uploaded

Recently uploaded (20)

ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 

Write a Java program that calculates the distance between two points.pdf

  • 1. Write a Java program that calculates the distance between two points when a user types the coordinates. Solution DistBetPoints.java import java.util.Scanner;//package for keyboard inputting //distance = square root of [(a2-a1)squared + (b2-b1)squared] public class DistBetPoints {//main class static double distance(double a1, double b1, double a2, double b2) { return Math.sqrt((a2-a1)*(a2-a1) + (b2-b1)*(b2-b1)); } public static void main(String[] args) { //main function double a2, a1, b2, b1; double distance; Scanner scan = new Scanner (System.in); System.out.println("Enter the x coordinate for point 1: "); a1 = scan.nextDouble();//keyboard inputting System.out.println("Enter the y coordinate for point 1: "); b1 = scan.nextDouble();//keyboard inputting System.out.println("Enter the x coordinate for point 2: "); a2 = scan.nextDouble();//keyboard inputting
  • 2. System.out.println("Enter the y coordinate for point 2: "); b2 = scan.nextDouble();//keyboard inputting distance = distance(a1,b1,a2,b2);//calling function System.out.println("The distance between the two points is " + distance + " ."); } } output Enter the x coordinate for point 1: 0 Enter the y coordinate for point 1: 0 Enter the x coordinate for point 2: 2 Enter the y coordinate for point 2: 2 The distance between the two points is 2.8284271247461903 .