In Java write three methods for the matrix class that corres
In Java write three methods for the matrix class that correspond to the three basic operations described below addition: two matrices must be the same size; add corresponding elements to create a new matrix that holds the sum of the values in the origanl matrices Scalar multiplication: all elements of a matrix are multiplied by a single number transpose: the transpose of a matrix is a new matrix in which the elements of each row of the original matrix becomes the corresponding column of the new matrix /** * Matrix class describes operations on a matrix of integers * @author Cate Sheller * @version 8/30/2011 */ public class Matrix { private int [][] grid; /** * default constructor: creates 3x3 matrix with random values * in the range 0..9 */ public Matrix() { grid = new int[3][3]; for(int x=0; x<3; x++) for(int y=0; y<3; y++) grid[x][y] = (int)(Math.random()*10); } /** * Creates matrix of specified size with random values 0..9 * @param size positive integer that represents the number of rows * and columns in the matrix */ public Matrix(int size) { grid = new int[size][size]; for(int x=0; x<size; x++) for(int y=0; y<size; y++) grid[x][y] = (int)(Math.random()*10); } /** * Creates a matrix of specified size with random values 0..9 * @param rows number of rows in matrix * @param columns number of columns int matrix */ public Matrix(int rows, int columns) { grid = new int[rows][columns]; for(int x=0; x<rows; x++) for(int y=0; y<columns; y++) grid[x][y] = (int)(Math.random()*10); } /** * @return String formatted as an m x n matrix of m rows and * n columns */ public String toString() { int rows = grid.length; int columns = grid[0].length; String table = new String(\"\"); for(int x=0; x<rows; x++) { table = table + \'|\' + \'\\t\'; for(int y=0; y<columns; y++) table = table + grid[x][y] + \'\\t\'; table = table + \'|\' + \'\ \'; } return table; } /** * @return true if number of rows equals number of columns */ public boolean isSquare() { return grid.length == grid[0].length; } /** * @param other another matrix to compare to this one * @return true if this matrix and the other have the same * number of rows and columns */ public boolean sameSize(Matrix other) { return grid.length == other.grid.length && grid[0].length == other.grid[0].length; } public static void main(String[] args) { Matrix m = new Matrix(); Matrix n = new Matrix((int)(Math.random()*5)+2); Matrix o = new Matrix(((int)(Math.random()*5)+2), ((int)(Math.random()*5)+2)); System.out.println (\"First matrix:\"); System.out.print(m); System.out.println (\"Second matrix:\"); System.out.println(n); System.out.println (\"Third matrix:\"); System.out.println(o); if(m.sameSize(n)) System.out.println(\"First two are the same size\"); else System.out.println(\"First two are not the same size\"); if(o.isSquare()) System.out.println(\"All three are square matrices\"); else System.out.println(\"Only first two are square matrices\"); } } Solution
Transpose of matrix in java:
public static double[][] addMatrix(double[][] m1, double[][] m2) {
double[][] result = new double[m1.length][m2[0].length];
for (int i = 0; i < m1.length; i++)
for (int j = 0; j < result.length; j++)
for (int k = 0; k < result[0].length; k++)
result[i][j] += m1[i][k] + m2[k][j];
return result;
}
Thank you.
| public static double[][] addMatrix(double[][] m1, double[][] m2) { |
