JAVA实验二:利用二维数组(double[])实现一个矩阵类:Matrix的相乘、转置、相加等

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fighting123678/article/details/83278749

题目:利用二维数组(double[])实现一个矩阵类:Matrix。

要求提供以下方法:
(1)set(int row, int col, double value):将第row行第col列的元素赋值为value;

(2)get(int row,int col):取第row行第col列的元素;

(3)width():返回矩阵的列数;

(4)height():返回矩阵的行数;

(5)Matrix add(Matrix b):返回当前矩阵与矩阵b相加后的矩阵;

(6)Matrix multiply(Matrix b):返回当前矩阵与矩阵b相乘后的矩阵。

(7)Matrix transpose():返回当前矩阵的转置矩阵;

(8)getMax():返回矩阵中的最大值及其所在行和列;

(9)print():以行和列的形式打印出当前矩阵。

答案:

import java.util.Scanner;

public class Matrix 
{
	protected int row;
	protected int col;
	protected double [][]m;
	public Matrix(int row,int col)
	{
		this.row=row;this.col=col;
		m=new double[row][col];//注意是m,而非[][]m;
	}
////////////////将第row行第col列的元素赋值为value
	public void set(int row,int col,double value)
	{
		if((row>=0&&row<=this.row-1)&&(col>=0&&col<=this.col-1))
		{
			m[row][col]=value;
		}
		else 
			System.out.println("Can not do this change");
	}
////////////////////取第row行第col列的元素	
	public double get(int row,int col)
	{
		if((row>=0&&row<=this.row-1)&&(col>=0&&col<=this.col-1))
		{
			System.out.println(m[row][col]);
			return m[row][col];
		}
		else 
			System.out.println("Can not get the number");
		return 0.0;
	}
//////////////////////返回矩阵的列数
	public int width()
	{
		return this.col;
	}
////////////////返回矩阵的行数
	public int height()
	{
		return this.row;
	}
///////////////返回当前矩阵与矩阵b相加后的矩阵
	public Matrix add(Matrix b)
	{
		if(b.row!=row||b.col!=col)
		{
			System.out.println("Wrong");
			return null;//这里的return值要注意了;
		}
		else
		{
			for(int i=0;i<row;i++)
			{
				for(int j=0;j<col;j++)
				{
					m[i][j]+=b.m[i][j];
				}
			}
			return this;//这里的return值要注意了;
		}
	}
////////////////////返回当前矩阵与矩阵b相乘后的矩阵
	public Matrix multiply(Matrix b)
	{
		if(b.row!=col)
		{
			System.out.println("Wrong");
			return null;//这里的return值要注意了;
		}
		else
		{
			Matrix t=new Matrix(row,b.col);
			for(int i=0;i<row;i++)
			{
				for(int j=0;j<b.col;j++)
				{
					t.m[i][j]=0;
					for(int k=0;k<col;k++)
					{
						t.m[i][j]+=m[i][k]*b.m[k][j];
					}
				}
			}
			return t;
		}
	}
////////////////////返回当前矩阵的转置矩阵///////注意了
	public Matrix transpose()
	{
		Matrix t=new Matrix(row,col);
		for(int i=0;i<col;i++)
		{
			for(int j=0;j<row;j++)
			{
				t.m[i][j]=m[j][i];
			}
		}
		return t;
	}
//////////////////返回矩阵中的最大值及其所在行和列	
	public int[] getMax()
	{
		double max=m[0][0];
		int i1=0,j1=0;
		for(int i=0;i<row;i++)
			for(int j=0;j<col;j++)
			{
				if(m[i][j]>max)
				{
					max=m[i][j];
					i1=i+1;
					j1=j+1;
				}
			}
		System.out.println("行为:"+i1+"列为:"+j1);
		int []a= {i1,j1};
		return a;
	}
//////////////以行和列的形式打印出当前矩阵
	public void print()
	{
		for(int i=0;i<row;i++)
		{
			for(int j=0;j<col;j++)
			{
				if(j==col-1)
					System.out.println(m[i][j]);
				else 
					System.out.print(m[i][j]+" ");
			}
		}
	}
	public static void main(String[] args) 
	{
		Matrix m11=new Matrix(2,2);
		for(int i=0;i<m11.row;i++)
		{
			for(int j=0;j<m11.col;j++)
			{
				m11.m[i][j]=i+1;
			}
		}
		m11.print();
		System.out.println(" ");
		Matrix m1=new Matrix(2,2);
		for(int i=0;i<m1.row;i++)
		{
			for(int j=0;j<m1.col;j++)
			{
				m1.m[i][j]=i+1;
			}
		}
		m1.print();
		System.out.println(" ");
		m11.set(1, 1, 1);
		m11.print();
		System.out.println(" ");
		m11.get(2, 2);
		System.out.println(" ");
		m11.add(m1);
		m11.print();
		System.out.println(" ");
		m11.multiply(m1).print();
		System.out.println(" ");
		m11.transpose().print();
		System.out.println(" ");
		m11.getMax();
	}
}

注意

1、矩阵转置

public Matrix transpose()
	{
		Matrix t=new Matrix(row,col,"NO");
		for(int i=0;i<col;i++)
		{
			for(int j=0;j<row;j++)
			{
				t.m[i][j]=m[j][i];
			}
		}
		return t;
	}

(1)t.m[i][j]=m[j][i];这里注意


2、矩阵相乘

public Matrix multiply(Matrix b)
	{
		if(b.row!=col)
		{
			System.out.println("Wrong");
			return null;//这里的return值要注意了;
		}
		else
		{
			Matrix t=new Matrix(row,b.col,"NO");
			for(int i=0;i<row;i++)
			{
				for(int j=0;j<b.col;j++)
				{
					t.m[i][j]=0;
					for(int k=0;k<col;k++)
					{
						t.m[i][j]+=m[i][k]*b.m[k][j];
					}
				}
			}
			return t;
		}
	}

3、矩阵相加

public Matrix add(Matrix b)
	{
		if(b.row!=row||b.col!=col)
		{
			System.out.println("Wrong");
			return null;//这里的return值要注意了;
		}
		else
		{
			for(int i=0;i<row;i++)
			{
				for(int j=0;j<col;j++)
				{
					m[i][j]+=b.m[i][j];
				}
			}
			return this;//这里的return值要注意了;
		}
	}

4、注意点

(1)

public Matrix(int row,int col,String y)
	{
		if(y=="YES")
		{
			this.row=row;this.col=col;
			m=new double[row][col];//注意是m,而非[][]m;
			Scanner scan=new Scanner(System.in);
			for(int i=0;i<row;i++)
			{
				for(int j=0;j<col;j++)
				{
					double t=scan.nextDouble();
					m[i][j]=t;
				}
			}
		}
		else if(y=="NO")
		{
			this.row=row;this.col=col;
			m=new double[row][col];//注意是m,而非[][]m;
		}
	}

m=new double[row][col];//注意是m,而非[][]m;

(2)

public Matrix add(Matrix b)
	{
		if(b.row!=row||b.col!=col)
		{
			System.out.println("Wrong");
			return null;//这里的return值要注意了;
		}
		else
		{
			for(int i=0;i<row;i++)
			{
				for(int j=0;j<col;j++)
				{
					m[i][j]+=b.m[i][j];
				}
			}
			return this;//这里的return值要注意了;
		}
	}

注意返回值return null;return this;

猜你喜欢

转载自blog.csdn.net/fighting123678/article/details/83278749