The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 107 Weeks Ago, 5 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
What I have:
import java.util.Scanner;
public class CentralCities {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of cities: ");
int numberOfCities = input.nextInt();
double [][] cities = new double[numberOfCities][2];
System.out.print("Enter the coordinates of the cities: ");
for(int row = 0; row for(int column = 0; column cities[row][column] = input.nextDouble();
}
}
System.out.printf("The central city is at (%.2f, %.2f).n" , cities[0][1]);
//System.out.printf("The total distance from the central city to all other cities is %.2f." , totalDistance);
}
public static double distance(double [] c1, double [] c2){
double distance = Math.sqrt((c2[0] - c1[0]) * (c2[0] - c1[0]) + (c2[1] - c1[1]) * (c2[1] - c1[1]));
return distance;
}
public static double totalDistance(double [][] cities, int i){
double totalDistance = 0;
for(int row = 0; row {
totalDistance += distance(cities[row], cities[i]);
}
return totalDistance;
}
}
Â
JAVA_HA8.1: Central CityGiven a set of cities, the central city is the city that has the shortest total distance to allother cities. Write a program (CentralCity.java) that prompts the user to enter thenumber of the cities and the locations of the cities (x and y coordinates), stores thelocations into a n-by-2 matrix where n is the number of the cities, and finds the centralcity and its total distance to all other cities. Here is a sample run:Enter the number of cities:5Enter the coordinates of the cities:2.5 5 5.1 3 1 9 5.4 54 5.5 2.1The central city is at (2.50, 5.00).The total distance from the central city 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 c2public static double distance(double [] c1, double [] c2)// return the total distance of a specific city to all other cities// where cities contains all cities, andiis the index for a specific citypublic static double totalDistance(double [][] cities, int i)Submit the source code fileCentralCity.javato drop box HA8.1.
Attachments:
-----------