| 1. |
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 A123245356Matrix B123245356Design 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 nameEqMatData members/instance variables a[ ] [ ]to store integer elementsmto store the number of rowsnto store the number of columnsMember functions / methods :EqMat(int mm, int nn) parameterised constructor to initialise the data members m = mm and n = nnvoid readarray ( )to enter elements in the arrayint check (EqMat P, EqMat Q)checks if the parameterized objects P and Q are equal and returns 1 if true, otherwise returns 0void 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. |
|
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"); } } |
|