Question
A class Matrix contains a two dimensional integer array of order [m x n].The maximum value possible for both ‘m’ and ‘n’ is 25 . Design a class Matrix to find the difference of the two matrices . The details of the members of the class are given below:
Classname:Matrix
Data members/ instance variables:
arr[ ][ ]:stores the matrix element
m:integer to store the number of rows
n:integer to store the number of columns
Member functions:
Matrix(int mm,int nn): to initialize the size of the matrix m=mm and n=nn
void fillarray():to enter the elements of the matrix
Matrix SubMat(Matrix A):subtract the current object from the matrix of parameterized object and return the resulting object
void display():display the matrix elements
Specify the class Matrix giving details of the constructor(int,int ). void fillarray() ,Matrix SubMat(Matrix) and void display() . Also define a main() function to create an object and call the methods accordingly to enable the task.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class Matrix
{
Scanner sc=new Scanner(System.in);
int arr[][];
int m,n;
Matrix(int mm,int nn)
{
m=mm;
n=nn;
arr=new int[m][n];
}
void fillarray()
{
for(int i=0;i< m;i++)
{
for(int j=0;j< n;j++)
{
arr[i][j]=sc.nextInt();
}
}
}
Matrix SubMat(Matrix A)
{
Matrix B=new Matrix(m,n);
for(int i=0;i< m;i++)
{
for(int j=0;j< n;j++)
{
B.arr[i][j]=A.arr[i][j]-arr[i][j] ;
}
}
return B;
}
void display()
{
for(int i=0;i< m;i++)
{
System.out.println();
for(int j=0;j< n;j++)
System.out.print(arr[i][j]+"\t");
}
}
public static void main()
{
Scanner sc1=new Scanner(System.in);
System.out.println("Size of array:");
int x=sc1.nextInt();
int y=sc1.nextInt();
if(x>25 ||y>25)
{
System.out.println("Invalid Size" );
}
else
{
Matrix P=new Matrix(x,y);
Matrix Q=new Matrix(x,y);
Matrix R=new Matrix(x,y);
System.out.println("ENTER ELEMENTS IN MATRIX P");
P.fillarray();
System.out.println("ENTER ELEMENTS IN MATRIX Q");
Q.fillarray();
System.out.println("MATRIX P - MATRIX Q");
R=Q.SubMat(P);
R.display();
}
}
}
Coding Store
Sale
![](https://www.keepkoding.com/wp-content/uploads/2022/02/2.png)
ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale
![](https://www.keepkoding.com/wp-content/uploads/edd/2022/02/ICSE-QUESTION-PAPER-WITH-SOLUTION-PROGRAMMING-ONLY.png)