Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- interface Matrix
- {
- int m=3,n=3;
- void accept();
- void display();
- void add();
- void multiply();
- }
- class methods implements Matrix
- {
- protected int i,j,k;
- int a[][] = new int[m][n];
- int b[][] = new int[m][n];
- int c[][] = new int[m][n];
- public void accept()
- {
- Scanner t = new Scanner(System.in);
- System.out.println("enter elements in 1st matrix");
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- a[i][j] = t.nextInt();
- }
- }
- System.out.println("enter elements in 2nd matrix");
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- b[i][j] = t.nextInt();
- }
- }
- }
- public void display()
- {
- System.out.println(" 1st Matrix is ");
- System.out.println();
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- System.out.print(a[i][j]+" ");
- }
- System.out.println();
- }
- System.out.println(" 2nd Matrix is ");
- System.out.println();
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- System.out.print(b[i][j]+" ");
- }
- System.out.println();
- }
- }
- public void add()
- {
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- c[i][j] = a[i][j]+b[i][j];
- }
- }
- System.out.println("Addition of two Matrix is ");
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- System.out.print(c[i][j]+" ");
- }
- System.out.println();
- }
- }
- public void multiply()
- {
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- c[i][j]=0;
- for(k=0;k<n;k++)
- {
- c[i][j] = c[i][j]+a[i][k]*b[k][j];
- }
- }
- }
- System.out.println("Multiplication of two Matrix is ");
- System.out.println();
- for(i=0;i<m;i++)
- {
- for(j=0;j<n;j++)
- {
- System.out.print(c[i][j]+" ");
- }
- System.out.println();
- }
- }
- }
- class test extends methods
- {
- public static void main(String args[])
- {
- methods xyz = new methods();
- xyz.accept();
- xyz.display();
- xyz.add();
- xyz.multiply();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement