/* * ===================================================================== * Track.java: Stations along a rail track in the Washington DC Metro... * ===================================================================== */ import java.lang.Math; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Track { protected String sName; protected List station; // Constructor methods .... public Track() { station = new ArrayList(); } public Track( String sName ) { this.sName = sName; station = new ArrayList(); } public Track( String sName, String stationNames[] ) { this.sName = sName; station = new ArrayList(); for ( int i = 0; i < stationNames.length; i = i + 1 ) station.add( stationNames[i] ); } // Set name of track .... public void setName ( String sName ) { this.sName = sName; } // Add station to track .... public void add ( String sName ) { station.add( sName ); } // Convert node to a string ... public String toString() { String s = "Track(\"" + sName + "\")\n"; for (int i = 0; i < station.size(); i = i + 1) s = s.concat( " " + station.get(i) + "\n" ); return s; } // Exercise methods in the Track class ..... public static void main( String args[] ) { System.out.println( "Exercise methods in Track class" ); System.out.println( "===============================" ); // Create list of stations of "green" track .... Track tA = new Track("Green"); tA.add ( "Greenbelt" ); tA.add ( "College Park" ); tA.add ( "PG Plaza" ); tA.add ( "Fort Totten" ); tA.add ( "L Enfant Plaza" ); // Create list of stations along "red" track .... String redTrack[] = { "Silver Spring", "Fort Totten", "Catholic University", "Union Station", "DuPont Circle" }; Track tB = new Track("Red", redTrack ); // Print details of "Green" and "Red" tracks .... System.out.println( tA.toString() ); System.out.println( tB.toString() ); } }