Question: Java Question: Addition to Code I already Wrote for Matrix Operations: The code I wrote For Simple Matrix Operations: Program description: Takes rows and columns
Java Question: Addition to Code I already Wrote for Matrix Operations:
The code I wrote For Simple Matrix Operations:
Program description: Takes rows and columns within range from user --> makes a matrix --> makes change to matrix (transpose, sum columns, reverse rows) as per user input. Valid input examples: 3,4 or 1,2 or 4,3 Enter Q (case insensitive) to exit the program.
--------------------------------------------------
import java.util.Scanner; //public class for matrix public class Matrix2 { int rows; int columns; int[][] matrix; int startVal=0; //constructor public Matrix2(int row, int col) { //initializing row and column value this.rows=row; this.columns=col; //initializing matrix with rows and columns matrix = new int[row][col]; //initializing the matrix for(int i=0;ifor(int j=0; jmatrix[i][j] = (i*10)+startVal+j; } } } //function prints the matrix new_matrix public void printMatrix(int[][] new_matrix){ int row = new_matrix.length; int col = new_matrix[0].length; for(int i=0; ifor(int j=0;jout.printf("%3d",new_matrix[i][j]); } System.out.println(); } } //function to perform transpose of the matrix public void transpose(){ int[][] new_mat = new int[columns][rows]; for(int i=0; irows; i++){ for(int j=0;jcolumns; j++){ new_mat[j][i] = matrix[i][j]; } } printMatrix(new_mat); } //function finds the sum of element of each column public void columnSum(){ for(int i=0; icolumns; i++){ int sum = 0; for(int j=0;jrows; j++){ sum += matrix[j][i]; } System.out.printf("%3d ", sum); } System.out.println(); } //function reverse the rows of matrix public void reverseRows(){ int[][] new_mat = new int[rows][columns]; for(int i=0; irows; i++){ for(int j=0;jcolumns; j++){ new_mat[i][j] = matrix[i][columns-j-1]; } } printMatrix(new_mat); } //main method public static void main(String[] args) { Scanner in = new Scanner(System.in); int row, col; //validating no of rows while(true){ System.out.print("Enter number of rows: "); row = in.nextInt(); if(row>0 && rowbreak; System.out.println("Invalid row size! Try again"); } //validating number of columns while(true){ System.out.print("Enter number of columns: "); col = in.nextInt(); if(col>0 && colbreak; System.out.println("Invalid column size! Try again"); } //creating object of class Matrix2 Matrix2 m1=new Matrix2(row,col); //printing the initial matrix System.out.println("Initial matrix"); m1.printMatrix(m1.matrix); //displaying menu and performing required operations char choice; do{ System.out.println(" T transpose"); System.out.println("C columnSum"); System.out.println("R reverseRows"); System.out.println("Q quit"); System.out.print("Your choice: "); choice = in.next().toLowerCase().charAt(0); switch(choice){ case 't': m1.transpose(); break; case 'c': m1.columnSum(); break; case 'r': m1.reverseRows(); break; case 'q': System.exit(0); default: System.out.println("Invalid choice! "); } }while(choice!='q'); } }---------------------------------------------------------------------------------------------------------------
Question:


options: System.out.printin("T transpose Rows become columns (and vice versa)") System.out.printin("C columnSum - Caclulate the sum of the values in each column") System.out.printin("R reverseRows Reverse all elements in every row of the matrix); / New options: System.out.println("P printMatrixPrint the original matrix") System.out.printin("AR addReverse Add reverse of matrix to original matrix"); System.out.printin("AN addNum-Add a number to each element of the original matrix" System.out.printin("Q quit Exit the program"); Option P Prints the original matrix. . You probably already have a method that does this, now you'll hook the method into the menu so a user can view the original matrix at any time, not just at the start of the program. . You should call this method from main like this: ml.printMatrix) Option 'AR Add the reverse of the original matrix to the original matrix. This involves adding the elements at the same position of each matrix. You already have a method to reverse a matrix, use that method here. You should call this method from main like this: arMat - m1.add (m1.reverseRows)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
