Java Program to Perform Matrix Operation | IT2305 - Java Programming Lab


AIM:
To write a Java program to perform Matrix operation in IT2305 - Java Programming Laboratory.

SOURCE CODE:
import java.util.*;
import java.io.*;
class Primen
{
    public static void main(String args[])
   {
      int i,j,m,n,a[][],b[][],c[][];
      System.out.println("Enter rows and columns:");
  Scanner sin=new Scanner(System.in);
 m=sin.nextInt();
  n=sin.nextInt();
  a=new int[m][n];
  b=new int[m][n];
  c=new int[m][n];
   System.out.println("Enter the first matrix:");
  for(i=0;i<a.length;i++)
  {

   for(j=0;j<a[i].length;j++)
   {
     a[i][j]=sin.nextInt();
   }
}
System.out.println("Enter the second  matrix:");
for(i=0;i<b.length;i++)
  {
   for(j=0;j<b[i].length;j++)
   {
     b[i][j]=sin.nextInt();
   }
 }
 for(i=0;i<c.length;i++)
  {
   for(j=0;j<c[i].length;j++)
   {
     c[i][j]=a[i][j]+b[i][j];

   }
 }
System.out.println("Matrix A");
for(int[]row:a)
{
  for(int x:row)
 {
   System.out.print(x);
  System.out.print("\t");
}
System.out.print("\n");
}
System.out.println("Matrix B");

for(int[]row:b)
{
  for(int x:row)
  {
   System.out.print(x);
  System.out.print("\t");
}
System.out.print("\n");
}
System.out.println("Resultant matrix:");
for(int[]row:c)
{
  for(int x:row)
 System.out.print(x);
  System.out.print("\t");
}
System.out.print("\n");
}
}

[08cs089@mepcolinux:~/java]cat MatrixOperation.java
import java.util.*;
class Matrix
{
        int a[][],m,n;
        public Matrix()
        {
                a=new int[0][0];
        }
        public Matrix(int x)
        {
                m=n=x;
                a=new int[m][];
                for(int i=0;i<a.length;i++)
                        a[i]=new int[x];
        }
        public Matrix(int x,int y)
        {
                m=x;
                n=y;
                a=new int[m][];
                for(int i=0;i<a.length;i++)
                        a[i]=new int[n];
        }
      public Matrix(Matrix m1)
        {
                m=m1.a.length;
                a=new int[m][];
                for(int i=0;i<a.length;i++)
                        a[i]=new int[m1.a[i].length];
                for(int i=0;i<a.length;i++)
                        a[i]=Arrays.copyOf(m1.a[i],m1.a[i].length);
        }
        public void getData()
        {
                Scanner sin=new Scanner(System.in);
                System.out.println("Enter the Matrix value:");
                for(int i=0;i<a.length;i++)
                        for(int j=0;j<a[i].length;j++)
                                a[i][j]=sin.nextInt();
        }
        public void displayMatrix()
        {
                System.out.println(Arrays.deepToString(a));
        }
}
public class MatrixOperation
{
        public static void main(String args[])
        {
                Matrix m1;
                int m,n;
                Scanner sin=new Scanner(System.in);
                System.out.print("Enter the no of rows and columns:");
                m=sin.nextInt();
                n=sin.nextInt();
                m1=new Matrix(m,n);
                m1.getData();
                m1.displayMatrix();
                System.out.println("After calling one parameter matrix:");
                Matrix m2=new Matrix(m);
                m2.getData();
                m2.displayMatrix();
                System.out.println("After calling copy constructor");
                Matrix m3=new Matrix(m1);
                m3.displayMatrix();
        }
}
Previous
Next Post »

Still not found what you are looking for? Try again here.