Java上机 第五周 任务6 矩阵中数据的位置变换功能、矩阵的加法功能、矩阵的乘法功能
/* * 程序头部注释开始 * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称:创建一个矩阵类 * 作 者:薛广晨 * 完成日期:2011 年 10 月 02 日 * 版 本号:x1.0 * 对任务及求解方法的描述部分 * 输入描述: * 问题描述:封装一类对矩阵操作的对象,该类对象能够对矩阵进行运算,如矩阵中数据的位置变换功能、矩阵的加法功能、矩阵的乘法功能。提示:矩阵的乘法规则。* 程序输出: * 程序头部的注释结束 *///测试类package xue;public class TestMatrixPlus {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubMatrixPlus M = new MatrixPlus();M.display();M.set_Column(6);M.set_Row(6);M.change_Plus(1, 1, 5);M.change_Plus(2, 2, 5);M.change_Plus(3, 3, 5);M.change_Plus(4, 4, 5);M.change_Plus(5, 5, 5);System.out.println("修改后矩阵的值为:");M.display();M.change(0, 0, 1, 1);System.out.println("矩阵的值交换后为:");M.display();MatrixPlus a = new MatrixPlus();a.set_Column(6);a.set_Row(6);System.out.println("相加后结果");M.matrixPlus_Add(a); System.out.println("相乘后结果");M.multiplication(a);}}//矩阵类package xue;public class MatrixPlus {/** * @param args */int [][]M;int column;int row; MatrixPlus() { this.column = 5; this.row = 5; M = new int [column][row]; for(int i = 0; i < column; i++) { for(int j = 0; j < M[i].length; j++) { M[i][j] = 1; } } } MatrixPlus(int column, int row) { this.column = column; this.row = row; M = new int [column][row]; for(int i = 0; i < column; i++) { for(int j = 0; j < M[i].length; j++) { M[i][j] = 1; } } } public void set_Column(int column) { this.column = column; M = new int [column][row]; for(int i = 0; i < column; i++) { for(int j = 0; j < M[i].length; j++) { M[i][j] = 1; } } } public void set_Row(int row) { this.row = row; M = new int [column][row]; for(int i = 0; i < column; i++) { for(int j = 0; j < M[i].length; j++) { M[i][j] = 1; } } } public void change_Plus(int column, int row, int a) { M[column][row] = a; } public void display() { if(M == null) { System.out.println("数组为空"); return; } for(int i = 0; i < column; i++) { for(int j = 0; j < M[i].length; j++) { System.out.print(M[i][j] + "\t" ); } System.out.println(); } } void change(int column1, int row1, int column2, int row2) { if(column1 < 0 && column1 > column && row1 < 0 && row1 > row && column2 < 0 && column2 > column && row2 < 0 && row2 > row) { System.out.println("输入的下标不合法"); return; } else { int number; number = M[column1][row1]; M[column1][row1] = M[column2][row2]; M[column2][row2] = number; } } // 矩阵元素相加 void matrixPlus_Add(MatrixPlus s) { if(s.column != this.column || s.row != this.row) { System.out.println("两矩阵的行或列不同,不能相加"); return; } MatrixPlus copy = new MatrixPlus(this.column, this.row); for (int i = 0; i < this.column; i++) for (int j = 0; j < this.row; j++) copy.M[i][j] = s.M[i][j] + this.M[i][j]; copy.display(); } // 矩阵元素相乘 void multiplication(MatrixPlus s) { //1.要判断矩阵能否相乘,能相乘继续,不能相乘给出提示为什么不能相乘 //2.得到C矩阵的行数和列数,以产生C矩阵 //3.输出结果矩阵C的值 if(s.row != this.column) { System.out.println("不能计算矩阵乘法!"); return; } int sum = 0; MatrixPlus c = new MatrixPlus(s.column, this.row); for (int i = 0; i < s.column; ++i) { for (int j = 0; j < this.row; ++j) { sum = 0; for (int m = 0; m < s.column; ++m) { sum = sum + s.M[i][m] * this.M[m][j]; } c.M[i][j] = sum; } } c.display(); }}
运行结果:
