1.实验内容
(1) 设计和编写代表矩阵的Matrix类。该类包括矩阵行列数变量int rows和int cols,矩阵数据数组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()等内容。
(2) 编写测试类MatrixTest,并在该类中创建两个矩阵对象,计算其乘积。
(3) 为矩阵类添加相关的自定义异常类,并在适当位置使用这些异常类。
2.预习内容
(1) 数组的定义
(2) 类的定义
(3) 异常处理
3.实验类型
验证型
4.实验目的
(1) 掌握数组的定义和使用
(2) 理解异常的使用和自定义异常类的方法。
(3) 理解throw和throws的区别和联系。
5.实验要求
(1) 编写Matrix类
(2) 编写MatrixTest类。在该类中通过键盘输入方式确定所要创建的两个矩阵的行列数,根据行列数随机生成数据或键盘输入,并通过setData方法生成矩阵的内容。
(3) 计算矩阵的乘积,并把结果通过toString方法输出到屏幕上
(4) 编写矩阵行数或列数非法异常类IllegalArgumentException、矩阵行号或列号非法异常类IllegalIndexException以及矩阵无法相乘异常类MatrixMultiplicationException。这些类只需要包含toString方法即可。
(5) 在Matrix类的构造方法中,如果rows或cols变量值小于1,抛出IllegalArgumentException异常;在getData和setData方法中,如果row或col大于等于矩阵行数或列数,或小于0,则抛出IllegalIndexException异常;在multiply方法中,如果两个矩阵的行列数不满足矩阵相乘规则,抛出MatrixMultiplicationException异常。
(6) 在MatrixTest类中测试上述异常定义是否有效。
(7) 提交调试通过的Java程序。
Matrix类:
package main;
public class Matrix extends Object
{
int rows;
int cols;
double[][] data;
Matrix()
{
rows = 1;
cols = 1;
data = new double[1][1];
}
Matrix(int rows, int cols) throws IllegalArgumentException
{
if(rows<1||cols<1)
throw new IllegalArgumentException();
else
{
this.rows = rows;
this.cols = cols;
data=new double[rows][cols];
}
}
Matrix(double data[][]) throws IllegalArgumentException
{
rows=data.length;
/// cols=data[0].length;
///System.out.println(rows);
///System.out.println(cols);
if(rows<1)
throw new IllegalArgumentException();
else {
cols=data[0].length;
if(cols<1)
throw new IllegalArgumentException();
else
{
this.data=new double[rows][cols];
for(int i=0;i<rows;i++)
for (int j=0;j<cols;j++)
{
this.data[i][j]=data[i][j];
}
}
}
/*if(rows<1||cols<1)
{
throw new IllegalArgumentException();
}
else
{
this.data=new double[rows][cols];
for(int i=0;i<rows;i++)
{
for (int j=0;j<cols;j++)
{
this.data[i][j]=data[i][j];
}
}
}*/
}
void getData(int row,int col) throws IllegalIndexException
{
if(row>=rows||col>=cols||row<0||col<0)
{
throw new IllegalIndexException();
}
else
System.out.println(row+"行"+col+"列为:"+data[row][col]);
}
void setData(int row,int col,double value) throws IllegalIndexException
{
if(row>=rows||col>=cols||row<0||col<0)
{
throw new IllegalIndexException();
}
else
data[row][col]=value;
}
void multiply(Matrix m) throws MatrixMultiplicationException
{
if(this.cols!=m.rows)
{
throw new MatrixMultiplicationException();
}
else
{
int x = this.cols;
int r = this.rows;
int c = m.cols; //记录行和列,r是行,c是列。
//System.out.println(this.rows);
int[][] temp = new int[r][c];
//double[][]temp=new double[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
//temp[i][j]=0;
for (int k = 0; k < x; k++) {
temp[i][j] += this.data[i][k] * m.data[k][j];
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
System.out.print(temp[i][j] + " ");
}
System.out.println();
}
}
}
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;
}
}
}
矩阵行数或列数非法异常类IllegalArgumentException
package main;
public class IllegalArgumentException extends Exception
{
public String toString()
{
return "rows或cols变量值小于1";
}
}
矩阵行号或列号非法异常类IllegalIndexException
package main;
public class IllegalIndexException extends Exception
{
public String toString()
{
return "row或col大于等于矩阵行数或列数";
}
}
矩阵无法相乘异常类MatrixMultiplicationException
package main;
public class MatrixMultiplicationException extends Exception
{
public String toString()
{
return "两个矩阵的行列数不满足矩阵相乘规则";
}
}
总体测试类:
package main;
import java.util.Scanner;
import java.util.Scanner;
public class MatrixTest
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("输入第一组行号:");
int x1=input.nextInt();
System.out.println("输入第一组列号:");
int y1=input.nextInt();
System.out.println("输入第二组行号:");
int x2=input.nextInt();
System.out.println("输入第二组列号:");
int y2=input.nextInt();
double [][]Datatest1={{4.0,4.0,4.0,4.0},{4.0,4.0,4.0,4.0},{4.0,4.0,4.0,4.0},{4.0,4.0,4.0,4.0}};
double [][]Datatest2;
Datatest2=new double[0][0];
//System.out.println(x);
try {
Matrix M1 = new Matrix(x1, y1);
Matrix M2 = new Matrix(x2, y2);
Matrix M3 = new Matrix(x1, y1);
Matrix M4= new Matrix(Datatest1);
Matrix M5=new Matrix(Datatest2);
for (int i = 0; i < x1; i++) {
for (int j = 0; j < y1; j++) {
M1.setData(i, j, 2);
}
}
for (int i = 0; i < x2; i++) {
for (int j = 0; j < y2; j++) {
M2.setData(i, j, 3);
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
M3.setData(i, j, 2);
}
}
M1.getData(1, 1);
M2.getData(2, 2);
//M1.getData(23,23);
M1.multiply(M2);
M1.equals(M2);
M1.equals(M3);
}
catch (IllegalArgumentException e)
{
System.out.println(e);
}
catch (IllegalIndexException e)
{
System.out.println(e);
}
catch (MatrixMultiplicationException e)
{
System.out.println(e);
}
}
}
大家有什么问题可以在下方留言,谢谢大家的支持