Created
March 31, 2015 20:46
-
-
Save kchodorow/d908c1cea4f80b828dd1 to your computer and use it in GitHub Desktop.
Train skeleton
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
public class SubwayLine { | |
public static void main(String[] args) { | |
Train local = new Train("C", 1, 16); | |
Train express = new Train("A", 3, 0); | |
for (int i = 0; i < 10; ++i) { | |
System.out.println("local: " + local.nextStop()); | |
System.out.println("express: " + express.nextStop()); | |
} | |
} | |
} | |
class Train { | |
private String name; | |
private int numStationsBetweenStops; | |
private int currentStation; | |
public Train(String inputName, int inputNumStations, | |
int startingStation) { | |
name = inputName; | |
numStationsBetweenStops = inputNumStations; | |
currentStation = startingStation; | |
} | |
public int nextStop() { | |
currentStation = currentStation + numStationsBetweenStops; | |
if (currentStation >= 20) { | |
currentStation = 20; | |
} | |
return currentStation; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment