Advertisement
Guest User

Untitled

a guest
Apr 12th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileReader;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5.  
  6.  
  7. public class Matrix {
  8.    
  9.     private int matrix[][];
  10.     private int n;
  11.     private int m;
  12.     File matrixFile = new File("Lab2_Matrix.txt");
  13.    
  14.     public Matrix(int n, int m) {        
  15.         this.n = n;
  16.         this.m = m;
  17.        
  18.         matrix = new int[n][];
  19.        
  20.         for(int i=0; i < n; i++){                
  21.             matrix[i] = new int[m];          
  22.         }
  23.     }
  24.    
  25.     public void setMatrixValue(int i, int j, int value) {//конкретная ячецка матрицы заполняется определенным значением
  26.         this.matrix[i][j] = value;
  27.     }
  28.    
  29.     public void setMatrixRand() {//Матрица заполняется случайными значениями от 0 до 10        
  30.         int a = 10;
  31.         for (int i = 0; i < this.getN(); i++) {
  32.             for (int j = 0; j < this.getM(); j++) {
  33.                     this.setMatrixValue(i, j, (int) (Math.random() * a));
  34.             }        
  35.         }
  36.     }
  37.    
  38.     public void output() { //вывод матрицы на экран;
  39.         System.out.println("");        
  40.         for (int i = 0; i < this.getN(); i++) {
  41.             for (int j = 0; j < this.getM(); j++) {
  42.                 System.out.print(this.getMatrix()[i][j] + "\t");              
  43.             }
  44.             System.out.println("");
  45.         }
  46.         System.out.println("");
  47.     }
  48.    
  49.     public Matrix add(Matrix ref) { //сумма двух матриц
  50.         if(this.getN() != ref.getN() || this.getM() != ref.getM()){
  51.             throw new ArithmeticException("Матрицы разного размера, их невозможно сложить");            
  52.         }
  53.        
  54.         Matrix result = new Matrix(getN(), getM());
  55.        
  56.         for (int i = 0; i < this.getN(); i++) {
  57.             for (int j = 0; j < this.getM(); j++) {
  58.                     result.setMatrixValue(i, j, this.getMatrix()[i][j] + ref.getMatrix()[i][j]);
  59.             }        
  60.         }
  61.                
  62.         return result;
  63.    }
  64.    
  65.      public boolean equal(Matrix ref) { //сравнение 2х матриц  
  66.         if(this.getN() != ref.getN() || this.getM() != ref.getM()){
  67.             throw new ArithmeticException("Матрицы разного размера, их невозможно сравнить");            
  68.         }
  69.        
  70.         for (int i = 0; i < this.getN(); i++) {
  71.             for (int j = 0; j < this.getM(); j++) {
  72.                     if (this.getMatrix()[i][j] != ref.getMatrix()[i][j]) {
  73.                         return false;
  74.                     };
  75.             }        
  76.         }        
  77.          return true;
  78.      }
  79.      
  80.     public void write() { //Записывает матрицу в текстовый файл        
  81.         try(FileWriter writer = new FileWriter(matrixFile)) {
  82.            
  83.            for (int i = 0; i < this.getN(); i++) {
  84.                 for (int j = 0; j < this.getM(); j++) {
  85.                     writer.write(String.valueOf(this.getMatrix()[i][j]));
  86.                     writer.append("\t");
  87.                 }
  88.                 writer.append("\n");
  89.             }  
  90.             writer.flush();
  91.             writer.close();
  92.         }
  93.         catch(IOException ex){            
  94.             System.out.println(ex.getMessage());
  95.         }
  96.     }
  97.    
  98.     public void read() { //Записывает матрицу в текстовый файл
  99.          try(FileReader reader = new FileReader(matrixFile))
  100.         {
  101.            // читаем посимвольно            
  102.             int c;
  103.             while((c=reader.read())!=-1){
  104.                System.out.print((char)c);  
  105.             }            
  106.            
  107.         }
  108.         catch(IOException ex){            
  109.             System.out.println(ex.getMessage());
  110.         }
  111.     }
  112.  
  113.     /**
  114.      * @return the matrix
  115.      */
  116.     public int[][] getMatrix() {
  117.         return matrix;
  118.     }
  119.  
  120.     /**
  121.      * @return the n
  122.      */
  123.     public int getN() {
  124.         return n;
  125.     }
  126.    
  127.     /**
  128.      * @return the m
  129.      */
  130.     public int getM() {
  131.         return m;
  132.     }
  133.    
  134.     /**
  135.      * @param n the n to set
  136.      */
  137.     public void setN(int n) {
  138.         this.n = n;
  139.     }
  140.  
  141.     /**
  142.      * @param m the m to set
  143.      */
  144.     public void setM(int m) {
  145.         this.m = m;
  146.     }
  147.      
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement