Question
Two matrices are said to be equal if they have the same dimension and their corresponding
elements are equal.
For example, the two matrices A and B given below are equal:
Matrix A
1 2 3
2 4 5
3 5 6
Matrix B
27 173 5
21 6 624
5 321 49
Design a class EqMat to check if two matrices are equal or not. Assume that the two matrices have the same dimension.
Some of the members of the class are given below:
Class name:EqMat
Data members/instance variables:
a[ ][ ]:to store integer elements
m:to store the number of rows
n:to store the number of columns
Member functions/methods:
EqMat(int mm, int nn): parameterised constructor to initialize the data members m = mm and n = nn
void readarray( ):to enter elements in the array
int check(EqMat P, EqMat Q):checks if the parameterized objects P and Q are equal and returns 1 if true, otherwise returns 0
void print( ):displays the array elements
Define the class EqMat giving details of the constructor( ), void readarray( ), int check(EqMat, EqMat) and void print( ). Define the main( ) function to create objects and call the functions 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 EqMat
{
int a[][];
int m;
int n;
Scanner sc=new Scanner(System.in) ;
EqMat(int mm,int nn)
{
m=mm ;
n=nn;
a=new int[m][n];
}
void readarray()
{
System.out.println("enter" + (m*n) + "elements" );
for (int i=0;i< m;i++)
{
for (int j=0 ;j< n ;j++)
{
a[i][j]=sc.nextInt();
}
}
}
int check(EqMat P,EqMat Q)
{
for (int i=0;i< P.m;i++)
{
for (int j=0 ;j < P.n ;j++)
{
if (P.a[i][j]!=Q. a[i][j])
{
return 0;
}
}
}
return 1;
}
void print()
{
for (int i=0;i< m;i++)
{
for (int j=0 ;j < n ;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static void main()
{
Scanner sc1=new Scanner(System.in);
System.out.println("ENTER NUMBER OF ROWS OF MATRIX");
int row=sc1.nextInt();
System.out.println("ENTER NUMBER OF COLUMNS OF MATRIX");
int col=sc1.nextInt();
EqMat ob1=new EqMat(row,col) ;
EqMat ob2=new EqMat(row,col) ;
System.out.println("enter nos for the 1st Matrix");
ob1.readarray();
System.out.println("enter nos for the 2nd Matrix");
ob2.readarray();
if(ob1.check(ob1,ob2)==1)
{
System.out.println("Equal Matrix") ;
ob1.print();
ob2.print();
}
else
{
System.out.println("not equal");
}
}
}
Coding Store
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale
