The program below is the solution to Liang’s Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.19.
Question: Write a program that prompts the user to enter two points (x1, y1) and (x2, y2) and displays their distance between them.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 2.19 - Geometry: distance of two points<br /> *<br /> */ <br /><br />import java.util.Scanner;<br /><br />public class Ex02_19 {<br /><br /> public static void main(String[] args) {<br /> <br /> // Display Program Information<br /> System.out.println("This Program Calculates The Distance Between Two "<br /> + "Points In Geometry.n");<br /><br /> // create Scanner <br /> Scanner input = new Scanner(System.in);<br /><br /> // prompt user to enter details<br /> System.out.println("Enter x1 and y1: ");<br /> double x1 = input.nextDouble();<br /> double y1 = input.nextDouble();<br /> System.out.println("Enter x2 and y2: ");<br /> double x2 = input.nextDouble();<br /> double y2 = input.nextDouble();<br /><br /> // calculate distance<br /> double distance = Math.pow(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)), 0.5);<br /><br /> //display the result<br /> System.out.println("The distance between the two points is " + distance + "n");<br /> }<br />}<br /> |
Program output |
Click here to see other solutions to Introduction to Java Programming.