-
-
Save bytecodeman/e1edcca6dcbd08536a218e0e5da5b8c8 to your computer and use it in GitHub Desktop.
CSC-111 In class two dimensional array exercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class CityDistanceMatrix { | |
private static int cityToIndex(String cities[], String city) { | |
for (int i = 0; i < cities.length; i++) | |
if (cities[i].equals(city)) | |
return i; | |
return -1; | |
} | |
// In class exercise, code this method | |
private static double milesInFlightPath(int[] cityIndexes, double[][] distMatrix) { | |
return 0; | |
} | |
private static int[] flightPath(Scanner sc, String cities[]) { | |
int[] path = new int[10]; | |
int count = 0; | |
do { | |
System.out.printf("Enter City %d on Path: ", count + 1); | |
String city = sc.nextLine(); | |
if (city.equals("End")) | |
break; | |
int cityIndex = cityToIndex(cities, city); | |
if (cityIndex == -1) { | |
return null; | |
} | |
path[count] = cityIndex; | |
count++; | |
} while (true); | |
int temp[] = new int[count]; | |
for (int i = 0; i < count; i++) | |
temp[i] = path[i]; | |
return temp; | |
} | |
private static void startToDestDistance(Scanner sc, String[] cities, double[][] distMatrix) { | |
System.out.print("Enter Start City: "); | |
String start = sc.nextLine(); | |
System.out.print("Enter Destination City: "); | |
String dest = sc.nextLine(); | |
int row = cityToIndex(cities, start); | |
int col = cityToIndex(cities, dest); | |
double dist = distMatrix[row][col]; | |
System.out.println("The Distance from " + start + " to " + dest + " is " + dist); | |
} | |
private static void flightPathDistance(Scanner sc, String[] cities, double[][] distMatrix) { | |
int[] path = flightPath(sc, cities); | |
if (path != null && path.length >= 2) { | |
double totalDist = milesInFlightPath(path, distMatrix); | |
System.out.printf("Total Distance = %.0f\n", totalDist); | |
} | |
else { | |
System.out.println("Problem with Flight Path"); | |
} | |
} | |
public static void main(String[] args) { | |
String cities[] = { | |
"Chicago", "Boston", "New York", "Atlanta", "Miami", | |
"Dallas", "Houston" | |
}; | |
double distMatrix[][] = { | |
{ 0, 983, 787, 714, 1375, 967, 1087 }, | |
{ 983, 0, 214, 1102, 1763, 1723, 1842 }, | |
{ 787, 214, 0, 888, 1549, 1548, 1627 }, | |
{ 714, 1102, 888, 0, 661, 781, 810 }, | |
{ 1375, 1763, 1549, 661, 0, 1426, 1197 }, | |
{ 967, 1723, 1548, 781, 1426, 0, 239 }, | |
{ 1087, 1842, 1627, 810, 1187, 239, 0 } | |
}; | |
Scanner sc = new Scanner(System.in); | |
//Comment the following for in class exercise | |
startToDestDistance(sc, cities, distMatrix); | |
//Uncomment The following for in class exercise | |
//flightPathDistance(sc, cities, distMatrix); | |
sc.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment