In Java, two matrices are considered equal if they have the same dimensions and all corresponding elements are identical. In this article, we will explore how to check if two matrices are equal using a Java program. We will implement a class that handles matrix creation, input, and comparison.
Steps to Check Matrix Equality
- Define the Matrix Class: Create a class to represent matrices.
- Input Matrix Data: Use a method to read matrix data from the user.
- Compare Matrices: Implement a method to check if two matrices are equal.
- Display Results: Print the comparison result.
Example Code
Here is a complete Java program that implements the above steps:
import java.util.Scanner;
class Matrix {
private int[][] data;
private int rows;
private int cols;
// Constructor to initialize matrix dimensions
public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
this.data = new int[rows][cols];
}
// Method to read matrix data from user
public void readMatrix() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter elements of the matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] = scanner.nextInt();
}
}
}
// Method to check if two matrices are equal
public boolean isEqual(Matrix other) {
if (this.rows != other.rows || this.cols != other.cols) {
return false; // Different dimensions
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (this.data[i][j] != other.data[i][j]) {
return false; // Found unequal elements
}
}
}
return true; // Matrices are equal
}
// Method to print the matrix
public void printMatrix() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(data[i][j] + "\t");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input dimensions for the first matrix
System.out.print("Enter number of rows and columns for first matrix: ");
int rows1 = scanner.nextInt();
int cols1 = scanner.nextInt();
Matrix matrix1 = new Matrix(rows1, cols1);
matrix1.readMatrix();
// Input dimensions for the second matrix
System.out.print("Enter number of rows and columns for second matrix: ");
int rows2 = scanner.nextInt();
int cols2 = scanner.nextInt();
Matrix matrix2 = new Matrix(rows2, cols2);
matrix2.readMatrix();
// Print both matrices
System.out.println("First Matrix:");
matrix1.printMatrix();
System.out.println("Second Matrix:");
matrix2.printMatrix();
// Check if matrices are equal and display the result
if (matrix1.isEqual(matrix2)) {
System.out.println("Both matrices are equal.");
} else {
System.out.println("Both matrices are not equal.");
}
scanner.close();
}
}
Java
Copy
Explanation of the Code
- Matrix Class: The Matrix class contains a 2D array of data to store matrix elements, along with rows and cols to store its dimensions.
- Constructor: The constructor initializes the dimensions and allocates memory for the 2D array.
- readMatrix Method: This method prompts the user to enter the elements of the matrix.
- isEqual Method: This method checks if another Matrix object is equal by comparing dimensions and corresponding elements.
- printMatrix Method: This method prints the contents of the matrix in a formatted manner.
- Main Method: In the main method, we create two Matrix objects, read their data, print them, and check their equality.
Sample Output
When you run this program, it will prompt you for input:
Output
Conclusion
In this article, we demonstrated how to check whether two matrices are equal in Java by creating a simple Matrix class that encapsulates functionality for reading data, comparing matrices, and printing results. This example highlights basic concepts such as object-oriented programming, array manipulation, and user input handling in Java. You can further enhance this program by adding features like error handling or support for non-integer matrices.