编写矩阵类实验
5.Matrix。该类包括矩阵数据数组double data[ ][ ],构造方法Matrix()、Matrix(int rows,int cols)、Matrix(double data[ ][ ])、获取某元素值的方法getData(int row,int col)、设置某元素值的方法setData(int row,int col,double value)、计算两个矩阵的乘积的方法multiply(Matrix m)以及equals ( )、toString( )等内容;
编写测试类实验
5.Test,测试Matrix类的正确性;编写矩阵行数或列数非法异常类IllegalArgumentException、矩阵行号或列号非法异常类IllegalIndexException、矩阵无法相乘异常类MatrixMultiplicationException;完善Matrix类的相关方法,使其在不正确的调用情况下抛出适当的异常对象;完善Test类,以测试Matrix对异常类的应用是否有效。
Matrix:
package Experiment5;
import org.w3c.dom.CDATASection;
import java.awt.datatransfer.DataFlavor;
import java.util.Scanner;
public class Matrix {
int rows;
int cols;
double data[][];
public Matrix() {
this(1, 1);
}
Matrix(int rows, int cols){
try {
if (rows < 1 || cols < 1)
throw new IllegalArgumentException();
data = new double[rows][cols];
}
catch (IllegalArgumentException e){
e.toString();
}
}
public Matrix(int rows, int cols, double[][] data) {
super();
this.rows = rows;
this.cols = cols;
this.data = data;
}
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
public double getData(int row, int col) {
return data[row][col];
}
public void setData(int row, int col, double value) {
data[row][col] = value;
}
public double[][] multiply(double[][] m2) {
int m1rows = data.length;
int m1cols = data[0].length;
int m2rows = m2.length;
int m2cols = m2[0].length;
double[][] result = new double[m1rows][m2cols];
for (int i = 0; i < m1rows; i++)
for (int j = 0; j < m2cols; j++)
for (int k = 0; k < m1cols; k++)
result[i][j] += data[i][k] * m2[k][j];
return result;
}
public boolean equals(Object obj)
{
if(obj instanceof Matrix)
{
Matrix M=(Matrix)obj;
if(this.cols==M.cols||this.rows==M.rows)
{
for(int i=0;i<this.rows;i++)
{
for (int j=0;j<this.cols;j++)
{
if(this.data[i][j]!=M.data[i][j])
return false;
}
}
System.out.println("相等!");
return true;
}
else
{
System.out.println("不相等!");
return false;
}
}
else
{
System.out.println("不相等!");
return false;
}
}
public String toString() {
StringBuffer sb = new StringBuffer();
int rows = data.length;
int cols = data[0].length;
sb.append("array[" + rows + "][" + cols + "] = " + "\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
sb.append(" " + data[i][j] + " ");
sb.append("\n");
}
sb.append("\n");
return sb.toString();
}
}
Test:
package Experiment5;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("请输入x数组阶数:");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
double x[][] = new double[n][n];
System.out.println("输入数组x的各个数字:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
x[i][j] = scan.nextInt();
}
}
System.out.println("数组x:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(x[i][j] + "\t\t");
if (j == n - 1)
System.out.println();
}
}
System.out.println("请输入y数组阶数:");
Scanner scan0 = new Scanner(System.in);
int k = scan.nextInt();
double y[][] = new double[k][k];
System.out.println("输入数组y的各个数字:");
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
y[i][j] = scan0.nextInt();
}
}
System.out.println("数组y:");
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
System.out.print(y[i][j] + "\t\t");
if (j == k - 1)
System.out.println();
}
}
Matrix m = new Matrix(n, n, x);
double z[][] = m.multiply(y);
Matrix zm = new Matrix(z.length, z[0].length, z);
System.out.println(zm);
}
}
IllegalArgumentException:
package Experiment5;
import Experiment5.Matrix;
class IllegalArgumentException extends Exception
{
public String toString()
{
return "row或col大于等于矩阵行数或列数";
}
}