Question: Write a program that asks the user to input the (x,y) coordinates of 2 points, and then calculates and shows the distance between those two
Write a program that asks the user to input the (x,y) coordinates of 2 points, and then calculates and shows the distance between those two points. The program should execute according to the following sample I/O:
Enter x coordinate for point 1: user inputs 2.0 Enter y coordinate for point 1: user inputs 1.0 Enter x coordinate for point 2: user inputs 5.0 Enter y coordinate for point 2: user inputs 5.0 The distance between the two points is 5.0Hint: Use variable names x1,y1 for the coordinates of point 1, and use variable names x2,y2 for the coordinates of point 2. The formula for the distance between two points is: (x2-x1)2+ (y2-y1)2. You have already seen how to calculate a number to the power of 2. To calculate the square root of a number, you can use the Math.sqrt method.
Below is my code:
mport java.util.Scanner; public class Exercise9 { public static void main(String[] args) { int x1,x2,y1,y2; double dis; Scanner sc=new Scanner(System.in); System.out.print(\"Enter x coordinate for point 1: \"); x1=new Scanner(System.in).nextInt(); System.out.print(\"Enter y coordinate for point 1: \"); y1=new Scanner(System.in).nextInt(); System.out.print(\"Enter x coordinate for point 2: \"); x2=new Scanner(System.in).nextInt(); System.out.print(\"Enter y coordinate for point 2: \"); y2=new Scanner(System.in).nextInt(); dis=Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); System.out.println(\"The distance between the two points is \"+\"(\"+x1+\",\"+y1+\"),\"+\"(\"+x2+\",\"+y2+\")===>\"+dis); } }
The question screenshot
Exercise Learning objective: Calculate a complex result. Class to edit: Exercise Write a program that asks the user to input the (x,y) coordinates of 2 points, and then calculates and shows the distance between those two points. The program should execute according to the following sample 1/0: Enter y coordinate for point 1: user inputs 2.0 >> Enter y coordinate for point 1: user inputs 1.0 >> Enter x coordinate for point 2: user inputs 5.0 >> Enter y coordinate for point 2: user inputs 5.0 The distance between the two points is 5.0 Hint: Use variable names x1,y1 for the coordinates of point 1, and use variable names x2,y2 for the coordinates of point 2. The formula for the distance between two points is: V(x2-x1)2 + (y2-y1)?. You have already seen how to calculate a number to the power of 2. To calculate the square root of a number, you can use the Math. sart method. 1 2 3 Not yet TESTCASES 0/1 passed test1 Your program produced the incorrect output. DIFF SPLIT DIFF YOUR OUTPUT EXPECTED + I Enter x coordinate for point 1: 2.0 Exception in thread \"main\" java.util. InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at Exercise9.main(Exercise9.java:8) Enter y coordinate for point 1: 1.0 Enter x coordinate for point 2: 5.0 + Enter y coordinate for point 2: 5.0 + The distance between the two points is 5.0 Show explanation
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
