Equal Matrices Java Program | ISC Computer Science 2018 Theory

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:

123
245
356
Matrix A
123
245
356
Matrix B

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): parameterized 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.

import java.util.Scanner;
class EqMat{
    int a[][];
    int m;
    int n;
    public EqMat(int mm, int nn){
        m = mm;
        n = nn;
        a = new int[m][n];
    }
    public void readarray(){
        Scanner in = new Scanner(System.in);
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                a[i][j] = Integer.parseInt(in.nextLine());
            }
        }
    }
    public int check(EqMat P, EqMat Q){
        if(P.m != Q.m || P.n != Q.n)
            return 0;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(P.a[i][j] != Q.a[i][j])
                    return 0;
            }
        }
        return 1;
    }
    public void print(){
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                System.out.print(a[i][j] + "\t");
            }
            System.out.println();
        }
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Row of matrix A: ");
        int m1 = Integer.parseInt(in.nextLine());
        System.out.print("Column of matrix A: ");
        int n1 = Integer.parseInt(in.nextLine());
        EqMat A = new EqMat(m1, n1);
        System.out.print("Row of matrix B: ");
        int m2 = Integer.parseInt(in.nextLine());
        System.out.print("Column of matrix B: ");
        int n2 = Integer.parseInt(in.nextLine());
        EqMat B = new EqMat(m2, n2);
        System.out.println("Enter matrix A elements:");
        A.readarray();
        System.out.println("Enter matrix B elements:");
        B.readarray();
        System.out.println("Matrix A:");
        A.print();
        System.out.println("Matrix B:");
        B.print();
        if(A.check(A, B) == 1)
            System.out.println("Matrices are equal!");
        else
            System.out.println("Matrices are not equal.");
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *