special_forces

Untitled

Apr 21st, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1. package com.mycompany.matrix;
  2.  
  3. import com.mycompany.summatrixexception.*;
  4.  
  5. import java.lang.String;
  6. import java.lang.StringBuffer;
  7.  
  8. import mycompany.productmatrixexception.*;
  9.  
  10.     public class Matrix{
  11.         protected int row  = 0;
  12.         protected int column = 0;
  13.         protected int array[][];
  14.  
  15.         public Matrix (int row, int column){
  16.            
  17.             if(row < 0 || column < 0) throw new  RuntimeException("Went beyond the matrix");
  18.             this.row = row;
  19.             this.column = column;
  20.            
  21.                 if(row < 0 || column < 0) throw new  RuntimeException("Went beyond the matrix");
  22.                    array = new int[row][];
  23.                    for (int i = 0; i < row; i++){
  24.                       array[i] = new int[column];
  25.                    }
  26.                    this.row = row;
  27.                    this.column = column;
  28.                    for (int i = 0; i<this.row; i++){
  29.                       for (int j = 0; j<this.column; j++){
  30.                       this.array[i][j] = 0 ;            
  31.                       }
  32.                    }
  33.  
  34.         }
  35.        
  36.         public Matrix (){
  37.         }
  38.        
  39.         public  Matrix sum (Matrix a){
  40.             Matrix c = new Matrix(this.row, this.column);
  41.             if ((this.row != a.row) ||(this.column != a.column)) throw new  SumMatrixException("Multiplication is impossible!");
  42.             for (int i = 0; i<this.row; i++){
  43.                 for (int j = 0; j<this.column; j++){
  44.                  c.setElement(i,j,this.getElement(i,j)+a.getElement(i,j));
  45.                 //  c.array[i][j] = this.array[i][j] + a.array[i][j];
  46.                   System.out.println( c.getElement(i,j));
  47.                 }
  48.             }
  49.             return c;
  50.          }
  51.          public Matrix product(Matrix a){
  52.              Matrix c = new Matrix(this.row, this.column);
  53.              if (this.column != a.row) throw new  ProductMatrixException("Multiplication is impossible!");
  54.              for(int i = 0; i < this.row; i++){
  55.                  for(int j = 0; j < a.column; j++){
  56.                     for(int k = 0; k < this.column; k++)
  57.                     c.setElement(i,j,c.getElement(i,j)+this.getElement(i,k)*a.getElement(k,j));
  58.                        //c.array[i][j] += (this.array[i][k] * a.array[k][j]);
  59.                  }
  60.               }
  61.               return c;
  62.         }
  63.          
  64.  
  65.          
  66.          public void setElement(int row, int column, int value){
  67.              if((row < 0 || column < 0) && (row > this.row)||(column > this.row)) throw new  RuntimeException("Went beyond the matrix!");
  68.          this.array[row][column] = value;
  69.          }
  70.  
  71.          public int getElement(int row, int column){
  72.          if((row < 0 || column < 0) && (row > this.row)||(column > this.row)) throw new  RuntimeException("Went beyond the matrix!");
  73.              return array[row][column];
  74.          }
  75.          
  76.          public String toString(){
  77.              StringBuffer strBuffer = new StringBuffer();
  78.              for(int i = 0; i < this.row; i++){
  79.                  for(int j = 0; j < this.column; j++){
  80.                  strBuffer.append(this.getElement(i,j));
  81.                  strBuffer.append(" ");
  82.                  }
  83.              strBuffer.append("\n");
  84.             }
  85.             return strBuffer.toString();  
  86.          }      
  87.          
  88.          public boolean equals ( Object o ) {
  89.              if(this.hashCode() != o.hashCode())
  90.                 return false;
  91.              if ( o instanceof Matrix ) {
  92.                  Matrix a = (Matrix)o;
  93.                  if ((this.row != a.row) ||(this.column != a.column) ) {
  94.                      return false;  
  95.                  }
  96.                  for (int i = 0; i<this.row; i++){
  97.                        for (int j = 0; j<this.column; j++){
  98.                          if (this.getElement(i,j) != a.getElement(i,j)){
  99.                             return false;
  100.                          }            
  101.                        }
  102.                     }
  103.                  return true;
  104.              }
  105.              else
  106.                  return false;
  107.          }
  108.     }
  109.    
  110.  
  111.  
Advertisement
Add Comment
Please, Sign In to add comment