Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will write a Java class called Matrix that has the following variables, constructors, and methods: 1. Declare two-dimensional matrix int [][] matrix, numRows, numCols

You will write a Java class called Matrix that has the following variables, constructors, and methods:

1. Declare two-dimensional matrix int [][] matrix, numRows, numCols to denote the number of rows and columns.

2. Non-default constructor public Matrix (int n, int m) that initializes matrix with number of rows equal to n and number of columns equal to m.

3. Method public Matrix add(Matrix mat) that adds the corresponding integer entries on this.matrix with values in mat and returns the resulting Matrix object. This operation succeeds only when the number of rows and columns of both matrices are equal, so make sure to check that before adding. If not, handle it via an appropriate exception. The result matrix is of the same dimensions as the ones added. (see Part A of attached pdf). What is the time-complexity of your method?

4. Method public Matrix transpose() OR public static Matrix transpose(Matrix mat) that returns the transpose of matrix mat. The transpose of a matrix M is a matrix M with number of rows and columns and the values interchanged as shown in the example in the pdf (Part E). What is the time-complexity of your method?

5. Method public Matrix subtract(Matrix mat) that subtracts the corresponding integer entries from this.matrix with values in mat and returns the resulting Matrix object. This operation succeeds only when the number of rows and columns of both matrices are equal, so make sure to check that before adding. If not, handle it via an appropriate exception. The result matrix is of the same dimensions as the ones in the subtraction operation. (see Part B of pdf). There is an easier way to do this, so think about it. What is the time-complexity of your method?

6. Method public Matrix scalarMult(int const) that returns a new Matrix object where each entry of this.matrix is multiplied by the value const. Again, see the example in the pdf to understand how it is done (Part C). What is the time-complexity of your method? What is the timecomplexity of your method?

7. Method public void printMatrix() that prints out the contents of this.matrix to a file matrix_output.txt . This file has on the first line Matrix is of dimension m rows by n columns, followed by the contents of the matrix starting from row 0, and then row 1, etc., each row of values on a separate line.

8. Method public static Matrix genMatrix(int m, int n) that creates a new matrix of size m x n whose [i][j]th entry is set equal to i+j. This can be used for your own testing purposes.

import java.util.Random;

public class Matrix {

private int[][] matrix;

private int row;

private int col;

public Matrix(int r, int c) {

row = value(r);

col = value(c);

// creating matrix

matrix = new int[row][col];

// calling function to fill matrix with random number in range [-10 10]

createMatrixAndFill();

}

public Matrix(int dimension) {

row = value(dimension);

col = row;

// creating matrix

matrix = new int[row][col];

createMatrixAndFill();

}

// function that takes row or column value and return after applying constraint

private int value(int num){

if(num<1)

num=1;

else if(num>5)

num = 5;

return num;

}

// function to fill matrix with random value in range [-10 10]

private void createMatrixAndFill(){

Random random = new Random();

for(int i=0; i

for(int j=0; j

matrix[i][j] = random.nextInt(21) -10;

}

public int getNumOfRows(){

return row;

}

public int getNumOfColumns(){

return col;

}

public int [][] getMatrxi(){

return matrix;

}

// transpose function

public void transpose(){

int transpose[][] = new int[col][row];

for ( int c = 0 ; c < row ; c++ )

{

for ( int d = 0 ; d < col ; d++ )

transpose[d][c] = matrix[c][d];

}

// now assigning transpose to matrix

matrix = transpose;

// changing row and col

int temp = row;

row = col;

col = temp;

}

public boolean add(Matrix m){

// if matrix can not be added

if(row != m.getNumOfRows() && col!=m.getNumOfColumns())

return false;

// getting matrix of m

int temp[][] = m.getMatrxi();

// adding corresponding elements of two matrix and storing matrix

for(int i=0; i

for(int j=0; j

matrix[i][j] = matrix[i][j] + temp[i][j];

return true;

}

public boolean subtract(Matrix m){

// if matrix can not be added

if(row != m.getNumOfRows() && col!=m.getNumOfColumns())

return false;

// getting matrix of m

int temp[][] = m.getMatrxi();

// subtracting corresponding elements of two matrix and storing matrix

for(int i=0; i

for(int j=0; j

matrix[i][j] = matrix[i][j] - temp[i][j];

return true;

}

public void multiply(int num){

for(int i=0; i

for(int j=0; j

matrix[i][j] = num*matrix[i][j];

}

@Override

public String toString() {

String s = "";

for(int i=0; i

for(int j=0; j

s = s+matrix[i][j]+" ";

}

s = s+" ";

}

return s;

}

}

############### Test Program #########

public class MatrixTest {

public static void main(String[] args) {

// creating a two matrix Object

Matrix m1 = new Matrix(6);

System.out.println("Row: "+m1.getNumOfRows()+", and Columns: "+m1.getNumOfColumns());

System.out.println(m1.toString());

System.out.println();

// Creating other Object

Matrix m2 = new Matrix(5, 5);

System.out.println("Row: "+m2.getNumOfRows()+", and Columns: "+m2.getNumOfColumns());

System.out.println(m2.toString());

System.out.println();

System.out.println("Addition of m1 + m2");

m1.add(m2);

System.out.println(m1.toString());

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Edward Sciore

2nd Edition

3030338355, 978-3030338350

More Books

Students also viewed these Databases questions

Question

Technology. Refer to Case

Answered: 1 week ago