package uwstout.courses.cs144.examples; import java.text.DecimalFormat; import java.util.Scanner; /** * Reads two delivery points from the user, and outputs the distance between the * origin, and the two in city blocks. * * Reads in two delivery points (X Y) from the user on, and calculates the * distance between (0,0), the first point, and the last point in city blocks. * This calculated value is then output to the console. * * @author Brett Bender * @version 2022.09.15 * */ public class Distance { private final static Stop ORIGIN_STOP = new Stop(0, 0); /** * Calculates the city block distance between points. * * Reads in two delivery points from System.in, entered in an X Y pair. These * points are then used to calculate the distance (in city blocks) between * (0,0), the first point, and the last point. This calculated value is then * output to the console. * * @param args The arguments passed from the command line - not used */ public static void main(String[] args) { Scanner scnr = new Scanner(System.in); DecimalFormat df = new DecimalFormat("#,##0.00"); // Read in route name System.out.print("Enter route name: "); String routeName = scnr.nextLine(); Stop stop1 = Stop.readStop(scnr, "Enter first delivery point X Y coordinates: "); Stop stop2 = Stop.readStop(scnr, "Enter second delivery point X Y coordinates: "); System.out.printf("%s to %s to %s\n", ORIGIN_STOP, stop1, stop2); // Calculate /// Starting at 0,0 and going to the two stops in sequence int blockDistanceTraveled = ORIGIN_STOP.getBlockDistance(stop1) + stop1.getBlockDistance(stop2); double directDistanceTraveled = ORIGIN_STOP.getDistance(stop1) + stop1.getDistance(stop2); // Output System.out.println("Stop 1: " + stop1); System.out.println("Stop 2: " + stop2); System.out.printf("Total distance travelled (in city blocks) for route %s: %d\n", routeName, blockDistanceTraveled); System.out.printf("The direct distance is: %s\n", df.format(directDistanceTraveled)); scnr.close(); } }