Matrix Multiplication in Java

Write a program to multiply two matrices in Java, using the following specifications:

Class name: Multiply
Data members:
int row: To store the number of rows.
int col: To store the number of columns.
int m[][]: To store the matrix elements.
Methods:
Multiply(int, int): Parameterized constructor to set the number of rows and columns, and create the matrix.
void input(): To input the matrix elements from the user.
Multiply multiply(Multiply): To multiply the matrices of the current object and the parameterized object, and return the object that will contain the product of the two matrices.
void display(): To display the matrix.

Also include the main() method to create the objects of the class and call the methods accordingly to enable the task.

Program:

import java.io.*;
class Multiply{
    int m[][];
    int row;
    int col;
    public Multiply(int r, int c){
        row = r;
        col = c;
        m = new int[row][col];
    }
    public void input()throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++)
                m[i][j] = Integer.parseInt(br.readLine());
        }
    }
    public Multiply multiply(Multiply obj){
        Multiply temp = new Multiply(this.row, obj.col);
        if(this.col != obj.row)
            return null;
        for(int i = 0; i < this.row; i++){
            for(int j = 0; j < obj.col; j++){
                for(int k = 0; k < this.col; k++){
                    temp.m[i][j] += this.m[i][k] * obj.m[k][j];
                }
            }
        }
        return temp;
    }
    public void display(){
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++)
                System.out.print(m[i][j] + "\t");
            System.out.println();
        }
    }
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Row 1: ");
        int row1 = Integer.parseInt(br.readLine());
        System.out.print("Column 1: ");
        int col1 = Integer.parseInt(br.readLine());
        System.out.print("Row 2: ");
        int row2 = Integer.parseInt(br.readLine());
        System.out.print("Column 2: ");
        int col2 = Integer.parseInt(br.readLine());
        Multiply obj1 = new Multiply(row1, col1);
        Multiply obj2 = new Multiply(row2, col2);
        Multiply obj3 = new Multiply(row1, col2);
        System.out.println("Enter matrix 1 elements: ");
        obj1.input();
        System.out.println("Enter matrix 2 elements: ");
        obj2.input();
        obj3 = obj1.multiply(obj2);
        if(obj3 == null)
            System.out.println("Multiplication not possible.");
        else
            obj3.display();
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.