Question: TaskC:defineslideUp,slideDown,slideLeft,andslideRightmethods Wheneverwe slideup / down / left / right , empty cellmaybe changed, so is thenumber of elements incorrect placement. SoweneedtoupdatedatamembersemptyCellRow,emptyCellCol,andnumCorrectinthesemethods. 4 . 1 DefinemethodslideUp
TaskC:defineslideUp,slideDown,slideLeft,andslideRightmethods Wheneverwe slideupdownleftright empty cellmaybe changed, so is thenumber of elements incorrect placement. SoweneedtoupdatedatamembersemptyCellRow,emptyCellCol,andnumCorrectinthesemethods. DefinemethodslideUp Callelementunderneathacell itsdownstairneighbor. Iftheemptycellhasnodownstairneighbor,thatis,theemptycellisonthelastrowalready,ieemptyCellRow numRowsoremptyCellRownumRows thereisnothingtodoinslideupoperation.Returnto thecaller. panel col index rowindex displayresult col index rowindex Nowtheemptycellhasadownstairneighbor.Dothefollowinginslideupoperation: a If thedownstairneighborof the emptycell isat its correct locationbefore slidingup, that is call valueCorrectonthisneighbor, thereturnistrue, thenafterslidingup, thiscell isnot inthecorrect position,sonumCorrectisdecreasedby Beforeslidingupthecellswhereelementsareingoodpositionsareingreencolor: panel col index rowindex displayresult col index rowindex Afterslidingupthecellswhereelementsareingoodpositionsareingreencolor: panel col index rowindex displayresult col index rowindex b Slidinguptakesseveralsteps. i Downstair neighbor moves up and the empty cell moves down. That is swap element at the empty cell with its downstair neighbor. ii After the above swapping, the value of downstair neighbor resides in the previous empty cell. If after swapping, the element is in correct position, then numCorrect is increased by A To test whether an element is in correct position, we can use valueCorrect method, which takes row and column indices as parameters. B Observe that the old downstair neighbor takes up the position of the previous empty cell. Before we update the row and col indices for newly empty cell, the row index of old downstair neighbor is emptyCellRow and the column index is emptyCellCol, but the value panelemptyCellRowemptyCellCol is the value of old downstair neighbor after swapping. C That is call valueCorrect on emptyCellRow and emptyCellCol, check the return. If the return is true, then increase numCorrect by Before sliding up the cells where elements are in good positions are in green color: panel col index row index dis playre sult col index row index After sliding up the cells where elements are in good positions are in green color: panel col index row index dis playre sult col index row index iii. Update emptyCellRow but not emptyCellCol why for the current empty cell. c Call display method to display panel in tabular format after sliding upd Warning: you may opt to call getInfo method to update values for emptyCellRow, emptyCellCol, and numCorrect after swapping the previous empty cell with its downstair neighbor. This approach works but is not efficient. i Reason: method getInfo goes through every cell in panel to check. It is necessary when we rearrange elements of panel in method randomize. ii However, in slideUp method, only the empty cell and its downstair neighbor are rearranged, call getInfo method is overkill
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
