Kotlinlearncs.online LogoJava
Return to List

Test Writing: Array Transpose

Created By: Pranav Narayanan
/ Version: 2024.10.0

Write a function transpose that takes a rectangular int[][] matrix and returns its transpose.

The transpose of any matrix satisfies two properties. First, if the original matrix has m rows and n columns, the transpose has n rows and m columns. Second, if an element is in row i and column j in the original matrix, that element is in row j and column i in the transpose. For example:

| 1 2 3 |  should become  | 1 4 |
| 4 5 6 |                 | 2 5 |
                          | 3 6 |

Assert that the passed array is non-null. Otherwise, it is always rectangular. If the passed array is empty (0 rows or 0 columns), you should return an array with 0 rows.

Test Design Challenge

You're challenge is to write tests for this problem described above.

  • Provide a method named test that accepts no arguments and does not return a value.
  • If the implementation of the class described above is incorrect, your test method should throw an exception.
  • If it is correct, do not throw an exception.
  • You may want to use Java's assert method
Report a Problem

Attribution must link to this page: https://www.learncs.online/testtesting/java/array-transpose/[email protected]