public class Distance { public static void main(String[] args) { printWelcomeMessage(); Scanner console = new Scanner(System.in); System.out.println("Pair 1:"); double distance1 = getDistance(console); System.out.println(); System.out.println("Pair 2:"); double distance2 = getDistance(console); System.out.println(); System.out.println("Pair 1 is " + distance1 + " apart"); System.out.println("Pair 2 is " + distance2 + " apart"); // distance1 < distance2 --> pair 1 is closer // distance2 < distance1 --> pair 2 is closer // nested if / else statement /* if (distance1 < distance2) { System.out.println("Pair 1 is the closer pair."); } else { // distance1 >= distance2 if (distance1 > distance2) { System.out.println("Pair 2 is the closer pair."); } else { // distance1 == distance2 System.out.println("The two pairs have equal distance."); } } */ if (distance1 < distance2) { System.out.println("Pair 1 is the closer pair."); } else if (distance1 > distance2) { System.out.println("Pair 2 is the closer pair."); } else { // distance1 == distance2 System.out.println("The two pairs have equal distance."); } } public static double getDistance(Scanner console) { System.out.print(" Enter a point's x and y coordinate: "); double x1 = console.nextDouble(); double y1 = console.nextDouble(); System.out.print(" Enter a point's x and y coordinate: "); double x2 = console.nextDouble(); double y2 = console.nextDouble(); double distance1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); return distance1; } public static void printWelcomeMessage() { System.out.println("This program reads two pairs of (x, y) points from the user"); System.out.println("and computes the distance between each pair, then reports"); System.out.println("the distances."); } }