Java Programe

profilezhangc
RoadNetwork.html
JavaScript is disabled on your browser. Skip navigation links
  • Prev Class
  • Next Class

Class RoadNetwork

  • java.lang.Object
    • RoadNetwork
  • public class RoadNetwork
    extends java.lang.Object
    A simple road network of the cities of a state. In this network, roads are bidirectional. That is, if the city A is connected with the city B via a road, then city B is also connected with the city A via the same road.
    Author:
    Md Osman Gani
    • Constructor Summary

      Constructors 
      Constructor and Description
      RoadNetwork() Creates a state, with no cities, and a maximum size of 500 cities.
      RoadNetwork(int maxSize) Creates a state, with no cities, with the specified maximum size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      boolean addCity(java.lang.String name) Adds a city to the state, i.e.
      boolean areConnected(java.lang.String city1, java.lang.String city2) Determines whether two cities are connected directly or through a chain of roads or not.
      boolean areDirectlyConnected(java.lang.String city1, java.lang.String city2) Determines whether two cities are connected directly via a road or not.
      boolean connect(java.lang.String city1, java.lang.String city2) If city1 and city2 are in the network, then connect them via a road.
      boolean containsCity(java.lang.String name) Returns true if there exists a city in the network with the given name, and false otherwise.
      int degreesOfSeparation(java.lang.String city1, java.lang.String city2) Returns the degrees of separation between two (not necessarily distinct) cities.
      void disconnect(java.lang.String city1, java.lang.String city2) Removes city1 from city2's directly connected city list, and removes city2 from city1's directly connected list.
      java.lang.String[] getCities() Returns an (possibly empty) array of all cities in the network, sorted by name.
      int getCityCount() Gets a count of the number of cities in the network.
      java.lang.String[] getDirectlyConnectedCities(java.lang.String name) Returns the directly connected cities of a particular city, sorted by name.
      void removeCity(java.lang.String name) Removes city from the road network.
      java.lang.String toString() Returns a newline-separated list of cities, including a comma-separated list of each city's directly connected cities.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • RoadNetwork
        public RoadNetwork()
        Creates a state, with no cities, and a maximum size of 500 cities.
      • RoadNetwork
        public RoadNetwork(int maxSize)
        Creates a state, with no cities, with the specified maximum size.
        Parameters:
        maxSize - the maximum number of cities for the network
    • Method Detail

      • addCity
        public boolean addCity(java.lang.String name)
        Adds a city to the state, i.e. the road network. Returns true if, at the end of the method, that city is in the network, and false otherwise. Note that if the city already exists in the network, the city will not be added a second time, and the method will return true to indicate that the city is in the network. The only reason this method should fail is if the road network has reached its maximum size.
        Parameters:
        name - the name of the city to add. No two cities may have the same name.
        Returns:
        true if the city is in the network at the completion of this method, and false if not.
      • removeCity
        public void removeCity(java.lang.String name)
        Removes city from the road network. If city has road with any other cities, all roads associated with that city need to be removed. That is, city will be removed from every cities road list. Does nothing if city is not in the network.
        Parameters:
        name - the name of the city to be removed
      • containsCity
        public boolean containsCity(java.lang.String name)
        Returns true if there exists a city in the network with the given name, and false otherwise.
        Parameters:
        name - the name of the city
        Returns:
        true if the member exists in the network, and false otherwise
      • getCityCount
        public int getCityCount()
        Gets a count of the number of cities in the network.
        Returns:
        the number of cities in the road network
      • getCities
        public java.lang.String[] getCities()
        Returns an (possibly empty) array of all cities in the network, sorted by name.
        Returns:
        an array of all cities in the network, sorted by name. Returns an array of length 0 if there are no cities in the network.
      • 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.
      • connect
        public boolean connect(java.lang.String city1,
                               java.lang.String city2)
        If city1 and city2 are in the network, then connect them via a road. Returns true if, at the end of the method, id1 and id2 are connected (which could mean they were connected before this method was invoked), and returns false otherwise (which really would only happen if city1 or city2 were not a member in the network.
        Parameters:
        city1 - the name of one city
        city2 - the name of another city
        Returns:
        true if city1 and city2 are connected directly in the network, and false otherwise.
      • disconnect
        public void disconnect(java.lang.String city1,
                               java.lang.String city2)
        Removes city1 from city2's directly connected city list, and removes city2 from city1's directly connected list. Does nothing city1 or city2 are not members, or if they are members but are not directly connected.
        Parameters:
        city1 - the name of one city
        city2 - the name of another city
      • areDirectlyConnected
        public boolean areDirectlyConnected(java.lang.String city1,
                                            java.lang.String city2)
        Determines whether two cities are connected directly via a road or not.
        Parameters:
        city1 - the name of one city
        city2 - the name of another city
        Returns:
        true if cities are directly connected, and false otherwise
      • areConnected
        public boolean areConnected(java.lang.String city1,
                                    java.lang.String city2)
        Determines whether two cities are connected directly or through a chain of roads or not.
        Parameters:
        city1 - the name of one city
        city2 - the name of another city (not necessarily different)
        Returns:
        true if cities are connected, and false otherwise
      • 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.
      • toString
        public java.lang.String toString()
        Returns a newline-separated list of cities, including a comma-separated list of each city's directly connected cities. Note that the list of cities should be sorted by name, and each city's list of directly connected cities should be sorted by name. For example: Akron [Athens, Columbus, Toledo] Athens [Akron] Cincinnati [Dayton] Columbus [Akron, Dayton] Dayton [Cincinnati, Columbus] Toledo [Akron]
        Overrides:
        toString in class java.lang.Object
        Returns:
        A newline-separated list of cities, with each city's directly connected cities, both sorted by name.
Skip navigation links
  • Prev Class
  • Next Class