This Test is open-book (course textbook) and open-notes (your own notes from this class) and open-laptop, but closed-neighbor. You may not communicate with anyone during the Test, other than the Instructor. There are a total of 100 points possible.
Consider the following (incomplete) program “Test1_1.java” that reads the number of rows and columns for a two-dimensional array, creates the array, initializes it with random values, and creates the transpose of the array.
import java.util.Scanner; public class Test1_1 { /** Main method */ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of rows (1-10): "); int rows = input.nextInt(); System.out.print("Enter the number of columns (1-10): "); int cols = input.nextInt(); // Create originalMatrix as two dimensional array int[][] originalMatrix = new int[rows][cols]; // Assign random values to originalMatrix for (int row = 0; row < originalMatrix.length; row++) for (int col = 0; col < originalMatrix[row].length; col++) { < sub>}< /sub> originalMatrix[row][col] = (int) (Math.random() * 10); // Print original matrix System.out.println("Original matrix:\n" + matrixToString(originalMatrix)); // Transpose matrix and print the result // REPLACE LINE BELOW if doing Part (d) without Part (e)! int[][] resultMatrix = transposeMatrix(originalMatrix); System.out.println("Transposed matrix:\n" + matrixToString(resultMatrix)); } /** The method for generating a String containing the contents of a matrix */ public static String matrixToString(int[][] matrix) { String out=""; for (int row = 0; row < matrix.length; row++) { for (int col = 0; col < matrix[row].length; col++) { < sub>}< /sub> out += matrix[row][col] + " "; < sub>}< /sub> out += "\n"; < sub>}< /sub> return out; /** The method for transposing a matrix */ // INSERT METHOD HERE for Part (e)!!! :) }
Take the provided source code, and make ANY FOUR (your choice) of the modifications listed below. If you choose to implement more than four of these modifications, you will receive normal credit for the four you do the best, plus a little bit of extra credit for each additional one of sufficient quality.
- Modify the code to provide the same general functionality, but without using “Scanner” or “System.out”.
- Modify the code to generate an error message if the user enters a number for rows or columns outside of the specified range.
- Modify the code to keep prompting the user to input the number of rows or columns until an integer in the specified range is entered.
- Write code to transpose the matrix. If you don't do part (e), then you may place this code in the main method.
- Write the missing user-defined method “transposeMatrix”. Include the code from part (d) within this method.
Sample Input/Output: