Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
7.4k views
in Computer by (79.3k points)

Two matrices are said to be equal if they have the same dimension and their corresponding elements are equal. 10 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

1 2 3
2 4 5
3 5 6

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 initialise 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.

1 Answer

+1 vote
by (76.4k points)
selected by
 
Best answer

import java. io. *;

class EqMat

{

int a [ ] [ ];

int m, n;

EqMat (int mm, int nn)

{

m = mm;

n = nn;

a = new int[m][n];

}

void readarray ( )

{

int i, j;

BufferedReader br = new BufferedReader (new InputStreamReader (System. in)); 

System. out. println ("Enter" + m + n + "values"); for (i = 0; i < m; i + +)

{

for (j = 0; j < n; j ++)

a [i] [j] = Integer. parseInt (br. readLine ());

}

}

int check (EqMat P, EqMat Q)

{

int i, j;

for (i = 0; i < m; i ++)

{

for (j = 0; j < n; j++)

if (P. a [i] [j] ! = Q. a [i] [j]) return (0);

}

return (1);

}

void print ( )

{

int i, j;

for (i = 0; i < m; i ++)

{

for (j = 0; j < n; j ++) System. out. print (a [i] [j]);

system. out. println ( ) ;

}

}

public static void main (String args [ ])

{

BufferedReader br = new BufferedReader (new InputStreamReader (System. in));

system. out. println ("Enter no. of rows of matrix");

int row = Integer. parseInt (br. readLine ( ));

system . out. println ("Enter no. of Columns of matrix ");

int col = Integer. parseInt (br. readLine ( )); 

EqMat Ob1 = new EqMat (row, col);

Ob1. readarray ( );

Ob1. print ( );

EqMat Ob2 = new EqMat(row, col);

Ob2. readarray ( );

Ob2. print ( );

EqMat Ob3 = new EqMat (row, col);

if (Ob3. check (Ob1, Ob2) = = 1)

system. out. println (" Matrices are equal"); 

else

system. out. println (" Matrices are not equal");

}

}

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...