/* * T01n03.java -- Compute the distance between two 2D points, using * a few methods from the Math class. * * Do you see why I have that "D" at the end of the second "100" in * the distanceForDisplay calculation? If not, try removing it and * see what happens. */ import java.util.*; public class T01n03 { public static void main (String [] args) { double x1, y1; // coordinates of the first point double x2, y2; // coordinates of the second point double distance; // distance between the two points double distanceForDisplay; // rounded to two decimal places Scanner keyboard = new Scanner (System.in); // still magic! // Gather the coordinates of the two points System.out.print("Enter the x-coordinate of the first point : "); x1 = keyboard.nextDouble(); System.out.print("Enter the y-coordinate of the first point : "); y1 = keyboard.nextDouble(); System.out.print("Enter the x-coordinate of the second point : "); x2 = keyboard.nextDouble(); System.out.print("Enter the y-coordinate of the second point : "); y2 = keyboard.nextDouble(); // Compute the distance between the points, and // round the result to two decimal places. distance = Math.sqrt( Math.pow(x2-x1,2) + Math.pow(y2-y1,2) ); distanceForDisplay = Math.round(distance * 100) / 100D; System.out.println("\nThe distance from (" + x1 + "," + y1 + ") " + "to (" + x2 + "," + y2 + ") " + "is " + distanceForDisplay + " units."); } }