Goal:The goal of this assignment is to gain practical experience with procedural abstraction – how complex functionality can be broken up into different methods and different classes.
Problem Overview:In this assignment, you will continue to implement the component classes of a program for simulating the behaviour of a train management system.
Task Overview:In brief, you will write a method for reading in the sections of a track from a file, and you will write a method for allocating routes to trains (so that they don’t collide). If you are a CSSE7023 student you will also be required to write a JUnit4 test suite for testing the track-reading method. More specifically, you must code method read from the TrackReader class and method allocate from the Allocator class that are available in the zip file that accompanies this handout, according to their specifications in those files. If you are a CSSE7023 student, you will also need to complete a systematic and understandable JUnit4 test suite for the read method in the skeleton of the TrackReaderTest class from the railway.test package. You may write your unit tests assuming that the classes that TrackReader depends on (e.g. the Junction, Section, Track classes and any of the Java 8 SE API classes) are implemented and functioning correctly. That is, you don’t need to create test stubs for these classes.
As in Assignment 1,You must complete these methods and classes as if other programmes were, at the same time, implementing classes that use it. Hence: • Don’t change the class names, specifications, or alter the method names, parameter types, return types, exceptions were thrown or the packages to which the files belong.
- You are encouraged to use Java 8 SE API classes, but no third party libraries should be used. (It is not necessary, and makes marking hard.)
- Don’t write any code that is operating-system specific (e.g. by hard-coding in newline characters etc.), since we will batch test your code on a Unix machine.
- Any new methods or fields that you add to TrackReader or Allocator must be private (i.e. don’t change the specification of these classes.)
- Your source file should be written using ASCII characters only.
- Implement the classes as if other programmes are going to be using and maintaining them. Hence:
- Your code should follow accepted Java naming conventions, be consistently indented, readable, and use embedded whitespace consistently. Line length should not be over 80 characters. (Hint: if you are using Eclipse you might want to consider getting it to automatically format your code.)
- Any additional methods that you write and fields that you introduce should be private to hide implementation details and protect invariants.
- Private methods that you write must be commented using preconditions and postconditions (require and ensures clauses). The informal description is OK.
- Fields and local variables (except for-loop variables) should have appropriate comments. Comments should also be used to describe any particularly tricky sections of code. However, you should also strive to make your code understandable without reference to comments; e.g. by choosing the sensible method and variable names, and by coding in a straightforward way.
- Any exceptions that are created and thrown should have appropriate messages to help the user understand why the exception was thrown. This is particularly important for the read method in TrackReader, since if there is an error with the file format, then the user will want to know what is wrong with it when a FormatException is thrown. Each
- FormatException thrown should have a meaningful message that accurately describes the problem with the input file format, including the line of the file where the problem was detected. (You can create a new FormatException with a message using the constructor that takes a string parameter.)
- The methods that you have to write must be decomposed into a clear and not overly complicated solution, using private methods to prevent any individual method from doing too much. since we will test your code using our original versions of these other files. Do not add any new files that your code for these classes depends on upon since you won’t submit them and we won’t be testing your code using them.
The JUnit4 test classes as provided in the package railway.test are not intended to be an exhaustive test for your code. Part of your task will be to expand on these tests to ensure that your code behaves as required by the javadoc comments. (Only if you are a CSSE7023 student will you be required to submit your test file TrackReaderTest.java.) We will test your code using our own extensive suite of JUnit test cases. (Once again, this is intended to mirror what happens in real life.
You write your code according to the “spec”, and test it, and then hand it over to other people … who test and / or use it in ways that you may not have thought of.) or email the course coordinator to clarify the requirements. Real software projects have requirements that aren’t entirely clear! If necessary, there may be some small changes to the files that are provided, up to 1 week before the deadline, in order to make the requirements clearer, or to tweak test cases. These updates will be clearly announced on the Announcements page of Blackboard, and during the lectures.
More about the allocate method from the Allocator class:In this section, we explain some of the terminology used in the specification of the allocate method.
- A route describes a path through a track. A route is a list of zero or more (non-null) segments.
- A segment, implemented as the class Segment from the railway package, is a part of the route. A segment is located on one section of track, and it has a direction of travel along that section, and a first and last location that are in that section. The direction of travel is specified with respect to the end-points of the section: one of the section endpoints is designated as the departing end-point and the other as the approaching end-point. The direction of travel is away from the departing end-point towards the approaching end-point. Let startOffset and endOffset be the offset of the segment's first location and last location (respectively) with respect to the departing endpoint of the segment. We must have that startOffset < endOffset. For every offset x satisfying startOffset <= x <= endOffset, the segment of the track contains every location at distance x from the segment's departing end-point. The location at distance startOffset from the departing end-point is referred to as the first location on the segment, the location at distance startOffset+1 from the departing end-point is the second location on the segment, etc. The length of the segment is endOffset - startOffset, and it contains (1 + endOffset - startOffset) locations. Since startOffset < endOffset, this length must be greater than or equal to one. For example, consider a segment that lies on a section of length 9 with end-points (j0, FACING) and (j1,FACING). If (j1,FACING) is the designated departing endpoint of the segment, the first location is 3 meters from (j1, FACING) and the last location is 7 meters from (j1, FACING), then the segment denotes a part of a route that starts at 3 meters from (j1, FACING), and travels towards end-point (j0, FACING) until it reaches the location that is 7 meters from (j1, FACING). It contains 5 locations and is 4 meters long.
- A segment is part of the track if it is a section that lies on that track. A route is part of a track if all of its segments are part of the track.
- A router r is valid if and only if the following conditions are all satisfied:
- The segments are connected, and they only connect at junctions. That is, for all indices i of route r satisfying 0 <= i < r.size()-1, the last location of r.get(i) equals the first location of r.get(i+1), and that location is at a junction.
- The direction of travel through junctions is possible. This means that if a train approaches a junction on a FACING branch, then it may only depart on the NORMAL or REVERSE branch of the junction. If it approaches the junction on one of the other two branches, then it may only depart the junction on the FACING branch. I.e. for for all indices i of route r satisfying 0 <= i < r.size()-1, if (r.get(i)).getApproachingBranch()is FACING, then (r.get(i+1)).getDepartingBranch()may be either NORMAL or REVERSE. If (r.get(i)).getApproachingBranch()is not FACING, then (r.get(i+1)).getDepartingBranch()must be FACING.
- A route r1 intersects another route r2, if there is a location that is contained in both r1 and r2. A route contains a location if that location is contained in one or more of its segments.
- A segment p is a prefix of a segment s if both segments p and s have the same section, departing end-point and start to offset (i.e. first location), and the length of p is less than or equal to the length of s. (As a special case, a segment is a prefix of itself.)
- A route p is a prefix of a route r if p.size()<= r.size() and for all indices i satisfying 0 <= i < p.size()-1, we have that p.get(i) equals r.get(i) and if p.size()!=0, then the last segment in p, p.get(p.size()-1), is a prefix of r.get(p.size()-1). (As a special case, a route is a prefix of itself, and the empty route – the route with no segments – is a prefix of any route.)
- The length of a route r is the sum of the lengths of its constituent segments, i.e. the sum of (r.get(i)).getLength() for each index i in the domain of the list.