⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genericmatrix.java

📁 此源码为机械工业出版社出版的《Java语言程序设计》第三版所配套的书中所有源代码。
💻 JAVA
字号:
// GenericMatrix.java: Define a matrix and its associated
// operations such as add and multiply
public abstract class GenericMatrix
{
  // Representation of a matrix using a two-dimensional array
  private Object[][] matrix;

  // Construct a matrix
  protected GenericMatrix(Object[][] matrix)
  {
    this.matrix = matrix;
  }

  // Getter method for matrix
  public Object[][] getMatrix()
  {
    return matrix;
  }

  // Setter method for matrix
  public void setMatrix(Object[][] matrix)
  {
    this.matrix = matrix;
  }

  // Add two matrices
  public GenericMatrix addMatrix(GenericMatrix secondGenericMatrix)
  {
    // Create a result matrix
    Object[][] result =
      new Object[matrix.length][matrix[0].length];

    // Obtain the second matrix
    Object[][] secondMatrix =  secondGenericMatrix.getMatrix();

    // Check bounds of the two matrices
    if ((matrix.length != secondMatrix.length) ||
        (matrix[0].length != secondMatrix.length))
    {
      System.out.println(
        "The matrices do not have the same size");
      System.exit(0);
    }

    // Perform addition
    for (int i=0; i<result.length; i++)
      for (int j=0; j<result[i].length; j++)
        result[i][j] = add(matrix[i][j], secondMatrix[i][j]);

    GenericMatrix genericResultMatrix = createGenericMatrix();
    genericResultMatrix.setMatrix(result);
    return genericResultMatrix;
  }

  // Multiply two matrices
  public GenericMatrix
    multiplyMatrix(GenericMatrix secondGenericMatrix)
  {
    // Obtain the second matrix
    Object[][] secondMatrix =  secondGenericMatrix.getMatrix();

    // Create result matrix
    Object[][] result =
      new Object[matrix.length][secondMatrix[0].length];

    // Check bounds
    if (matrix[0].length != secondMatrix.length)
    {
      System.out.println("Bounds error");
      System.exit(0);
    }

    // Perform multiplication of two matrices
    for (int i=0; i<result.length; i++)
      for (int j=0; j<result[0].length; j++)
    {
      result[i][j] = zero();

      for (int k=0; k<matrix[0].length; k++)
      {
        result[i][j] = add(result[i][j],
          multiply(this.matrix[i][k], secondMatrix[k][j]));
      }
    }

    GenericMatrix genericResultMatrix = createGenericMatrix();
    genericResultMatrix.setMatrix(result);
    return genericResultMatrix;
  }

  // Print matrice, the operator, and their operation result
  public static void printResult(
    GenericMatrix m1, GenericMatrix m2, GenericMatrix m3, char op)
  {
    for (int i=0; i<(m1.getMatrix()).length; i++)
    {
      for (int j=0; j<(m1.getMatrix())[0].length; j++)
        System.out.print(" " + (m1.getMatrix())[i][j]);

      if (i == (m1.getMatrix()).length/2)
        System.out.print( "  " + op + "  " );
      else
        System.out.print( "     " );

      for (int j=0; j<(m2.getMatrix()).length; j++)
        System.out.print(" " + (m2.getMatrix())[i][j]);

      if (i == (m1.getMatrix()).length/2)
        System.out.print( "  =  " );
      else
        System.out.print( "     " );

      for (int j=0; j<(m3.getMatrix()).length; j++)
        System.out.print(" " + (m3.getMatrix())[i][j]);

      System.out.println();
    }
  }

  // Abstract method for creating a GenericMatrix instance
  public abstract GenericMatrix createGenericMatrix();

  // Abstract method for adding two elements of the matrices
  protected abstract Object add(Object o1, Object o2);

  // Abstract method for multiplying two elements of the matrices
  protected abstract Object multiply(Object o1, Object o2);

  // Abstract method for defining zero for the matrix element
  protected abstract Object zero();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -