2020/8/12 Assignment 2 - Courses/COMP103_2020T2 | ECS | Victoria University of Wellington
https://ecs.wgtn.ac.nz/Courses/COMP103_2020T2/Assignment2 1/7 School of Engineering and Computer Science TeKuraMātaiPūkaha,Pūrorohiko COMP103 2020 Tri 2 Assignment 2: Using Collections
Due 14 Aug , 10 am
Goals and Assessment
This assignment will give you experience at writing programs using Collections.
The first part is a program that answers queries about Wellington trains and stations.
The second part is a very simple drawing program to which you must add an undo/redo facility.
The third and fourth parts have several methods using lists and stacks that will be automatically marked,
which you should do after the first two parts.
The assignment is worth 15% of your final mark.
Resources and links
Download Zip file containing the necessary code and data.
Getting online help
Video of lecture session with Demos of the assignments (go to minute 33 and minute 45).
Submit your answers
Marks and feedback
What To Hand In
By 10 am on 14 Aug, you need to submit:
WellingtonTrains.java, Station.java, TrainLine.java, TrainService.java,
Pencil.java, and any other classes you made for the Pencil program,
HTMLChecker.java
ListOfLists.java
If you haven't finished by three days after the due time (ie, by 10am Monday 17th) submit whatever you have
done at that point.
Part 1: Wellington Trains (Weight: 4% of your final mark)
WellingtonTrains is a program to answer queries about Wellington train stations, train lines and the
timetables for the train services on those lines.
2020/8/12 Assignment 2 - Courses/COMP103_2020T2 | ECS | Victoria University of Wellington
https://ecs.wgtn.ac.nz/Courses/COMP103_2020T2/Assignment2 2/7
(See https://www.metlink.org.nz/#plan for Metlink's equivalent program for the whole of the Wellington
Regional Transport system. Your program is not web-based, and doesn't need such a fancy interface.)
Stations, Lines, and Services: Your program has to deal with three kinds of information:
Train Stations
Train Lines: a sequence of stations. For example, the Wellington_Johnsonville line, which starts at
Wellington station, and ends at Johnsonville station, with 7 stations in between. To make things easier,
we treat the inbound and outbound as two separate train lines, so the Wellington_Melling line is different
from the Melling_Wellington line. (They have the same stations, but the sequence in one is the reverse
of the sequence in the other.)
Train Service: a schedule/timetable for a particular train running on a given train line. For example, the
11:32 train on the Wellington_Johnsonville line that leaves Wellington station at 11:32, leaves CroftonDowns at 11:40, ... gets in to Johnsonville at 11:55.
A Train Service is specified by a sequence of times - the time that the train leaves the first station,
followed by the times that the train gets to each of the remaining stations on the line. Note that some
train services don't stop at every station on the line. If a train doesn't stop at a station, the corresponding
time will be -1.
Queries
The basic queries the program should be able to handle are the following:
1. List all the stations in the region
2. List all the stations in alphabetic order (by name).
3. List all the train lines in the region
4. List the train lines that go through a given station
5. List the stations along a given train line
6. Print the name of a train line that goes from a station to a destination station. (The train line must go the
correct direction.)
7. (Completion) Find the next train service for each line at a station after the specified time
8. (Completion) Find a trip between two stations (on the same line), after the specified time.
Find the train line, the time that next service on that line will leave the first station, the time that the
service will arrive at the destination station, and the number of fare zones the trip goes through.
9. (Completion) Find the 10 closest stations to a point on the map, listing the names and distances from
closest to furthest.
10. (Challenge:) More complex trips involve going from the first station to an exchange station on one train
line, then going from the exchange station to the second station on another train line. (In larger cities,
trips might require more than 2 train lines, but the Wellington region has a central hub, so you never
need more than two segments for a trip.)
Find the best trip (earliest arrival) between a starting station and a destination station. If the trip requires
two train lines, print out the starting time, the first train line, the exchange station, the arriving and
departing times at the exchange station, the second train line, and the arrival time at the destination, and
the total number of fare zones.
Data Files
There are a set of data files in the data sub directory that contain the information that your program needs
to load and then use.
data/stations.data contains one line of information about each station in the system: name, fare zone,
and location on the map. eg
Crofton-Downs 3 100 401
Note that there are no spaces in the station names - if the name has two parts, they are joined by a
hyphen.
data/train-lines.data contains the names of all the train lines, eg Melling_Wellington. Each line
is named by the station at the beginning of the line and the station at the end of the line, joined by an
underscore.
For each train line, there are two files:
one with the sequence of stations on the train line (eg data/Wellington_Johnsonvillestations.data )
2020/8/12 Assignment 2 - Courses/COMP103_2020T2 | ECS | Victoria University of Wellington
https://ecs.wgtn.ac.nz/Courses/COMP103_2020T2/Assignment2 3/7
one with services (timetable) information (eg data/Wellington_Johnsonville-services.data )
Each line of a XXX_YYY-services.data file has a sequence of times for one train service on the train
line. The times are given in 24 hour time, with no ':' in the middle eg, 1437 is 2:37pm.
Data Structures Your WellingtonTrains program will need to load the data from the files into data structures inside the
program. A sensible way of organising the data inside your program will be to have
a collection of Stations (a Map, indexed by the names of the stations)
a collection of TrainLines (a Map, indexed by the names of the TrainLines)
For the Core of the assignment, you do not have to worry about the Train Services - just the Stations and
TrainLines.
We have provided Java Classes for Station, TrainLine, and TrainService objects to store
information about individual stations, train lines, and train services. Make sure that you read these classes
carefully and work out how to use them. They each have constructors, and methods to add data to the
object, and methods for accessing data in the object. Note that a TrainLine contains inside it a collection
of the TrainServices on that line, as well as a List of Stations, and a Station contains a collection of
the TrainLines that go through that station.
Template Code.
In addition to the Station.java , TrainLine.java , and TrainService.java classes, we have provided a
WellingtonTrains.java that contains just a few methods and fields. It contains a main method, a
doLoadData method, and a setupGUI method for creating a user interface like the one in the demo. You
do not need to use these methods unless you want to. The rest of the class is empty, and you will need to
write the methods that do all the work.
Suggested Algorithms for loading the datadistance from the
hub station
To Create the Map of Stations:distance from the hub station
Read the data from "data/stations.data"
For each line of the data,
construct a Station object using the data on the line
put the Station into the Map of Stations (the Station objects won't have any TrainLine
information at this point)
To Create the Map of TrainLines:
Read the list of train line names from "data/train-lines.data"
For each train line name
Construct a TrainLine object
Put the TrainLine object into the Map of TrainLines
For each station name in "data/XXX_YYY-stations.data
Look up the Station object in the Map of Stations,
Add the Station object to the stations in the TrainLine object,
Add the TrainLine object to the train lines in the Station object
To load the Train Service (timetable) information: [Completion only]
Read the list of train line names from "data/train-lines.data"
For each train line name
For each sequence of times in "data/XXX_YYY-services.data" (one line per sequence):
Construct a TrainService object
Add the TrainService object to the TrainLine object
2020/8/12 Assignment 2 - Courses/COMP103_2020T2 | ECS | Victoria University of Wellington
https://ecs.wgtn.ac.nz/Courses/COMP103_2020T2/Assignment2 4/7
For each time in the sequence of times,
Add the time to the TrainService object.
Core:
For the Core, your program should
Load the Station and Train Line data from the files into distance from the hub stationdata structures
Be able to answer the first 6 queries above.
Note that question 6 is a bit tricky because it has to check that the first station comes before the
destination station in the sequence of stations along the line.
Completion:
For the Completion, your program should also
Load the Train Service (timetable) data from the files into the data structures.
Be able to answer questions 7 and 8 above (both of which require the train service data).
Answer question 9 above, (which requires responding to the mouse).
Challenge:
For the Challenge, your program should also
Be able to answer question 10 above - finding the best trip, even if it requires an exchange between two
train lines.
Improve the interface and make the program better, eg by showing the stations and the lines on a map
(there are two png files with maps of the system that you can use).
Hints
Look at the data files carefully so you know what is there. The data is all from the
www.metlink.org.nz site (but cleaned up); looking at the site may help you understand the data
better.
Read the Station.java , TrainLine.java , and TrainService.java classes carefully to make sure you
know how to use them.
Sketch a diagram of the data structures, showing the Maps, a couple of Stations, a couple of TrainLines,
and a TrainService, in order to make sure you know how they are all related.
Part 2: Pencil (Weight: 3% of your final mark)
The Pencil program is a very simple drawing program that lets the use draw freehand lines on the graphics
pane with a "pencil". The drawing will consist of a collection of strokes, where each stroke is a freehand line
between the point the user pressed the mouse and the point the user released the mouse.
You are to add an undo and redo facility to Pencil. The undo button should remove strokes from the
drawing, one at a time, from the most recently added back to the first stroke. The redo button should "undo
the undo", by putting strokes back into the drawing after they have been undone. If the user draws a new
stroke after doing an undo or redo, the program will "forget" all the strokes that have been undone.
This is similar to the Sokoban program in Assignment 1, except that the base program is much simpler, and
you will have to do more work to make the undo work. You will almost certainly need a class to represent
strokes, and you may need to change the way the program works, more than you did for Sokoban.
Core:
2020/8/12 Assignment 2 - Courses/COMP103_2020T2 | ECS | Victoria University of Wellington
https://ecs.wgtn.ac.nz/Courses/COMP103_2020T2/Assignment2 5/7
Add the ability to undo the strokes
Completion:
Add the ability to redo strokes that have been undone and then undo and redo them again, (as long as
no new strokes have been created since the undo action).
Challenge:
Add the ability to change the colour and width of the pen, and ensure that undoing a stroke will take the
colour and width back to what it was before that stroke was added. Eg, if the user draws three black
strokes, then changes to red and draws two red strokes, then undoing the two red strokes will switch the
color back to black.
Part 3: HTMLChecker: (Automarked) (Weight: 2% of your final mark)
HTML files contain tags that specify the formatting of the document.
Tags are surrounded by < and >
Opening tags have a word immediately following the <
Closing tags have a forward-slash followed by a word after the <
To be a properly constructed HTML file, all the tags should come in matching opening and closing pairs, and
they should be nested properly. For example
is valid, but
| |
|
is invalid because there is no closing tag to match the opening table tag, and the tr and td tags are not
properly nested.
Complete the checkTags(...) method in the HTMLChecker.java program so that it checks whether a
list of opening and closing tags is valid (matching opening and closing tags that are all properly nested). It
returns true or false to say whether the list of tags is valid.
The argument to checkTags is a List of Strings which are the names of the tags. The closing tags start with
"/", and the opening tags do not. For example:
["html","header", "/header", "body", "div", "/div", "/body", "/html"]
You should use a Stack to do the checking.
Note: There are several methods to help you test your code. The readTags(...) method will read an html
file, and extract a list of all the opening and closing tags in the format needed by checkTags. There are
several example html files provided, but you can use readTags(...) to test your method on any html file,
including your own test files.
2020/8/12 Assignment 2 - Courses/COMP103_2020T2 | ECS | Victoria University of Wellington
https://ecs.wgtn.ac.nz/Courses/COMP103_2020T2/Assignment2 6/7 Part 4: Lists of Lists: (Automarked) (Weight: 6% of your final mark)
Complete the following three methods for manipulating Lists of Lists of Things in the
ListsOfLists.java file.
Notes:
a Thing has an equals method
Thing is Comparable, so Things can be compared with the compareTo method,
Your answers should not depend on any of the other methods in the Thing class (eg the toString
method) though you may use them in your testing.
Your answers may not use the Collections class or any of the other classes in java.util except
List , and Stack . public List appendAll(List> lists)
appendAll is passed a List of Lists of Things,
It should return a new List of Things which contains all the lists appended together.
For example, if lists contains three lists:
{A, B, C, D} and {C, E, F, G} and {M, H, F, B} ,
then appendAll should return the list:
{A, B, C, D, C, E, F, G, M, H, F, B} You may assume that none of the lists contain null.
The method should not modify the lists in the argument.
public List mergeZipped(List> lists)
mergeZipped is passed a List of Lists of Things,
It should return a new List of Things which contains all the items in the lists, merged "like a zip": The
answer should have all the first elements of the lists, then all the second elements, then all the third
elements, etc.
The lists may be different lengths.
For example, if lists contains four lists:
{A, B, C, D} and {C, E, F, G, I, J} and {M, F, B} and {Z, Y, X, W, V} ,
then mergeZipped should return the list:
{A, C, M, Z, B, E, F, Y, C, F, B, X, D, G, W, I, V, J}
The method should not modify the lists in the argument.
You may assume that none of the lists contain null.
public List mergeSorted(List> lists)
mergeSorted is passed a List of sorted Lists of Things,
It should return a new List of Things which contains all the items in the lists, in sorted order.
For example, if lists contains four lists:
{A, B, C, D} and {C, E, F, G, I, J} and {B, F, M} and {V, W, X, Y, Z} ,
then mergeSorted should return the list:
{A, B, B, C, C, D, E, F, F, G, I, J, M, V, W, X, Y, Z}
The method may not modify the argument lists.
Hint: make copies of the lists which are safe to modify.
The lists may be different lengths.
You may assume that none of the lists contain null.
mergeSorted should construct the new list by adding one Thing at a time to the list:
At each step, your method should:
2020/8/12 Assignment 2 - Courses/COMP103_2020T2 | ECS | Victoria University of Wellington
https://ecs.wgtn.ac.nz/Courses/COMP103_2020T2/Assignment2 7/7
Find the next item to add to the answer by looking at the front item of each list to find the smallest.
(Hint: Thing is Comparable, so Things can be compared with compareTo).
Move that item from its list to the answer. (Hint: remove lists once they are empty).
UPDATE: You must use the described method. Do not use bubblesort (or any other sorting algorithm),
Collections.sort or Arrays.sort.
Note: Your code in HTMLChecker.java and ListOfLists may be automarked. That means
Your code must compile, otherwise you may get 0.
Check the message you get back when you complete your submission to ensure that your code
compiled correctly. You must not change the signature (the header line) of any of the methods that will be marked. (This will
make your code not compile in our marking system.)
You may not change or add any import lines (which means you may not use any methods from the
Collections class).
The methods to be marked must not do any interaction with the user or read from files - this will cause
the automarking to give you 0.
Therefore, don't use
any UI.ask...(...) methods
UI.print(...) or UI.println(...) or UI.printf(....)
UIFileChooser.open() or UIFileChooser.save()
System.out or System.in
You are allowed to modify the test code, and you are allowed to use the UI methods in the test code, but
not in the three methods that will be marked.
- QQ:99515681
- 邮箱:99515681@qq.com
- 工作时间:8:00-21:00
- 微信:codinghelp
联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!