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 slideup/down/left/right, empty cellmaybe changed, so is thenumber of elements incorrect placement. SoweneedtoupdatedatamembersemptyCellRow,emptyCellCol,andnumCorrectinthesemethods. 4.1 DefinemethodslideUp 1. Callelementunderneathacell itsdownstairneighbor. 2. Iftheemptycellhasnodownstairneighbor,thatis,theemptycellisonthelastrowalready,ie,emptyCellRow +1>=numRowsoremptyCellRow==numRows-1, thereisnothingtodoinslideupoperation.Returnto thecaller. panel col index rowindex 012045113262897 displayresult col index rowindex 012045113262873.Nowtheemptycellhasadownstairneighbor.Dothefollowinginslideupoperation: (a) If thedownstairneighborof the emptycell isat its correct locationbefore slidingup, that is, call valueCorrectonthisneighbor, thereturnistrue, thenafterslidingup, thiscell isnot inthecorrect position,sonumCorrectisdecreasedby1. Beforeslidingup(thecellswhereelementsareingoodpositionsareingreencolor): panel col index rowindex 012045116922783 displayresult col index rowindex 01204511622783 Afterslidingup(thecellswhereelementsareingoodpositionsareingreencolor): panel col index rowindex 012045116922783 displayresult col index rowindex 01204511682273(b) Slidinguptakesseveralsteps. 19 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 1. 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 panel[emptyCellRow][emptyCellCol] 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 1. Before sliding up (the cells where elements are in good positions are in green color): panel col index row index 012013217892456 dis playre sult col index row index 01201321782456 After sliding up (the cells where elements are in good positions are in green color): panel col index row index 012013217892456 dis playre sult col index row index 01201321782456 iii. Update emptyCellRow but not emptyCellCol (why??) for the current empty cell. (c) Call display() method to display panel in tabular format after sliding up.(d) 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 re-arrange elements of panel in method randomize. ii. However, in slideUp method, only the empty cell and its downstair neighbor are re-arranged, call getInfo method is overkill

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!