iven 2D 273 367 293 0 150 680 654 358 681 593 65 465 647 151 432 612 765 54 492 502 412 265 665 51 135 prted row 2D 0 150 273 293 367 358 593 654 680 681 51 65 135 465 647 54 151 432 612 765 265 412 492 502 665 ranpose of 2D o 358 51 54 265
You can see in the above display, we first sort each row of the 2D array; we then take the transpose of a two D array, i.e., all the row elements becoming the column elements; we then sort each row of the 2D again. If you read the final array, each row is sorted; each column is also sorted. The smallest element obviously is the 1st element of the two D array and the last element is the largest element of a two D array.
Let us now look at the following UML diagram: (Note that additional methods are allowed; proposed methods and instance variable cannot be changed)
Main method firstly constructs a 2D array of certain sizes and then construct a TwoD object and drive the task according to the above runtime interactions and displays.
TwoD class has only one instance variable which is a two D array of numbers ( int or double). The constructor must do some “deep” copying. A copy constructor. The other three methods are obvious in definition: to sort each row, to rotate the 2D array (i.e., change 1st row to 1st column, 2nd row to 2nd column etc.); and to display the 2D array (explore the use of nested enhanced for loops).
Algorithm:
- Start
- Implement construct2D() to read a 2d array
- Create a class TwoD with Attributes a 2d array 'a'
- Implement constructor to Initialize the data
- Implement copy constructor
- Implement sortRow() to sort each of the 2d array using Arrays.sort() method
- Implement transpose() method, to transpose the the 2d array
- Implement display() method to display the 2d array
- Inside the main method, create objects of TwoD class and call methods accordingly
- Stop
Step by step
Solved in 4 steps with 2 images