Page 346 - Computer science 868 Class 12
P. 346

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.
                Ans.  import java.util.*;
                    class EqMat
                    {
                     int a[][];
                     int m;
                     int n;
                     static 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++)
                     { System.out.println();
                     for (int j=0;j<n;j++)
                     System.out.print(a[i][j]+"\t");
                     }
                     }
                     public static void main(String[] args)
                     { EqMat ob1=new EqMat(3,3);
                     EqMat ob2=new EqMat(3,3);
                     System.out.println("enter nos for the Ist 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");
                     }
                     }




                344344  Touchpad Computer Science-XII
   341   342   343   344   345   346   347   348   349   350   351