Create a class called 'Matrix' containing constructor that initializes the number of rows and number of columns of a new Matrix object.
PROGRAM CODE WITH OUTPUT
Create a class called 'Matrix' containing constructor that initializes the number of rows and number of columns of a new Matrix object.
The Matrix class has the following information:
1 - number of rows of matrix
2 - number of columns of matrix
3 - elements of matrix in the form of 2D array.
The Matrix class has methods for each of the following :
1.Get the number of rows
2.Get the number of columns.3.Set the elements of the matrix at a given position (i , j)
4.Adding two matrices .
If the matrices are not addable, "Matrix can not be added will displayed.
5.Multplying the two matrices.
OUTPUT Screenshot
Program Code
import java.io.*;
import java.util.*;
class Matrix {
int row;
int col;
int[][] a;
public Matrix(int r, int c)
{
row = r;
col = c;
a = new int[row][col];
}
public int getrow()
{
return row;
}
public int getcol()
{
return col;
}
public int getelement(int r, int c)
{
return a[r][c];
}
public void setelement(int r, int c, int value)
{
a[r][c] = value;
}
public static Matrix add(Matrix a, Matrix b)
{
if((a.row != b.row) || (a.col != b.col))
{
System.out.println("Matrices can not be added");
return new Matrix(0,0);
}
else
{
Matrix m = new Matrix(a.row,a.col);
for(int i = 0;i<m.row;i++)
{
for(int j = 0;j<m.col;j++)
{
m.setelement(i,j,(a.getelement(i,j)+b.getelement(i,j)));
}
}
return m;
}
}
public static Matrix mult(Matrix a, Matrix b)
{
Matrix m = new Matrix(a.row,b.col);
for(int i = 0;i<a.row;i++)
{
for(int j = 0;j<b.col;j++)
{
int s = 0;
for(int k = 0;k<a.col;k++)
{
s = s+(a.getelement(i,k)*b.getelement(k,j));
}
m.setelement(i,j,s);
}
}
return m;
}
public void printmat()
{
for(int i = 0;i<row;i++)
{
for(int j = 0;j<col;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println("");
}
}
}
Comments
Post a Comment