#### 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:

**Class name : Matrix**

**Data members/instance variables:**

arr[ ][ ] : stores the matrix element

m : integer to store the number of rows

n : integer to store the number of columns

**Member functions:**

**Matrix(int mm, int nn) :** to initialize the size of the matrix m = mm and n = nn

**void fillarray() :** to enter the elements of the matrix

**Matrix SubMat(Matrix A) :** subtract the current object from the matrix of parameterized object and return the resulting object

**void display() :** display the matrix elements

Specify the class **Matrix** giving details of the **constructor(int,int), void fillarray(), Matrix SubMat(Matrix A)** and **void display()**. Define the **main()** function to create objects and call the function accordingly to enable the task.

#### Programming Code:

/** * The class Matrix inputs 2 Matrices as objects and subtracts the first from the second matrix * @author : www.javaforschool.com * @Program Type : BlueJ Program - Java * @Question Year : ISC Theory 2013 Question 10 */ import java.io.*; class Matrix { static BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); int arr[][]; int m,n; Matrix(int mm, int nn) //parameterised constructor { m=mm; n=nn; arr=new int[m][n]; } void fillarray()throws IOException //function for inputting the values in the Matrix { 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]=Integer.parseInt(br.readLine()); } } } Matrix SubMat(Matrix A) //function for subtractng the calling Object (Matrix) from the Matrix A { Matrix C=new Matrix(m,n); //Object for storing the result 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]; /* keyword 'this' is referring to the object which will call the function SubMat(Matrix) */ } } return C; } void display() //function for displaying the Matrix { 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[])throws IOException { System.out.print("Enter the number of Rows: "); //Inputting Number of Rows int r=Integer.parseInt(br.readLine()); System.out.print("Enter the number of Columns: "); //Inputting Number of Columns int c=Integer.parseInt(br.readLine()); if(r>25 || c>25) System.out.println("Out of Range"); else { Matrix X=new Matrix(r,c); //The 1st Matrix Matrix Y=new Matrix(r,c); //The 2nd Matrix Matrix Z=new Matrix(r,c); //The Resultant Matrix //Inputting 1st Matrix by calling the function fillarray() System.out.println("\nEnter the 1st Matrix"); X.fillarray(); //Inputting 2nd Matrix by calling the function fillarray() System.out.println("\nEnter the 2nd Matrix"); Y.fillarray(); //Displaying 1st Matrix by calling the function display() System.out.println("\nThe 1st Matrix"); X.display(); //Displaying 2nd Matrix by calling the function display() System.out.println("\nThe 2nd Matrix"); Y.display(); /*Calling the function SubMat() using the object 'X' in order to subtract the values of Matrix X from Matrix Y*/ Z=X.SubMat(Y); //Displaying the resultant Matrix by calling the function display() System.out.println("\nThe Resultant Matrix"); Z.display(); } } }

#### Output:

Enter the number of Rows: 2

Enter the number of Columns: 3

Enter the 1st Matrix

Enter Element at [0][0] : 15

Enter Element at [0][1] : 2

Enter Element at [0][2] : 4

Enter Element at [1][0] : 53

Enter Element at [1][1] : 19

Enter Element at [1][2] : 37

Enter the 2nd Matrix

Enter Element at [0][0] : 11

Enter Element at [0][1] : 50

Enter Element at [0][2] : 3

Enter Element at [1][0] : 9

Enter Element at [1][1] : 45

Enter Element at [1][2] : 8

The 1st Matrix

15 2 4

53 19 37

The 2nd Matrix

11 50 3

9 45 8

The Resultant Matrix

-4 48 -1

-44 26 -29

## One comment

Pingback: Download ISC 2013 Question Papers (Theory + Practical) | Java For School