讲解:COMP 250、Java、Java、data structuresPython|Web

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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 222,252评论 6 516
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,886评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,814评论 0 361
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,869评论 1 299
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,888评论 6 398
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,475评论 1 312
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,010评论 3 422
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,924评论 0 277
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,469评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,552评论 3 342
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,680评论 1 353
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,362评论 5 351
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,037评论 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,519评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,621评论 1 274
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,099评论 3 378
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,691评论 2 361

推荐阅读更多精彩内容