Question

first off this is an assignment so I'm more looking for help then coded answers (don't want to cheat!). My assignment is to create a program that processes a train/railway network of stations. The section i'm stuck on adds the stations, their connections, and returns these connections as an Array List of Strings. I've included below the code I have so far, and also an extract from the assignment (related to the section I'm on now). I've been struggling with this bit all weekend, so any help would be hugely appreciated.

It's only the implementation of the interface I need to edit, the "MyNetwork" class. I just feel I've been going in circles, and may not have even gotten off on the right foot?

From the assignment;

Create a class MyNetwork that implements the Network interface.

The getConnections method of this class should return an array containing only those stations directly connected to the fromStation argument. Hint 1: you can do this using a HashMap, with the keys being Strings (representing stations) and the values being ArrayLists of Strings (representing the stations to which there is a direct connection).

Hint 2: Although the getConnections method returns an array of Strings, it would be better for the values in the HashMap to be ArrayLists of Strings

The Interface;

public interface Network {

    /**
     * Add a station to the network.
     * @param station The station to be added.
     */
    public void addStation(String station);

    /**
     * Add a direct connection from one station to another.
     * @pre both fromStation and toStation have already been added by the method
     * addStation.
     * @param fromStation The station from which the connection begins. 
     * @param toStation The station at which the connection ends.
     */
    public void addConnection(String fromStation, String toStation);

    /**
     * Get a list of all stations directly connected to a given station.
     * @pre fromStation has been added to the network by the method addStation.
     * @param fromStation
     * @return A list of all the stations to which there is a direct connection
     * from fromStation. 
     */
    public String[] getConnections(String fromStation);

    /**
     * Search for a station in the network.
     * @param station Station to be searched for,
     * @return true if the Station exists in the network, false otherwise.
     */
    public boolean hasStation(String station);

    /**
     * Get all stations in the network.
     * @return An array containing all the stations in the network, with no
     * duplicates.
     */
    public String[] getStations();

The Implementation:

public class MyNetwork implements Network {

    @Override
    public void addStation(String station) {

        ArrayList<String> Stations = new ArrayList<>();
        Stations.add(station);
    }

    @Override
    public void addConnection(String fromStation, String toStation) {

        Map<String,String> Connections = new HashMap<>();
        Connections.put(fromStation, toStation);
    }

    @Override
    public String[] getConnections(String fromStation) {
        return null; // dummy value!

    }

    @Override
    public boolean hasStation(String station) {
        return false; // dummy value!
    }

    @Override
    public String[] getStations() {
        return null; // dummy value!
    }
}
Was it helpful?

Solution

Your network needs to have a state, using one or several instance field(s).

As is, it doesn't have any state. Each method creates a local variable of type List or Map, adds something to this List or Map, and returns. So the List or Map directly goes out of scope and is garbage collected.

private Map<String, List<String>> stations = new HashMap<>();

// now all your methods should use the above map.

See http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top