Advertisement
d1i2p3a4k5

10.Interface matrix

Oct 18th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3. interface Matrix
  4. {
  5.     int m=3,n=3;
  6.     void accept();
  7.     void display();
  8.     void add();
  9.     void multiply();
  10. }
  11. class methods implements Matrix
  12. {
  13.     protected int i,j,k;
  14.     int a[][] = new int[m][n];
  15.     int b[][] = new int[m][n];
  16.     int c[][] = new int[m][n];
  17.     public void accept()
  18.     {
  19.         Scanner t = new Scanner(System.in);
  20.         System.out.println("enter elements in 1st matrix");
  21.         for(i=0;i<m;i++)
  22.         {  
  23.             for(j=0;j<n;j++)
  24.             {
  25.                 a[i][j] = t.nextInt();
  26.             }
  27.         }
  28.         System.out.println("enter elements in 2nd matrix");
  29.         for(i=0;i<m;i++)
  30.         {  
  31.             for(j=0;j<n;j++)
  32.             {
  33.                 b[i][j] = t.nextInt();
  34.             }
  35.         }
  36.     }
  37.     public void display()
  38.     {
  39.         System.out.println(" 1st Matrix is ");
  40.         System.out.println();
  41.         for(i=0;i<m;i++)
  42.         {
  43.             for(j=0;j<n;j++)
  44.             {
  45.                 System.out.print(a[i][j]+"  ");
  46.             }
  47.             System.out.println();
  48.         }
  49.         System.out.println(" 2nd Matrix is ");
  50.         System.out.println();
  51.         for(i=0;i<m;i++)
  52.         {
  53.             for(j=0;j<n;j++)
  54.             {
  55.                 System.out.print(b[i][j]+"  ");
  56.             }
  57.             System.out.println();
  58.         }
  59.     }
  60.     public void add()
  61.     {
  62.         for(i=0;i<m;i++)
  63.         {
  64.             for(j=0;j<n;j++)
  65.             {
  66.                 c[i][j] = a[i][j]+b[i][j];
  67.             }
  68.         }
  69.         System.out.println("Addition of two Matrix is ");
  70.         for(i=0;i<m;i++)
  71.         {
  72.             for(j=0;j<n;j++)
  73.             {
  74.                 System.out.print(c[i][j]+"  ");
  75.             }
  76.             System.out.println();
  77.         }
  78.     }
  79.     public void multiply()
  80.     {
  81.         for(i=0;i<m;i++)
  82.         {
  83.             for(j=0;j<n;j++)
  84.             {  
  85.                 c[i][j]=0;
  86.                 for(k=0;k<n;k++)
  87.                 {
  88.                     c[i][j] = c[i][j]+a[i][k]*b[k][j];
  89.                 }
  90.             }
  91.         }
  92.         System.out.println("Multiplication of two Matrix is ");
  93.         System.out.println();
  94.         for(i=0;i<m;i++)
  95.         {
  96.             for(j=0;j<n;j++)
  97.             {
  98.                 System.out.print(c[i][j]+"  ");
  99.             }
  100.             System.out.println();
  101.         }
  102.     }
  103. }
  104. class test extends methods
  105. {
  106.     public static void main(String args[])
  107.     {
  108.         methods xyz = new methods();
  109.         xyz.accept();
  110.         xyz.display();
  111.         xyz.add();
  112.         xyz.multiply();
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement