Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a Java question. [65 points) You are also given a class TrainLine. A TrainLine contains stations that move around. It has the following

This is a Java question.
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
[65 points) You are also given a class TrainLine. A TrainLine contains stations that move around. It has the following fields: TrainStation left Terminus : the terminal station on the left TrainStation right Terminus : 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 node 0 is at the left, and the last node at the right). You can assume there is only one train on the line which magically awaits for you at the transfer station, so the direction of the line is the direction of this train. public TrainStation [] lineMap : an array of TrainStation which encodes the map of the line. in that array, the station at index 0 is the left-most station of the line. A 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 for printing 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 stations on the line. public TrainStation findStation (String name) : this method take as input the name of a station, and searches through the line to return the TrainStation of this name. 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 and returns the next station of the line. - There is only one train on the line, it always goes in the same direction, until it hits a 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, but while also considering line transfers. Line transfers count as a station change and take the same time as a standard move between stations. So if you are at a station that has the option of transferring, travelOnestation should return the station transferred to, and this should count as one time step of one hour. public TrainStation travelOneStation (TrainStation current, TrainStation previous) : takes as input the previous and the current station and returns the next station, but while also considering line transfers. Line transfers count as a station change and take the same time as a standard move between stations. So if you are at a station that has the 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 (condemning you 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 a StationNotFoundException public TrainStation [] getLineArray() : returns an array of the train stations on the line, in order from the left terminal (index 0 of the array) to the right terminal (last index of the array). This array is completely independent from the lineMap. The idea is to use this function to update the lineMap public void shuffleLine(): shuffles the station on the line. - You are provided with a shuffleArray method, which takes as input an array of Train Stations generated with getLineArray, and updates the lineMap to a shuffled version of this array - Once you updated the lineMap using the provided method, reorder the stations of the line so that their order matches that of the lineMap. - tips: remember to keep track of the terminal stations, and to update the TrainStation objects. public void sortLine() : sorts the stations of the line in increasing alphabetical order, and updates the lineMap (using getLineArray). Note that for clarity, we make every station name in TrainRide 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: you can make a helper swap function to make your life easier

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

socialist egalitarianism which resulted in wage levelling;

Answered: 1 week ago

Question

soyuznye (all-Union, controlling enterprises directly from Moscow);

Answered: 1 week ago