Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.util.Scanner; public class Exercise 0 8 _ 2 1 { public static void main ( String [ ] args ) { Scanner input =
import java.util.Scanner;
public class Exercise
public static void mainString args
Scanner input new ScannerSystemin;
System.out.printEnter the number of cities: ;
int numberOfCities input.nextInt;
System.out.printEnter the coordinates of the cities: ;
double cities new doublenumberOfCities;
for int i ; i cities.length; i
citiesi input.nextDouble;
citiesi input.nextDouble;
Find the central city and the total distance
from the central city to all other cities
double minTotal totalDistancecities;
int minIndex ;
for int i ; i cities.length; i
double total totalDistancecities i;
if minTotal total
minTotal total;
minIndex i;
System.out.printlnThe central city is at
citiesminIndex citiesminIndex;
System.out.printlnThe total distance to all other cities is
minTotal;
public static double totalDistancedouble cities, int i
double total ;
for int j ; j cities.length; j
total distancecitiesi citiesj;
return total;
public static double distancedouble c double c
return Math.sqrtc cc c
c cc c;
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