Question
getDirectlyConnectedCities public java.lang.String[] getDirectlyConnectedCities(java.lang.String name) Returns the directly connected cities of a particular city, sorted by name. Parameters: name - the name of the city
getDirectlyConnectedCities
public java.lang.String[] getDirectlyConnectedCities(java.lang.String name)
Returns the directly connected cities of a particular city, sorted by name.
Parameters:
name - the name of the city
Returns:
an array of the directly connected cities of the specified city, sorted by name. Returns an array of size zero if the specified city does not exist, or if the city exists but has no connected cities.
degreesOfSeparation
public int degreesOfSeparation(java.lang.String city1, java.lang.String city2)
Returns the degrees of separation between two (not necessarily distinct) cities. Returns -1 if there is no way to get to one city from another through a chain of roads. For example, if Oxford is directly connected to Cincinnati, and Cincinnati is directly connected to Dayton, then: The degrees of separation between Oxford and Cincinnati is 1. The degrees of separation between Oxford and Dayton is 2. The degrees of separation between Oxford and Oxford is 0. The degrees of separation between Oxford and Columbus is -1. Note that degrees of separation is commutative. So, if degreesOfSeparation("A", "B") is 5, then degreesOfSeparation("B", "A") must also be 5.
Parameters:
city1 - the name of one city
city2 - the name of another city (not necessarily different)
Returns:
The number of degrees of separation between city1 and city2, or -1 if the two are not connected directly or by a chain of roads.
import java.util.ArrayList; import java.util.Arrays;
public class RoadNetwork {
private String [] data; private int size; public static int DEFAULT_CAPACITY = 500;
//Constructors public RoadNetwork() { data = new String[DEFAULT_CAPACITY]; size = 0; }
public RoadNetwork(int maxSize) { data = new String[maxSize]; size = 0; }
//Methods public boolean addCity(String name) { if(size >= data.length) { return false; } else { data[size] = name; size++; return true; }
}
public void removeCity(String name) { for(int i = 0; i < size; i ++) { if(name.equals(data[i])) { data[i] = data[size - 1]; data[size - 1] = null; size --; } } }
public boolean containsCity(String name) { for(int i = 0; i < size; i ++) { if(name.equals(data[i])) { return true; } } return false; }
public int getCityCount() { return size;
}
public String[] getCities() { return data;
}
//create new arrayList of all the cities and put connected cities into the arrayList public boolean connect(String city1, String city2) { if(this.containsCity(city1)&&!this.containsCity(city2)) { this.addCity(city2); return true; } if(!this.containsCity(city1)&&this.containsCity(city2)) { this.addCity(city1); return true; }
return false;
} //go to city1 list and remove city2 and go to city2 list and remove city1 public void disconnect(String city1, String city2) { if(this.containsCity(city1)) { this.removeCity(city2); } if(this.containsCity(city2)) { this.removeCity(city1); } return; } //If there are any line that gets you from one city to another public boolean areConnected(String city1, String city2) { if(this.containsCity(city1)&&this.containsCity(city2)) { return true; }
return false;
} // within the arrayList of the single city public boolean areDirectlyConnected(String city1, String city2) { if(this.containsCity(city1)&&this.containsCity(city2)&&this.getCityCount()==2) { return true; } return false; }
//how far apart a city is from another city
public int degreesOfSeparation(String city1, String city2) {
return 0;
}
// return arrayList of all the cities that are directly connected
public String[] getDirectlyConnectedCities(String name) {
return null;
}
}
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