1、实验二 Array一、实验目的:1. Grasp the declaration and usage of basic data types2. Grasp the declaration, definition, initialization and usage of Array3. Grasp the javadoc comments二、实验内容:设计一个类,包含一个元素类型为 int 的矩阵,设计一个方法把矩阵转置,重写方法toString(),使其能输出此矩阵。设计一个造成矩阵类 Matrix,包含一个元素类型为 int 的二维数组,分别设计一个静态方法和实例方法把矩阵转置,分别设计静
2、态方法和实例方法实现二个矩阵相加、相乘。重写方法 toString(),使其能输出此矩阵。三、实验要求:1. 体会静态方法和实例方法的设计2. 要求使用 toString()方法、以二维形式输出转置前的矩阵和转置后的矩阵。四、实验学时: 2 学时五、实验步骤:1、Edit the Java program;/主类/* To change this template, choose Tools | Templates* and open the template in the editor.*/package array;/* author zxl*/public class Array pub
3、lic static void main(String args) inta=1,2,3,3,2,1; /测试用例intb=0,1,1,1,0,1,1,1,1;Matrix mat1=new Matrix(a);Matrix mat2=new Matrix(b);String s=“;s+=“原始矩阵为: n“+mat1.toString()+“n“;s+=“转置矩阵为(实例方法): n“+mat1.Transpose()+“n“;s+=“转置矩阵为(静态方法): n“+Matrix.Transpose(mat1)+“n“;System.out.print (s);s=“;s+=“第一个矩阵为
4、: n“+mat1.toString()+“n“;s+=“第二个矩阵为: n“+mat2.toString()+“n“;s+=“相加结果为(实例方法): n“+mat1.plus(mat1).toString()+“n“;s+=“相加结果为(静态方法): n“+Matrix.plus(mat1,mat2)+“n“;System.out.print (s);s=“;s+=“第一个矩阵为: n“+mat1.toString()+“n“;s+=“第二个矩阵为: n“+mat2.toString()+“n“;s+=“相乘结果为(实例方法): n“+mat1.Multiply(mat2).toStrin
5、g()+“n“;s+=“相乘结果为(静态方法): n“+Matrix.Multiply(mat1,mat2)+“n“;System.out.print (s);/Matrix 类/* To change this template, choose Tools | Templates* and open the template in the editor.*/package array;/* author zxl*/public class Matrix private intdata; /定义一个私有二维字段/定义带参构造函数public Matrix(intelem) data=elem;
6、/重写函数 toString(),负责矩阵输出Overridepublic String toString() String s=“;for(int i=0;idata.length;i+)for(int j=0;jdata0.length;j+)s+=dataij+“,“;s+=“n“;return s; /实例方法,实现矩阵转置public Matrix Transpose() intmtx=new intdata0.lengthdata.length;for(int i=0;idata.length;i+)for(int j=0;jdata0.length;j+)mtxji=dataij
7、;Matrix resultMatrix=new Matrix(mtx);return resultMatrix;/静态方法,实现矩阵转置public static Matrix Transpose(Matrix newMat) return newMat.Transpose();/实例方法,实现矩阵相加public Matrix plus(Matrix another)intmtx=new intdata.lengthdata0.length;for(int i=0;idata.length;i+)for(int j=0;jdata0.length;j+)mtxij=dataij+anoth
8、er.dataij;Matrix resultMatrix=new Matrix(mtx);return resultMatrix;/静态方法,实现矩阵相加public static Matrix plus(Matrix first,Matrix second)return first.plus(second);/实例方法,实现矩阵相乘public Matrix Multiply(Matrix another)intmtx=new intdata.lengthdata0.length;int i,j,k,r;for(k=0;kanother.data0.length;k+)for(i=0;id
9、ata.length;i+)r=0;for(j=0;jdatai.length;j+)r+=dataij*another.datajk;mtxik=r;Matrix resultMatrix=new Matrix(mtx);return resultMatrix;/静态方法,实现矩阵相乘public static Matrix Multiply(Matrix first,Matrix second)return first.plus(second);2、Compile Java program, find the syntactic errors in the program, record
10、the main errors and correct them;编辑程序,编译出错,检查程序,并加以修改。1) 修改前修改后:3、Test the program using the pre-designed test case, find the logical errors in the program and correct them;调试运行程序,矩阵相乘结果出错,与预期不符:分析:预期相乘结果应为( 5,4,6) (3,4,6) ,检查程序,发现矩阵相乘函数Matrix itmes(Matrix another)中第二个矩阵的行列出错,如下:修改后为:修改好编译通过,运行结果问题得以消除:4、Run the final program, and analyze the results。取随机测试矩阵:inta=1,2,3,3,2,1;intb=0,1,1,1,0,1,1,1,1;运行结果如下:结果正确。六总结