Question
Given a set of cities, the central city is the city that has the shortest total distance to all other cities. Write a program (
Given a set of cities, the central city is the city that has the shortest total distance to all
other cities. Write a program (
CentralCity.java
) that prompts the user to enter the
number of the cities and the locations of the cities (x and y coordinates), stores the
locations into a n-by-2 matrix where n is the number of the cities, and finds the central
city and its total distance to all other cities. Here is a sample run:
Enter the number of cities: 5
Enter the coordinates of the cities: 2.5 5 5.1 3 1 9 5.4 54 5.5 2.1
The central city is at (2.50, 5.00).
The total distance to all other cities is 60.81.
You are required to write two methods with the following headers, respectively:
// return distance between two points c1 and c2
public static double distance(double [] c1, double [] c2)
// return the total distance of a specific city to all other cities
// where cities contains all cities, and
i
is the index for a specific city
public static double totalDistance(double [][] cities, int i)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started