Matrix Difference Class Program in Java and Python
ISC 2013 Question 10 matrix difference class solution with algorithm, explanation, Java program and simple Python program.
Question:
A class Matrix contains a two-dimensional integer array of order m x n. The maximum value possible for both m and n is 25. Design a class Matrix to find the difference of the matrices. The details of the members of the class are given below:
Specify the class Matrix, giving details of the constructor Matrix(int, int), void fillarray(), Matrix SubMat(Matrix A) and void display(). Define the main() function to create objects and call the functions accordingly to enable the task.
Algorithm:
Step 1: Start.
Step 2: Define a class named Matrix.
Step 3: Declare a two-dimensional integer array arr and two integer variables m and n.
Step 4: In the constructor, accept the number of rows and columns and assign them to m and n.
Step 5: Create the array arr with size m x n.
Step 6: In fillarray(), use nested loops to input every matrix element.
Step 7: In SubMat(Matrix A), create a new Matrix object to store the result.
Step 8: Use nested loops to subtract each element of the current object from the corresponding element of matrix A.
Step 9: Store each difference in the result matrix and return the result object.
Step 10: In display(), use nested loops to print all elements row by row.
Step 11: In main(), accept the order of the matrices.
Step 12: If rows or columns exceed 25, display Out of Range and go to Step 18.
Step 13: Create two matrix objects for input and one matrix object for the result.
Step 14: Input and display both matrices.
Step 15: Call SubMat() to find the difference of the matrices.
Step 16: Display the resultant matrix.
Step 17: Stop.
Explanation:
This question is mainly about object-oriented programming with a two-dimensional array. The class Matrix stores the matrix as an instance variable, so each object of the class has its own separate matrix. If two objects X and Y are created, each object has its own arr, m and n. This is why the subtraction method can work with two different matrix objects without using separate global arrays.
The constructor Matrix(int mm, int nn) initializes the order of the matrix. The values passed to the constructor are stored in m and n, and then the two-dimensional array is created. This ensures that every matrix object knows its own size. The fillarray() method uses nested loops because a matrix has rows and columns. The outer loop moves through the rows, while the inner loop moves through the columns of the current row.
The most important method is SubMat(Matrix A). According to the question, the current object has to be subtracted from the parameterized object. Therefore, if the method is called as X.SubMat(Y), matrix X is the current object and matrix Y is the parameter object. The result is calculated as Y - X, element by element. The expression A.arr[i][j] - this.arr[i][j] clearly shows this direction of subtraction.
A new Matrix object is created inside SubMat() to store the result. This is better than changing either of the original matrices, because the input matrices remain available for display or further processing. The display() method again uses nested loops to print the matrix row by row. In main(), the program checks that the number of rows and columns do not exceed 25, creates the required objects, fills both matrices, displays them and finally displays the difference matrix.
Java Program:
/**
* The class Matrix inputs two matrices as objects and subtracts
* the current matrix from the parameter matrix.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Theory 2013 Question 10
*/
import java.util.Scanner;
class Matrix
{
static Scanner sc = new Scanner(System.in);
int arr[][];
int m;
int n;
Matrix(int mm, int nn)
{
m = mm;
n = nn;
arr = new int[m][n];
}
void fillarray()
{
// Input every element of the matrix row by row.
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print("Enter Element at [" + i + "][" + j + "]: ");
arr[i][j] = sc.nextInt();
}
}
}
Matrix SubMat(Matrix A)
{
Matrix C = new Matrix(m, n);
/*
* this refers to the object that calls SubMat().
* The current matrix is subtracted from matrix A.
*/
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
C.arr[i][j] = A.arr[i][j] - this.arr[i][j];
}
}
return C;
}
void display()
{
// Display the matrix in rows and columns.
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
public static void main(String args[])
{
System.out.print("Enter the number of Rows: ");
int r = sc.nextInt();
System.out.print("Enter the number of Columns: ");
int c = sc.nextInt();
if(r > 25 || c > 25)
{
System.out.println("Out of Range");
}
else
{
Matrix X = new Matrix(r, c);
Matrix Y = new Matrix(r, c);
Matrix Z;
System.out.println("\nEnter the 1st Matrix");
X.fillarray();
System.out.println("\nEnter the 2nd Matrix");
Y.fillarray();
System.out.println("\nThe 1st Matrix");
X.display();
System.out.println("\nThe 2nd Matrix");
Y.display();
// Subtract matrix X from matrix Y.
Z = X.SubMat(Y);
System.out.println("\nThe Resultant Matrix");
Z.display();
}
}
}Equivalent Python Program:
class Matrix:
def __init__(self, mm, nn):
self.m = mm
self.n = nn
self.arr = []
# Create a matrix filled with zeros.
for i in range(0, self.m):
row = []
for j in range(0, self.n):
row.append(0)
self.arr.append(row)
def fillarray(self):
# Input every matrix element row by row.
for i in range(0, self.m):
for j in range(0, self.n):
self.arr[i][j] = int(input("Enter Element at [" + str(i) + "][" + str(j) + "]: "))
def SubMat(self, A):
C = Matrix(self.m, self.n)
# Subtract the current matrix from matrix A.
for i in range(0, self.m):
for j in range(0, self.n):
C.arr[i][j] = A.arr[i][j] - self.arr[i][j]
return C
def display(self):
# Display the matrix row by row.
for i in range(0, self.m):
for j in range(0, self.n):
print(self.arr[i][j], end="\t")
print()
r = int(input("Enter the number of Rows: "))
c = int(input("Enter the number of Columns: "))
if r > 25 or c > 25:
print("Out of Range")
else:
X = Matrix(r, c)
Y = Matrix(r, c)
print("\nEnter the 1st Matrix")
X.fillarray()
print("\nEnter the 2nd Matrix")
Y.fillarray()
print("\nThe 1st Matrix")
X.display()
print("\nThe 2nd Matrix")
Y.display()
Z = X.SubMat(Y)
print("\nThe Resultant Matrix")
Z.display()Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.