Assignment 2COMP 250 Winter 2020posted: Tuesday, Feb. 11 2020due: Tuesday, Feb. 25, 2020 at 23:59Learning ObjectivesThis assignment aims at building on what we have seen in assignment 1 to start storing informationand navigating it using complex data structures. Unlike in assignment 1, you will actually have tomake decisions on how you want to solve the problems at hand, and while we will see some methodsto tackle them in class, most of this discussion will be held at a conceptual level. Your task is totranslate your conceptual understanding of those concepts into Java. Watch out: the output of thisassignment is not deterministic, which means you can get different outcomes between executions.Unlike for assignment 1, or for your assignments in COMP 202, it will not be immediately obviouswhether your code works. Make sure to think about this and test your program thoroughly! Interms of specific concepts, this assignment tests all your knowledge on object-oriented programmingcovered in A1, as well as doubly linked list, navigating between nodes on lists, rearranging lists,and sorting.General Instructions• Submission instructions– Late assignments will be accepted up to 2 days late and will be penalized by 10 points perday. Note that submitting one minute late is the same as submitting 23 hours late. Wewill deduct 10 points for any student who has to resubmit after the due date (i.e. late)irrespective of the reason, be it wrong file submitted, wrong file format was submittedor any other reason. This policy will hold regardless of whether or not the student canprovide proof that the assignment was indeed “done” on time.– Don’t worry if you realize that you made a mistake after you submitted: you can submitmultiple times but only the latest submission will be evaluated. We encourage youto submit a first version a few days before the deadline (computer crashes do happenand myCourses may be overloaded during rush hours).– Please store all your files in a folder called “Assignment2”, zip the folder and submit itto MyCourses. Inside your zipped folder, there must be the following files.∗ TrainNetwork.java∗ TrainLine.java∗ TrainStation.java1Do not submit any other files, especially .class files. Any deviation from theserequirements may lead to lost marks• It does not matter whether or not you create a package to store all these classes. It is up toyou to decide whether you’d like to have a package or not.• You are given class templates to complete. You can only change the code of these classeswithin the methods containing the comments ”YOUR CODE GOES HERE”. You cannotchange any method headers unless a comment specifies you can, and even then, the onlychanges you can make are to add exception handling. If you change a method header, wemight not be able to grade you and you may receive a grade of zero. Read the comments inthe templates carefully.• Requests to evaluate the assignment manually shall not be entertained, so please make surethat you follow the instruction closely or your code may fail to pass the automatic tests.Note that for this assignment, you are NOT allowed to import any other class (including forexample ArrayList or LinkedList). The template code comes with two imports in the classTrainLine. You cannot change them nor add any new ones. Any failure to comply withthese rules will give you an automatic 0.• We have included with these instruction a tester class called TrainRide, which will help youtest your network. You are welcome to add more tests to it. This tester includes a networkof 15 stations over three lines, but your code can be evaluated on another network. We willalso release (later) a very light MiniTester, which is a mini version of the final tester usedto evaluate your assignment and will call every method of the assignment. If your code failsthose tests, it means that there is a mistake somewhere. Even if your code passes those tests,it may still contain some errors. We will test your code on a much more challenging set ofexamples. We therefore highly encourage you to modify the tester class and expand it.• You will automatically get 0 if your code does not compile.• Failure to comply with any of these rules will be penalized. If anything is unclear, it is up toyou to clarify it by asking either directly a TA during office hours, or on the discussion boardon Piazza.2Figure 1: A train network plan (left), before and after a shuffling (right)A Confusing Train RideDue to a significant increase in its student population, especially in its Machine Learning department,the single direct train going to Hogwarts has been replaced by a network of 15 stationsdistributed over three lines. However, this has revealed to not exactly be an improvement. Justlike the staircases within the castle, the stations get bored and, to fight this boredom, rearrangethemselves every 2 hours. However, luckily for you, the stations have not yet started switching lines;they always remain on the same line, but the order of the stations within a line gets shuffled.Your task is to implement this shuffling train network and simulate the trip of an unfortunatetraveler. You will be given Java templates to complete. Follow the instructions for each classclosely.Here are a few things you should know about the network:• Traveling between two stations takes an hour.• All stations are unique and only exist on one line.• Transfer is possible between specific stations of different lines, which are indicated by havingthe same name but followed by a unique letter, identifying a distinct platform. Those remaintwo distinct stations, but that offer the possibility of traveling from one to the other. Transferis always two-way; if it is possible to transfer from station A to B, then transfer is possiblefrom station B to A.• If transferring is possible, the passenger is forced to transfer, unless the passenger would betransferring to a station he just transferred from. for example, if I station from station London- A to London - B, your next step is not to transfer from London - B to London - A.3• Shuffling occurs every two hours. During a shuffling event, the order of the stations within aline changes. The left and right terminals might change. The transfer stations are part of theshuffling, but they stations they transfer to remain unaffected.• You can assume there is only one traveler on the network at a time, and that every line hasa single train which is magically waiting for the traveler at the appropriate transfer station.You should now be ready to start the assignment, which is divided in four classes, as follows:[0 points] First, you are given a class TrainStation. A TrainStation encodes the stations of thenetwork. A station is part of a TrainLine, which you can imagine as a doubly linked list. Ithas the following fields:• TrainStation left : the next station on the left• TrainStation right : the next station on the right• boolean rightTerminal : true if the station is at the right end of the line• boolean leftTerminal : true if the station is at the left end of the line• String name : the name of the station.• TrainLine line: the TrainLine this station belongs to.• boolean hasConnecti代写COMP 250作业、代做Java编程设计作业、代写Java语言作业、代做data structures作业 代写Pon: true if the train station connects to another line. This is theonly public field.• TrainStation transfersToStation : the station object on the other line, if this transferexists.• TrainLine transfersToLine : the line object you can transfer to at this station.All those fields, except hasConnection, are private. As such, you are provided with get andset methods for all the private fields. The class also comes with two constructors, as well asan equals method for comparing stations. Do not modify the TrainStation class.[65 points] You are also given a class TrainLine. A TrainLine contains stations that movearound. It has the following fields:• TrainStation leftTerminus : the terminal station on the left• TrainStation rightTerminus : the terminal station on the right• String lineName : the name of the line.• boolean goingRight: true if the train is going from the left to the right (assuming node0 is at the left, and the last node at the right). You can assume there is only one trainon the line which magically awaits for you at the transfer station, so the direction of theline is the direction of this train.• public TrainStation[] lineMap : an array of TrainStation which encodes the map ofthe line. in that array, the station at index 0 is the left-most station of the line.4A constructor is provided, as well as equals method and a helper class StationNotFoundException.You are also provided with a method toString which converts the lineMap to a String forprinting purposes. Finally, a function shuffleArray shuffles the lineMap for you.Your task is to implement the following methods:• public int getSize() : this method returns an integer equal to the number of stationson the line.• public TrainStation findStation(String name) : this method take as input thename of a station, and searches through the line to return the TrainStation of thisname. All station names are unique.– Iterate over the line until you find a station of the right name.– If the station is not found, throw a StationNotFoundException• public TrainStation getNext(TrainStation station) : takes as input a station andreturns the next station of the line.– There is only one train on the line, it always goes in the same direction, until it hitsa terminal station, then it turns around.– Use the goingRight field to know in which direction the train is going.– if the station is not on this line, throw a StationNotFoundException.– You cannot use the lineMap to find the next station.• public TrainStation travelOneStation(TrainStation current, TrainStation previous): takes as input the previous and the current station and returns the next station, butwhile also considering line transfers. Line transfers count as a station change and takethe same time as a standard move between stations. So if you are at a station that hasthe option of transferring, travelOnestation should return the station transferred to,and this should count as one time step of one hour.– Trains do not like passengers. If you have the opportunity to transfer, you must,unless transferring brings you back to the station you just arrived from (condemningyou to an eternal ping-pong between the two).– If a valid transfer is available, return the station you transferred to. Otherwise,return the next station on the usual path of the line, computed with getNext.– If the current argument to travelOneStation is not on this train line, throw aStationNotFoundException–• public TrainStation[] getLineArray() : returns an array of the train stations onthe line, in order from the left terminal (index 0 of the array) to the right terminal (lastindex of the array).• public void shuffleLine() : shuffles the station on the line.5– You are provided with a shuffleArray method, which takes as input an array ofTrainStations generated with getLineArray, and updated the lineMap to a shuffledversion of this array.– Once you generated this shuffled array, reorder the stations of the line so that theirorder matches that of the lineMap.– tips: remember to keep track of the terminal stations, and to update the TrainStationobjects.• public void sortLine() : sorts the stations of the line in increasing alphabeticalorder, and updates the lineMap. Note that for clarity, we make every station name inTrainRide start with a number. Numbers are included in the alphabetical comparison.You can use any of the algorithms covered in class, namely bubble sort, insertion sort,or selection sort. No matter what you use, you need to implement it yourself. Tip: youcan make a helper swap function to make your life easier.[35 points] You are also given a class TrainNetwork. A TrainNetwork contains an array of trainlines. You are asked to implement the following functions:• public TrainLine getLineByName(String lineName) : this method take as input thename of a Line and returns a line of that name, otherwise throws a LineNotFoundException(helper class provided).• public void dance() : shuffles all the lines using shuffleLine.• public void undance() : sorts all the lines using sortLine.• public int travel(String startStation, String startLine, String endStation,String endLine) : the key function of the program. It takes as input coordinates fordeparture and arrival and simulates a trip. Please follow the instructions closely.1. Obtain departure station and line objects from the name strings provided as parametersto the method. Store them in the variables curLine and curStation.2. Iterate over the train network starting at the departure station in the providedwhile loop, updating curStation and curLine . You can change the terminationcondition.3. Keep track of the number of stations visited. This number is equal to the numberof hours spent on the train. Assume line transfers always take an hour.4. Remember the network dances every two hours.5. At each iteration, keep track of the current station and the previous station. Tip:you do need to keep track of the previous station even if it is not obvious why.Remember how transferring works.6. at each iteration, check whether you have arrived at destination by comparing theobjective name to the name of the current station.67. Once you have reached the destination, or after 168 hours of traveling, stop iteratingand return an integer equal to the number of hours spent traveling.8. If the station cannot be found, assume you stayed on the train for a week beforegiving up, and return 168.9. Do not throw exceptions in this method.[0 points] You are also provided with a pre-written class TrainRide, which instantiates the differentobjects to a full train network (see the illustration). You can make modifications to thelines or itinerary to test out your code, but the contents of this class will not be graded.All methods of this assignment are connected and as such, a part of your grade willcome from methods that call other methods. This means that one method workingincorrectly can lead to a lot of lost marks. Make sure you test your assignment thoroughlyby trying the TrainRide, which is a syntactic and logical test of every method.Double-check that your results make sense given the rules stated in this handout.Good luck!7转自:http://www.3zuoye.com/contents/9/4814.html