ISC Practicals and Theory - Matrix Multiplication (Using a Class)
Updated 20 Oct 2026
Matrix multiplication combines two two-dimensional arrays into a third one. In this version the work is divided among the methods of the class MatrixMult, and main only creates an object and calls them in order.
Definition
Two matrices $A$ and $B$ can be multiplied only when the number of columns of $A$ is equal to the number of rows of $B$. If $A$ has $m$ rows and $n$ columns, and $B$ has $p$ rows and $q$ columns, the product exists only when $n = p$. The product $C$ then has $m$ rows and $q$ columns.
Each element is the sum of products of one row of $A$ with one column of $B$:
C[i][j] = A[i][0] * B[0][j] + A[i][1] * B[1][j] + ... + A[i][n-1] * B[n-1][j]
If $A$ is $2 \times 3$ and $B$ is $3 \times 2$, then $C$ is $2 \times 2$. If $A$ is $2 \times 3$ and $B$ is $2 \times 2$, multiplication is not possible because $3 \ne 2$.
Algorithm
- In
main, read the rows and columns of matrix A (r1,c1) and of matrix B (r2,c2). - Create the object
obj. The constructorMatrixMult(int, int, int, int)copies them into the data membersm,n,pandq. - Call
isValid(). It returnstrueonly if all four sizes are greater than zero. If it returnsfalse, printInvalid sizeand stop. - Call
canMultiply(). It returnstruewhenn == p. If it returnsfalse, printMultiplication is not possibleand stop. - Call
readMatrices(sc). It creates the arraysa,bandcand reads the elements ofaandbwith nested loops. - Call
multiply():- The loop
iruns over the rows of A and the loopjover the columns of B. c[i][j]is set to 0, then the loopkfrom 0 ton - 1addsa[i][k] * b[k][j]to it.
- The loop
- Call
display()to print the product row by row.
Program
The constructor stores the sizes, isValid() and canMultiply() check them, readMatrices() reads both matrices, multiply() finds the product and display() prints it.
import java.util.Scanner;
public class MatrixMult
{
int a[][], b[][], c[][];
int m, n, p, q;
MatrixMult(int r1, int c1, int r2, int c2)
{
m = r1;
n = c1;
p = r2;
q = c2;
}
boolean isValid()
{
return m > 0 && n > 0 && p > 0 && q > 0;
}
boolean canMultiply()
{
return n == p;
}
void readMatrices(Scanner sc)
{
a = new int[m][n];
b = new int[p][q];
c = new int[m][q];
System.out.println("Enter the elements of matrix A:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter the elements of matrix B:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
b[i][j] = sc.nextInt();
}
}
}
void multiply()
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < q; j++)
{
c[i][j] = 0;
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
}
void display()
{
System.out.println("Product matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < q; j++)
{
System.out.print(c[i][j] + "\t");
}
System.out.println();
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows and columns of matrix A: ");
int r1 = sc.nextInt();
int c1 = sc.nextInt();
System.out.print("Enter rows and columns of matrix B: ");
int r2 = sc.nextInt();
int c2 = sc.nextInt();
MatrixMult obj = new MatrixMult(r1, c1, r2, c2);
if (!obj.isValid())
{
System.out.println("Invalid size");
}
else if (!obj.canMultiply())
{
System.out.println("Multiplication is not possible");
}
else
{
obj.readMatrices(sc);
obj.multiply();
obj.display();
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
| a | int[][] | Data member that stores the elements of matrix A |
| b | int[][] | Data member that stores the elements of matrix B |
| c | int[][] | Data member that stores the elements of the product matrix |
| m | int | Data member: number of rows of matrix A |
| n | int | Data member: number of columns of matrix A |
| p | int | Data member: number of rows of matrix B |
| q | int | Data member: number of columns of matrix B |
| r1 | int | Number of rows of A, read in main and passed to the constructor |
| c1 | int | Number of columns of A, read in main and passed to the constructor |
| r2 | int | Number of rows of B, read in main and passed to the constructor |
| c2 | int | Number of columns of B, read in main and passed to the constructor |
| sc | Scanner | Reads the sizes and the elements from the keyboard |
| i | int | Loop counter for the rows |
| j | int | Loop counter for the columns |
| k | int | Loop counter that runs along a row of A and a column of B |
| args | String[] | Command line arguments of main; not used |
| obj | MatrixMult | Object created in main to call the methods |
Sample input and output
Sample 1
Enter rows and columns of matrix A: 2 3
Enter rows and columns of matrix B: 3 2
Enter the elements of matrix A:
1 2 3
4 5 6
Enter the elements of matrix B:
7 8
9 10
11 12
Product matrix:
58 64
139 154 Sample 2
Enter rows and columns of matrix A: 2 3
Enter rows and columns of matrix B: 2 2
Multiplication is not possibleSample 3
Enter rows and columns of matrix A: 0 3
Enter rows and columns of matrix B: 3 2
Invalid sizeDry run
Take $A$ as $2 \times 3$ with rows 1 2 3 and 4 5 6, and $B$ as $3 \times 2$ with rows 7 8, 9 10 and 11 12. The constructor sets m = 2, n = 3, p = 3, q = 2. isValid() returns true and canMultiply() returns true because n == p. Then multiply() works as follows.
| i | j | k | Working | c[i][j] |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 + 1 x 7 | 7 |
| 0 | 0 | 1 | 7 + 2 x 9 | 25 |
| 0 | 0 | 2 | 25 + 3 x 11 | 58 |
| 0 | 1 | 0 | 0 + 1 x 8 | 8 |
| 0 | 1 | 1 | 8 + 2 x 10 | 28 |
| 0 | 1 | 2 | 28 + 3 x 12 | 64 |
| 1 | 0 | 0 | 0 + 4 x 7 | 28 |
| 1 | 0 | 1 | 28 + 5 x 9 | 73 |
| 1 | 0 | 2 | 73 + 6 x 11 | 139 |
| 1 | 1 | 0 | 0 + 4 x 8 | 32 |
| 1 | 1 | 1 | 32 + 5 x 10 | 82 |
| 1 | 1 | 2 | 82 + 6 x 12 | 154 |
display() prints the rows 58 64 and 139 154, the same as the first sample run.
Exam tip: Keep each method to one job: checking, reading, multiplying and displaying are separate methods, and main only creates the object and calls them.
Variations
- Square matrices "Accept two square matrices of size n x n, where n is greater than 1 and less than 10, and display their product."
- Transpose "Write a method to find the transpose of a matrix and display it."
- Diagonal sum "Display the product matrix and the sum of its diagonal elements."