Java程序设计(七)----矩阵(对矩阵操作的对象)

来源:互联网 发布:手机五线谱编辑软件 编辑:程序博客网 时间:2024/06/05 21:08

/* (程序头部注释开始)

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2012, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:

* 作 者: 刘镇
* 完成日期: 2012 年 10 月 09 日
* 版 本 号: 2.007

* 对任务及求解方法的描述部分
* 输入描述: ......

* 问题描述:封装一类对矩阵操作的对象,该类对象能够对矩阵进行运算,如矩阵中数据的位置变换功能、矩阵的加法功能、矩阵的乘法功能

* 程序输出: ......

* 程序头部的注释结束
*/

 

MatrixOperation.java:

 

 

 

package lz_4w;public class MatrixOperation extends Matrix{public void changeMatrix(Matrix m, int column_1, int row_1, int column_2, int row_2){int t = 0;t = m.M[column_1][row_1];    m.M[column_1][row_1] = m.M[column_2][row_2];    m.M[column_2][row_2] = t;}public void MatrixPlus(Matrix m, Matrix n){int column = m.M.length;int row = n.M[0].length;int N = n.M.length;if(m.M[0].length != n.M.length){System.out.println("不能计算矩阵乘法!");return ;}int[][] c = new int[column][row];for(int i = 0; i < column; ++i){for(int j = 0; j < row; ++j){int total = 0;for(int k = 0; k < N; ++k){total += m.M[i][k] * n.M[k][j];}c[i][j] = total;}}for(int i = 0; i < column; ++i){for(int j = 0; j < row; ++j){System.out.print(c[i][j] + "\t");}System.out.println();}System.out.println();}public void MatrixAdd(Matrix m, Matrix n){if((m.M.length != n.M.length) || (m.M[0].length != n.M[0].length)){System.out.println("不能计算两矩阵加法!");return ;}int column = m.M.length;int row = m.M[0].length;int[][] c = new int[column][row];for(int i = 0; i < column; ++i){for(int j = 0; j < row; ++j){c[i][j] = m.M[i][j] + n.M[i][j];}}for(int i = 0; i < column; ++i){for(int j = 0; j < row; ++j){System.out.print(c[i][j] + "\t");}System.out.println();}System.out.println();}}


 

 

 

测试类:

 

package lz_4w;public class Test_Matrix {/** * @param args */public static void main(String[] args) {Matrix m = new Matrix(8, 8, 10);Matrix n = new Matrix(8, 8, 5);MatrixOperation M = new MatrixOperation();m.setM(3, 2, 60);n.setM(5, 4, 40);m.displayMatrix();n.displayMatrix();    M.changeMatrix(m, 2, 2, 1, 1);M.changeMatrix(n, 3, 3, 2, 2);M.MatrixAdd(m, n);M.MatrixAdd(m, n);}}


 

 

 

成果展示:

 

 

 

原创粉丝点击