It's Friday evening and tomorrow in the early morning hours Jill will
have to
travel from San Luis Obispo to Sacramento in order to get to the
regional
programming contest. Since she is afraid of arriving too late and being
excluded from the contest
she is looking for the train which gets her to Sacramento as early as
possible.
However, she dislikes waking up too early, so she wants to
be able to specify an earliest departure time.
Jill asks you to help her with her problem.
You are given a set of railroad schedules from which you must compute
the train with the earliest arrival time that departs on or after Jill's
specified departure time. One good thing: Jill is very experienced
in changing trains. She can do this
instantaneously, i.e., in zero time!!! So if a train arrives in
San Jose at 1230 and another train departs San Jose at 1230, Jill can
make the connection.
The train schedule is provided in a data file. Sample data file.
Each train trip in the schedule has a unique identifier, a departing
location, an arrival location, a departure time and an arrival
time.
The identifier and location names are alphanumeric strings
containing no blanks. The data fields are delimited by blanks or
newlines (the default for a Scanner).
Times are always specified in 4-digit 24-hour time format.
Assumptions:
The data in the schedule is properly formatted.
There are no duplicate trips in the schedule.
The core of your solution is a class TrainSchedule that
contains
two public methods shown below:
public class TrainSchedule
/** Load a train
schedule from the specified InputStream. */
public void
load(java.io.InputStream stream)
/** Find the best
route that satisfies the specified criteria.
* @param
depart the location where the journey begins
* @param
arrive the location where the journey ends.
* @param
startTime the earliest time at which the route may begin.
*
@return String the selected route as a list of trip identifiers
separated by blanks,
*
or "0" if no route meets the specified criteria.
* @pre depart != arrive
* @pre depart and arrive exist in the schedule
* @pre startTime is a valid time 0000 - 2359.
*/
public String
findBestRoute(String depart, String
arrive, String startTime)
The remainder of the design is left to the student. Your solution
will be graded for the quality of your design. You should create
at least one and probably two support classes.
You should use the JGraphT
library. If you look carefully in the JGraphT API you can find
algorithms that will solve the majority of the problem for you. The
instructor's solution is about 150 LOC.
Similarly, you should not write all the time manipulation functions
from scratch but take advantage of Java's Date, Calendar, and SimpleDateFormat classes.
You must write your own JUnit tests. Here's an example test:
TrainSchedule sched =
new TrainSchedule();
String t1 = "A2 SFO FAT 1100
1200\nA1 SFO LAX 1000 1500\nA3 FAT LAX 1201 1400";
StringBufferInputStream s =
new StringBufferInputStream (t1);
sched.load(s);
String result =
sched.findBestRoute("SFO","LAX","0900");
assertEquals("A2
A3",result.trim());
Technical note:
Release 0.8.0 and above of JGraphT require Java 1.6 to run.
Fortunately we don't need any version 0.8 features, and you can
download Release 0.7.3 and it will run on Java 1.5.
Handing in Your Source
Electronically
- Make sure your name is in the @author tag.
- Submit a zip file containing your source code and unit tests to
Web-CAT. You do not need to submit the jgrapht.jar file. Web-CAT will
use Release 0.7.3.
The program is worth the same number of points as a regular programming project. This is an individual assignment. You may not collaborate with anyone else on this project.