鱼C论坛

 找回密码
 立即注册
查看: 205|回复: 1

[已解决]怎么写啊

[复制链接]
发表于 2023-12-3 21:52:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
编写一个设计通用矩阵类的程序,实现下列要求:
(1)定义数据域:使用二维数组表示一个m×n阶矩阵,数组元素为double类型;
(2)定义若干个构造方法(至少包含以下三个):
一个默认无参数的构造方法;
一个创建指定二维数组具体值的矩阵的构造方法;
一个创建指定矩阵维数(m×n)的随机数的矩阵的构造方法;
(3)属性的访问器方法;
(4)toString()方法:返回矩阵的维数和按矩阵行列排列的元素值;
(5)矩阵常见运算的方法
矩阵加法:将两个矩阵作为参数,首先判断两个矩阵维数是否相同,然后按矩阵加法运算规则将两个矩阵相加,返回相加后的矩阵;
矩阵相乘:将两个矩阵作为参数,首先判断矩阵相乘运算中的左矩阵的行数和右矩阵的列数是否相同,然后按照矩阵乘法运算规则进行两个矩阵相乘,返回相乘后的矩阵;
矩阵转置:实现矩阵转置,返回转置矩阵。
数除矩阵:将一个常数、数组作为参数,并将矩阵的每个元素整除该常数,返回相除后的新矩阵;
获取某列向量:获取矩阵中某一列向量,并返回该列向量;
获取某行向量:获取矩阵中某一行向量,并返回该行向量;

编写一个方阵类的程序,实现下列要求:
(1)方阵类要求继承通用矩阵类,并定义相应的数据域,数组元素为double类型;
(2)定义方阵类的相应构造方法:
一个默认无参数的构造方法;
调用父类构造方法;
(3)属性的访问器方法;
(4)求方阵主对角线和的方法,返回和值;

编写一个矩阵的自定义异常类的程序,实现下列要求:
(1)矩阵相加时维数不相等时的异常,显示两个矩阵的维数;
(2)矩阵相乘是左矩阵的行数与右矩阵的列数不相等时的异常,显示两个矩阵的维数;

另外编程一个测试类,在main方法中使用创建矩阵实例,能够测试类中的所有方法,成员方法返回的矩阵要求调用toString()方法输出相应的矩阵,运行结果的截图体现在实验报告中,并以相应的文字说明。
最佳答案
2023-12-3 21:53:50
以下是一个基于Java语言的编程示例,实现了上述要求:

通用矩阵类 Matrix:
  1. public class Matrix {
  2.     private double[][] data; // 数据域
  3.     // 默认无参数构造方法
  4.     public Matrix() {
  5.     }
  6.     // 创建指定二维数组具体值的矩阵的构造方法
  7.     public Matrix(double[][] data) {
  8.         this.data = data;
  9.     }
  10.     // 创建指定矩阵维数(m×n)的随机数的矩阵的构造方法
  11.     public Matrix(int m, int n) {
  12.         this.data = new double[m][n];
  13.         for (int i = 0; i < m; i++) {
  14.             for (int j = 0; j < n; j++) {
  15.                 data[i][j] = Math.random();
  16.             }
  17.         }
  18.     }
  19.     // 获取矩阵维数的访问器方法
  20.     public int getRows() {
  21.         return data.length;
  22.     }
  23.     public int getCols() {
  24.         return data[0].length;
  25.     }
  26.     // 返回矩阵的字符串表示
  27.     @Override
  28.     public String toString() {
  29.         StringBuilder result = new StringBuilder();
  30.         result.append("Matrix " + getRows() + "x" + getCols() + ":\n");
  31.         for (int i = 0; i < getRows(); i++) {
  32.             for (int j = 0; j < getCols(); j++) {
  33.                 result.append(data[i][j] + "\t");
  34.             }
  35.             result.append("\n");
  36.         }
  37.         return result.toString();
  38.     }
  39.     // 矩阵加法
  40.     public Matrix add(Matrix matrix) throws MatrixDimensionMismatchException {
  41.         if (getRows() != matrix.getRows() || getCols() != matrix.getCols()) {
  42.             throw new MatrixDimensionMismatchException(this, matrix);
  43.         }
  44.         double[][] resultData = new double[getRows()][getCols()];
  45.         for (int i = 0; i < getRows(); i++) {
  46.             for (int j = 0; j < getCols(); j++) {
  47.                 resultData[i][j] = data[i][j] + matrix.data[i][j];
  48.             }
  49.         }
  50.         return new Matrix(resultData);
  51.     }
  52.     // 矩阵相乘
  53.     public Matrix multiply(Matrix matrix) throws MatrixDimensionMismatchException {
  54.         if (getCols() != matrix.getRows()) {
  55.             throw new MatrixDimensionMismatchException(this, matrix);
  56.         }
  57.         double[][] resultData = new double[getRows()][matrix.getCols()];
  58.         for (int i = 0; i < getRows(); i++) {
  59.             for (int j = 0; j < matrix.getCols(); j++) {
  60.                 double sum = 0;
  61.                 for (int k = 0; k < getCols(); k++) {
  62.                     sum += data[i][k] * matrix.data[k][j];
  63.                 }
  64.                 resultData[i][j] = sum;
  65.             }
  66.         }
  67.         return new Matrix(resultData);
  68.     }
  69.     // 矩阵转置
  70.     public Matrix transpose() {
  71.         double[][] resultData = new double[getCols()][getRows()];
  72.         for (int i = 0; i < getCols(); i++) {
  73.             for (int j = 0; j < getRows(); j++) {
  74.                 resultData[i][j] = data[j][i];
  75.             }
  76.         }
  77.         return new Matrix(resultData);
  78.     }
  79.     // 数除矩阵
  80.     public Matrix divide(double number) {
  81.         double[][] resultData = new double[getRows()][getCols()];
  82.         for (int i = 0; i < getRows(); i++) {
  83.             for (int j = 0; j < getCols(); j++) {
  84.                 resultData[i][j] = data[i][j] / number;
  85.             }
  86.         }
  87.         return new Matrix(resultData);
  88.     }
  89.     // 获取某列向量
  90.     public Matrix getColumn(int col) throws IndexOutOfBoundsException {
  91.         if (col < 0 || col >= getCols()) {
  92.             throw new IndexOutOfBoundsException("Invalid column index: " + col);
  93.         }
  94.         double[][] resultData = new double[getRows()][1];
  95.         for (int i = 0; i < getRows(); i++) {
  96.             resultData[i][0] = data[i][col];
  97.         }
  98.         return new Matrix(resultData);
  99.     }
  100.     // 获取某行向量
  101.     public Matrix getRow(int row) throws IndexOutOfBoundsException {
  102.         if (row < 0 || row >= getRows()) {
  103.             throw new IndexOutOfBoundsException("Invalid row index: " + row);
  104.         }
  105.         double[][] resultData = new double[1][getCols()];
  106.         for (int j = 0; j < getCols(); j++) {
  107.             resultData[0][j] = data[row][j];
  108.         }
  109.         return new Matrix(resultData);
  110.     }
  111. }
复制代码

方阵类 SquareMatrix:
  1. public class SquareMatrix extends Matrix {
  2.     // 调用父类构造方法
  3.     public SquareMatrix(int n) {
  4.         super(n, n);
  5.     }
  6.     // 求主对角线和的方法
  7.     public double sumOfMainDiagonal() {
  8.         double sum = 0;
  9.         for (int i = 0; i < getRows(); i++) {
  10.             sum += super.data[i][i];
  11.         }
  12.         return sum;
  13.     }
  14. }
复制代码

矩阵维度不匹配时的自定义异常类 MatrixDimensionMismatchException:
  1. public class MatrixDimensionMismatchException extends Exception {
  2.     public MatrixDimensionMismatchException(Matrix matrix1, Matrix matrix2) {
  3.         super("Matrix dimension mismatch: (" + matrix1.getRows() + "x" + matrix1.getCols()
  4.                 + ") and (" + matrix2.getRows() + "x" + matrix2.getCols() + ")");
  5.     }
  6. }
复制代码

测试类 Main:
  1. public class Main {
  2.     public static void main(String[] args) {
  3.         Matrix matrix1 = new Matrix(new double[][]{{1, 2}, {3, 4}});
  4.         Matrix matrix2 = new Matrix(new double[][]{{5, 6}, {7, 8}});
  5.         try {
  6.             System.out.println("Matrix1:");
  7.             System.out.println(matrix1);
  8.             System.out.println("Matrix2:");
  9.             System.out.println(matrix2);
  10.             System.out.println("Matrix1 + Matrix2:");
  11.             System.out.println(matrix1.add(matrix2));
  12.             System.out.println("Matrix1 * Matrix2:");
  13.             System.out.println(matrix1.multiply(matrix2));
  14.             System.out.println("Transpose of Matrix1:");
  15.             System.out.println(matrix1.transpose());
  16.             System.out.println("Matrix1 / 2:");
  17.             System.out.println(matrix1.divide(2));
  18.             System.out.println("Column 1 of Matrix1:");
  19.             System.out.println(matrix1.getColumn(0));
  20.             System.out.println("Row 0 of Matrix1:");
  21.             System.out.println(matrix1.getRow(0));
  22.             SquareMatrix squareMatrix = new SquareMatrix(3);
  23.             System.out.println("SquareMatrix:");
  24.             System.out.println(squareMatrix);
  25.             System.out.println("Sum of main diagonal of SquareMatrix:");
  26.             System.out.println(squareMatrix.sumOfMainDiagonal());
  27.         } catch (MatrixDimensionMismatchException e) {
  28.             System.out.println(e.getMessage());
  29.         } catch (IndexOutOfBoundsException e) {
  30.             System.out.println(e.getMessage());
  31.         }
  32.     }
  33. }
复制代码

上述代码中创建了一个通用矩阵类 Matrix 和一个方阵类 SquareMatrix,以及一个自定义异常类 MatrixDimensionMismatchException。测试类 Main 中展示了如何使用这些类进行矩阵相关操作,并处理可能抛出的异常。

请根据实际需求修改代码,例如添加其他所需的方法或属性,适配不同的问题场景。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-3 21:53:50 | 显示全部楼层    本楼为最佳答案   
以下是一个基于Java语言的编程示例,实现了上述要求:

通用矩阵类 Matrix:
  1. public class Matrix {
  2.     private double[][] data; // 数据域
  3.     // 默认无参数构造方法
  4.     public Matrix() {
  5.     }
  6.     // 创建指定二维数组具体值的矩阵的构造方法
  7.     public Matrix(double[][] data) {
  8.         this.data = data;
  9.     }
  10.     // 创建指定矩阵维数(m×n)的随机数的矩阵的构造方法
  11.     public Matrix(int m, int n) {
  12.         this.data = new double[m][n];
  13.         for (int i = 0; i < m; i++) {
  14.             for (int j = 0; j < n; j++) {
  15.                 data[i][j] = Math.random();
  16.             }
  17.         }
  18.     }
  19.     // 获取矩阵维数的访问器方法
  20.     public int getRows() {
  21.         return data.length;
  22.     }
  23.     public int getCols() {
  24.         return data[0].length;
  25.     }
  26.     // 返回矩阵的字符串表示
  27.     @Override
  28.     public String toString() {
  29.         StringBuilder result = new StringBuilder();
  30.         result.append("Matrix " + getRows() + "x" + getCols() + ":\n");
  31.         for (int i = 0; i < getRows(); i++) {
  32.             for (int j = 0; j < getCols(); j++) {
  33.                 result.append(data[i][j] + "\t");
  34.             }
  35.             result.append("\n");
  36.         }
  37.         return result.toString();
  38.     }
  39.     // 矩阵加法
  40.     public Matrix add(Matrix matrix) throws MatrixDimensionMismatchException {
  41.         if (getRows() != matrix.getRows() || getCols() != matrix.getCols()) {
  42.             throw new MatrixDimensionMismatchException(this, matrix);
  43.         }
  44.         double[][] resultData = new double[getRows()][getCols()];
  45.         for (int i = 0; i < getRows(); i++) {
  46.             for (int j = 0; j < getCols(); j++) {
  47.                 resultData[i][j] = data[i][j] + matrix.data[i][j];
  48.             }
  49.         }
  50.         return new Matrix(resultData);
  51.     }
  52.     // 矩阵相乘
  53.     public Matrix multiply(Matrix matrix) throws MatrixDimensionMismatchException {
  54.         if (getCols() != matrix.getRows()) {
  55.             throw new MatrixDimensionMismatchException(this, matrix);
  56.         }
  57.         double[][] resultData = new double[getRows()][matrix.getCols()];
  58.         for (int i = 0; i < getRows(); i++) {
  59.             for (int j = 0; j < matrix.getCols(); j++) {
  60.                 double sum = 0;
  61.                 for (int k = 0; k < getCols(); k++) {
  62.                     sum += data[i][k] * matrix.data[k][j];
  63.                 }
  64.                 resultData[i][j] = sum;
  65.             }
  66.         }
  67.         return new Matrix(resultData);
  68.     }
  69.     // 矩阵转置
  70.     public Matrix transpose() {
  71.         double[][] resultData = new double[getCols()][getRows()];
  72.         for (int i = 0; i < getCols(); i++) {
  73.             for (int j = 0; j < getRows(); j++) {
  74.                 resultData[i][j] = data[j][i];
  75.             }
  76.         }
  77.         return new Matrix(resultData);
  78.     }
  79.     // 数除矩阵
  80.     public Matrix divide(double number) {
  81.         double[][] resultData = new double[getRows()][getCols()];
  82.         for (int i = 0; i < getRows(); i++) {
  83.             for (int j = 0; j < getCols(); j++) {
  84.                 resultData[i][j] = data[i][j] / number;
  85.             }
  86.         }
  87.         return new Matrix(resultData);
  88.     }
  89.     // 获取某列向量
  90.     public Matrix getColumn(int col) throws IndexOutOfBoundsException {
  91.         if (col < 0 || col >= getCols()) {
  92.             throw new IndexOutOfBoundsException("Invalid column index: " + col);
  93.         }
  94.         double[][] resultData = new double[getRows()][1];
  95.         for (int i = 0; i < getRows(); i++) {
  96.             resultData[i][0] = data[i][col];
  97.         }
  98.         return new Matrix(resultData);
  99.     }
  100.     // 获取某行向量
  101.     public Matrix getRow(int row) throws IndexOutOfBoundsException {
  102.         if (row < 0 || row >= getRows()) {
  103.             throw new IndexOutOfBoundsException("Invalid row index: " + row);
  104.         }
  105.         double[][] resultData = new double[1][getCols()];
  106.         for (int j = 0; j < getCols(); j++) {
  107.             resultData[0][j] = data[row][j];
  108.         }
  109.         return new Matrix(resultData);
  110.     }
  111. }
复制代码

方阵类 SquareMatrix:
  1. public class SquareMatrix extends Matrix {
  2.     // 调用父类构造方法
  3.     public SquareMatrix(int n) {
  4.         super(n, n);
  5.     }
  6.     // 求主对角线和的方法
  7.     public double sumOfMainDiagonal() {
  8.         double sum = 0;
  9.         for (int i = 0; i < getRows(); i++) {
  10.             sum += super.data[i][i];
  11.         }
  12.         return sum;
  13.     }
  14. }
复制代码

矩阵维度不匹配时的自定义异常类 MatrixDimensionMismatchException:
  1. public class MatrixDimensionMismatchException extends Exception {
  2.     public MatrixDimensionMismatchException(Matrix matrix1, Matrix matrix2) {
  3.         super("Matrix dimension mismatch: (" + matrix1.getRows() + "x" + matrix1.getCols()
  4.                 + ") and (" + matrix2.getRows() + "x" + matrix2.getCols() + ")");
  5.     }
  6. }
复制代码

测试类 Main:
  1. public class Main {
  2.     public static void main(String[] args) {
  3.         Matrix matrix1 = new Matrix(new double[][]{{1, 2}, {3, 4}});
  4.         Matrix matrix2 = new Matrix(new double[][]{{5, 6}, {7, 8}});
  5.         try {
  6.             System.out.println("Matrix1:");
  7.             System.out.println(matrix1);
  8.             System.out.println("Matrix2:");
  9.             System.out.println(matrix2);
  10.             System.out.println("Matrix1 + Matrix2:");
  11.             System.out.println(matrix1.add(matrix2));
  12.             System.out.println("Matrix1 * Matrix2:");
  13.             System.out.println(matrix1.multiply(matrix2));
  14.             System.out.println("Transpose of Matrix1:");
  15.             System.out.println(matrix1.transpose());
  16.             System.out.println("Matrix1 / 2:");
  17.             System.out.println(matrix1.divide(2));
  18.             System.out.println("Column 1 of Matrix1:");
  19.             System.out.println(matrix1.getColumn(0));
  20.             System.out.println("Row 0 of Matrix1:");
  21.             System.out.println(matrix1.getRow(0));
  22.             SquareMatrix squareMatrix = new SquareMatrix(3);
  23.             System.out.println("SquareMatrix:");
  24.             System.out.println(squareMatrix);
  25.             System.out.println("Sum of main diagonal of SquareMatrix:");
  26.             System.out.println(squareMatrix.sumOfMainDiagonal());
  27.         } catch (MatrixDimensionMismatchException e) {
  28.             System.out.println(e.getMessage());
  29.         } catch (IndexOutOfBoundsException e) {
  30.             System.out.println(e.getMessage());
  31.         }
  32.     }
  33. }
复制代码

上述代码中创建了一个通用矩阵类 Matrix 和一个方阵类 SquareMatrix,以及一个自定义异常类 MatrixDimensionMismatchException。测试类 Main 中展示了如何使用这些类进行矩阵相关操作,并处理可能抛出的异常。

请根据实际需求修改代码,例如添加其他所需的方法或属性,适配不同的问题场景。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 01:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表