2017-01-15 2 views
2

나는 stackoverflow에 게시 할 수 있도록 자바로 작성된 Matrix 클래스를 생성하는 데 도움이 필요했습니다. 결국 나는 그것을 알아 냈다. 이 질문을 삭제할 수 없기 때문에 필자는 완성 된 Matrix Class를 온라인으로 설정하기로 결정했습니다. 이걸 가지고 뭔가를 가져 가려고한다면 너 자신의 것을 만들어 보라.자바의 매트릭스 클래스

import java.util.Scanner; 
public class Matrix{ 

//State Variables: Private state varibles were created so that they could not be accidently accessed. Each iteration of the Matrix Class with have it's own m,numberOfRows and numberOfColumns. 

private double[][] m; 
private int numberOfRows; private int numberOfColumns; 

//Constructors: Two constructors were created. The first, takes two ints as parameters for the shape of the matrix and initializes the elements to 0. The second has no parameters, but instead asks the user to supply the shape and element values via scanner. 

public Matrix(int rows, int columns) 
{ 
    m = new double[rows][columns]; 

    for(int i = 0; i < rows; i++) 
    { 
     for(int j = 0; j < columns; j++) 
     { 
      m[i][j] = 0; 
     } 
    } 
} 

public Matrix() 
{ 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println(); 
    System.out.print("Please enter the number of rows: "); numberOfRows = keyboard.nextInt(); 
    System.out.print("Please enter the number of columns: "); numberOfColumns = keyboard.nextInt(); 
    m = new double[numberOfRows][numberOfColumns]; 
    System.out.println("Please assign the following elements: "); 
    for(int i = 0; i < numberOfRows; i++) 
     { 
      for(int j = 0; j < numberOfColumns; j++) 
       { 
        System.out.print("[" + i + "]" + "[" + j + "]: "); 
        m[i][j] = keyboard.nextDouble(); 
       } 
     } 
} 



//Accessors: Private accessors were created in order to retrieve data for use elsewhere in the class. 

private int getRows(){return numberOfRows;} 
private int getColumns(){return numberOfColumns;} 
private double getElement(int rows, int columns){return m[rows][columns];} 

//Mutators: A private void mutator was created so that the value of a particular element within m, could be asigned a value later within the addition/subtraction/multiplication methods. 

private void assignElement(double value, int i, int j){m[i][j] = value;} 


//Methods: Addition, subtraction and mutiplication operations were written as methods in order to operate on matricices. Both static and non-static methods were constructed. 
//The static versions take two matricies as parameters, while the non-static counterparts only take one and operate it against m. 
//Within all six methods a test is performed in order to check whether or not the two matrcicies are compatible or not. 
//For addiition and subtraction the number of rows and columns must be the same for both matricies and a temporary matrix is created to match those parameters. 
//For multiplication, the number of columns of the first matrix must be equivalent to the number of rows of the second matrix and a temporary matrix is created with the number of rows of the first matrix and 
//the number of columns of the second matrix as parameters. If the matricies are not compatable an error message is printed. 

public static Matrix staticAdd(Matrix x, Matrix y) 
{ 
    Matrix z = new Matrix(x.getRows(), x.getColumns()); 
    double value; 
    System.out.println(); 
    System.out.println("The sum of the matricices is: "); 

    if(x.getRows() == y.getRows() && x.getColumns() == y.getColumns()) 
     { 
      for(int i = 0; i < x.getRows(); i++) 
       { 
        for(int j = 0; j < y.getColumns(); j++) 
        { 
         value = x.getElement(i,j) + y.getElement(i,j); 
         z.assignElement(value,i,j); 
         System.out.print("[" + z.getElement(i,j) + "]"); 
        } 
        System.out.print('\n'); 
       } 
      return z; 
     } 
    else{System.out.println("ERROR: The number of rows and columns of the matricies are not equal."); return z;} 
} 


public static Matrix staticSubtract(Matrix x, Matrix y) 
{ 
    Matrix z = new Matrix(x.getRows(), x.getColumns()); 
    double value; 
    System.out.println(); 
    System.out.println("The difference of the two matricices is: "); 

    if(x.getRows() == y.getRows() && x.getColumns() == y.getColumns()) 
     { 
      for(int i = 0; i < x.getRows(); i++) 
       { 
        for(int j = 0; j < y.getColumns(); j++) 
         { 
          value = x.getElement(i,j) - y.getElement(i,j); 
          z.assignElement(value,i,j); 
         System.out.print("[" + z.getElement(i,j) + "]"); 
         } 
        System.out.print('\n'); 
       } 
      return z; 
     } 
    else{System.out.println("ERROR: The number of rows and columns of the matricies are not equal."); return z;} 
} 

public static Matrix staticMultiply(Matrix x, Matrix y) 
{ 
    Matrix z = new Matrix(x.getRows(), y.getColumns()); 
    double value; 
    System.out.println(); 
    System.out.println("The product of the matricices is: "); 

    if(x.getColumns() == y.getRows()) 
     { 
      for(int i = 0; i < x.getRows(); i++) 
       { 
        for(int j = 0; j < y.getColumns(); j++) 
         { 
          double sum = 0; 
          for(int k = 0; k < x.getRows(); k++) 
           { 
            sum += x.getElement(i,k) * y.getElement(k,j); 
           } 
          value = sum; 
          z.assignElement(value,i,j); 
          System.out.print("[" + z.getElement(i,j) + "]"); 
         } 
        System.out.print('\n'); 
       } 
      return z; 
     } 
    else{System.out.println("ERROR: The number of columns of the first matrix and the number of rows of the second matrix are not equivalent."); return z;} 
} 


public Matrix add(Matrix x) 
{ 
    Matrix z = new Matrix(numberOfRows, numberOfColumns); 
    double value; 
    System.out.println(); 
    System.out.println("The sum of the matricices is: "); 

    if(numberOfRows == x.getRows() && numberOfColumns == x.getColumns()) 
     { 
      for(int i = 0; i < x.getRows(); i++) 
       { 
        for(int j = 0; j < x.getColumns(); j++) 
         { 
          value = m[i][j] + x.getElement(i,j); 
          z.assignElement(value,i,j); 
          System.out.print("[" + z.getElement(i,j) + "]"); 
         } 
        System.out.print('\n'); 
       } 
      return z; 
     } 
    else{System.out.println("ERROR: The number of rows and columns of the matricies are not equivalent."); return z;} 
} 


public Matrix subtract(Matrix x) 
{ 
    Matrix z = new Matrix(numberOfRows, numberOfColumns); 
    double value; 
    System.out.println(); 
    System.out.println("The difference of the two matricices is: "); 

    if(numberOfRows == x.getRows() && numberOfColumns == x.getColumns()) 
     { 
      for(int i = 0; i < x.getRows(); i++) 
       { 
        for(int j = 0; j < x.getColumns(); j++) 
         { 
          value = m[i][j] - x.getElement(i,j); 
          z.assignElement(value,i,j); 
          System.out.print("[" + z.getElement(i,j) + "]"); 
         } 
        System.out.print('\n'); 
       } 
      return z; 
     } 
    else{System.out.println("ERROR: The number of rows and columns of the matricies are not equivalent."); return z;} 
} 

public Matrix multiply(Matrix x) 
{ 
    Matrix z = new Matrix(numberOfRows, x.getColumns()); 
    double value; 
    System.out.println(); 
    System.out.println("The product of the matricices is: "); 

    if(numberOfColumns == x.getRows()) 
     { 
      for(int i = 0; i < numberOfRows; i++) 
       { 
        for(int j = 0; j < x.getColumns(); j++) 
         { 
          double sum = 0; 
          for(int k = 0; k < numberOfRows; k++) 
           { 
            sum += m[i][k] * x.getElement(k,j); 
           } 
          value = sum; 
          z.assignElement(value,i,j); 
          System.out.print("[" + z.getElement(i,j) + "]"); 
         } 
        System.out.print('\n'); 
       } 
      return z; 
     } 
    else{System.out.println("ERROR: The number of columns of the first matrix and the number of rows of the second matrix are not equivalent."); return z;} 
} 

//toString: A to string was created to display the matricices and to prevent java from created its own version of a toString. 

public String toString() 
{ 
    String es = new String(); 

    for(int i = 0; i < numberOfRows; i++) 
     { 
      for(int j = 0; j < numberOfColumns; j++) 
       { 
        es += "["; 
        es += m[i][j]; 
        es += "]"; 
       } 
      es += '\n'; 
     } 
    System.out.println(); 
    System.out.println("Your matrix: "); 
    return es; 
} 

} 당신에 의해 실행 코드가 (당신이 codereview.stackexchange.com에 도달 시도해 볼 수도 있습니다) 아직 여러 측면에서 개선 될 수 있지만, 당신은 당신이 점점 오류를 제거 할 수 있지만

+0

문제는 'm3 [i] [j] = m1 [i] [j] + m2 [i] [j]'문과 동일합니다. 'm3','m1'과'm2'를 행렬로 정의했습니다. 'm3 [i] [j]'는 무엇을 의미합니까? 'm3 [i] [j]'표기법은 m3이 배열 인 경우에만 의미가 있습니다. 행렬에 값을 설정하는 메소드를 추가해야하며, 행렬에 값을 가져 오는 메소드를 추가해야합니다. –

답변

1

. -

public static Matrix add(Matrix m1, Matrix m2) { 
    Matrix m3 = new Matrix(m1.getRows(),m1.getCols()); 
    for(int i = 0; i < m1.getRows(); i++) { 
     for(int j = 0; j < m2.getCols(); j++) { 
      m3.m[i][j] = m1.m[i][j] + m2.m[i][j]; // m is the double[][] array for each Matrix instance 
     } 
    } 
    return m3; 
}