Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class Matrix {
- private int matrix[][];
- private int n;
- private int m;
- File matrixFile = new File("Lab2_Matrix.txt");
- public Matrix(int n, int m) {
- this.n = n;
- this.m = m;
- matrix = new int[n][];
- for(int i=0; i < n; i++){
- matrix[i] = new int[m];
- }
- }
- public void setMatrixValue(int i, int j, int value) {//конкретная ячецка матрицы заполняется определенным значением
- this.matrix[i][j] = value;
- }
- public void setMatrixRand() {//Матрица заполняется случайными значениями от 0 до 10
- int a = 10;
- for (int i = 0; i < this.getN(); i++) {
- for (int j = 0; j < this.getM(); j++) {
- this.setMatrixValue(i, j, (int) (Math.random() * a));
- }
- }
- }
- public void output() { //вывод матрицы на экран;
- System.out.println("");
- for (int i = 0; i < this.getN(); i++) {
- for (int j = 0; j < this.getM(); j++) {
- System.out.print(this.getMatrix()[i][j] + "\t");
- }
- System.out.println("");
- }
- System.out.println("");
- }
- public Matrix add(Matrix ref) { //сумма двух матриц
- if(this.getN() != ref.getN() || this.getM() != ref.getM()){
- throw new ArithmeticException("Матрицы разного размера, их невозможно сложить");
- }
- Matrix result = new Matrix(getN(), getM());
- for (int i = 0; i < this.getN(); i++) {
- for (int j = 0; j < this.getM(); j++) {
- result.setMatrixValue(i, j, this.getMatrix()[i][j] + ref.getMatrix()[i][j]);
- }
- }
- return result;
- }
- public boolean equal(Matrix ref) { //сравнение 2х матриц
- if(this.getN() != ref.getN() || this.getM() != ref.getM()){
- throw new ArithmeticException("Матрицы разного размера, их невозможно сравнить");
- }
- for (int i = 0; i < this.getN(); i++) {
- for (int j = 0; j < this.getM(); j++) {
- if (this.getMatrix()[i][j] != ref.getMatrix()[i][j]) {
- return false;
- };
- }
- }
- return true;
- }
- public void write() { //Записывает матрицу в текстовый файл
- try(FileWriter writer = new FileWriter(matrixFile)) {
- for (int i = 0; i < this.getN(); i++) {
- for (int j = 0; j < this.getM(); j++) {
- writer.write(String.valueOf(this.getMatrix()[i][j]));
- writer.append("\t");
- }
- writer.append("\n");
- }
- writer.flush();
- writer.close();
- }
- catch(IOException ex){
- System.out.println(ex.getMessage());
- }
- }
- public void read() { //Записывает матрицу в текстовый файл
- try(FileReader reader = new FileReader(matrixFile))
- {
- // читаем посимвольно
- int c;
- while((c=reader.read())!=-1){
- System.out.print((char)c);
- }
- }
- catch(IOException ex){
- System.out.println(ex.getMessage());
- }
- }
- /**
- * @return the matrix
- */
- public int[][] getMatrix() {
- return matrix;
- }
- /**
- * @return the n
- */
- public int getN() {
- return n;
- }
- /**
- * @return the m
- */
- public int getM() {
- return m;
- }
- /**
- * @param n the n to set
- */
- public void setN(int n) {
- this.n = n;
- }
- /**
- * @param m the m to set
- */
- public void setM(int m) {
- this.m = m;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement