⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix.java

📁 (1) 设计和编写代表矩阵的Matrix类。该类包括矩阵行列数变量int rows和int cols
💻 JAVA
字号:
import java.io.*;
class InvalidIndexException extends Exception{  //矩阵数组下标异常
   int row,col,rows,cols;
   InvalidIndexException(int row,int col,int rows,int cols){
      this.row=row;
      this.col=col;
      this.rows=rows;
      this.cols=cols;
   }
   public String toString(){
      return "数组下标溢出:"+row+">"+rows+" 或 "+col+">"+cols;
   }
}
class InvalidMultiplicationException extends Exception{  //乘法异常
   public String toString(){
      return "两个矩阵的行列数不符合乘法规则要求!";
   }
}
class InvalidAdditionException extends Exception{  //加法异常
   public String toString(){
      return "两个矩阵的行列数不符合加法规则!";
   }
}
class Matrix{
   private int data[][],rows,cols;
   Matrix(int rows,int cols){
      this.rows=rows;
      this.cols=cols;
      data=new int[rows][cols];
   }
   void setData(int row,int col,int data) throws InvalidIndexException{
      if (row>rows || col>cols)
         throw new InvalidIndexException(row,col,rows,cols);
      this.data[row][col]=data;
   }
   int getData(int row,int col) throws InvalidIndexException{
      if (row>rows || col>cols)
         throw new InvalidIndexException(row,col,rows,cols);
      return data[row][col];
   }
   int getRows(){
      return rows;
   }
   int getCols(){
      return cols;
   }
   Matrix multiply(Matrix n) throws InvalidMultiplicationException{
		int i,j,k;
      if(cols!=n.getRows()){
         throw new InvalidMultiplicationException();
      }
      Matrix result=new Matrix(rows,n.getCols());
			for(i=0;i<rows;i++)
				for(j=0;j<n.getCols();j++)		
					for(k=0;k<cols;k++)
					   try{
							result.setData(i,j,result.getData(i,j)+data[i][k]*n.getData(k,j));
						}
						catch(InvalidIndexException e){
						   System.out.println(e);
						}
      	return result;
   }
   Matrix add(Matrix n) throws InvalidAdditionException{
      int i,j;
      if(cols!=n.getCols() && rows!=n.getRows()){
         throw new InvalidAdditionException();
      }
      Matrix result=new Matrix(rows,cols);
      for(i=0;i<rows;i++)
         for(j=0;j<cols;j++)    
            try{
               result.setData(i,j,data[i][j]+n.getData(i,j));
            }
            catch(InvalidIndexException e){
               System.out.println(e);
            }
      return result;
   }
   public String toString(){
      String r="";
      int i,j;
		for(i=0;i<rows;i++){
			for(j=0;j<cols;j++)
			   r+=data[i][j]+"\t";
			r+="\n";
		}
      return r;
   }
}
class MatrixTest 
{
   public static void main(String args[]){
      int row1,row2,col1,col2;
      System.out.print("第一个矩阵的行数:");
        try{InputStreamReader  ir;
	    BufferedReader     in;
	  
	   ir=new InputStreamReader(System.in);
	   in=new BufferedReader(ir);
	   //BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      row1=Integer.parseInt(in.readLine()); 
      System.out.print("第一个矩阵的列数:");
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      col1=Integer.parseInt(br.readLine()); 
      System.out.print("第二个矩阵的行数:");
      BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
      row2=Integer.parseInt(br1.readLine()); 
      System.out.print("第二个矩阵的列数:");
      BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
      col2=Integer.parseInt(br2.readLine()); 
      Matrix m=new Matrix(row1,col1);
      Matrix n=new Matrix(row2,col2);
    
         for(int i=0;i<row1;i++)
            for(int j=0;j<col1;j++)
               m.setData(i,j,(int)(Math.random()*10) );
         for(int i=0;i<row2;i++)
            for(int j=0;j<col2;j++)
               n.setData(i,j,(int)(Math.random()*10) );
         Matrix r=m.multiply(n);
         System.out.println("第一个矩阵");
         System.out.println(m);
         System.out.println("第二个矩阵");
         System.out.println(n);
         System.out.println("相乘后矩阵");
         System.out.println(r);
      }
      catch(Exception e){
         System.out.println(e);
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -