声明一个Matrix类表示矩阵

声明Matrix类表示矩阵,使用二维数组存储矩阵元素,实现以下方法:

       public void print()          //输出Matrix类中所有元素值

       public Matrix transpose()    //返回当前矩阵的转置矩阵

       public boolean isTriangular()  //判断当前矩阵是否是上三角矩阵

       public void add(Matrix b)    //将当前矩阵与矩阵b相加

       public Matrix plus(Matrix b)   //返回当前矩阵与b相加后的矩阵,不改变当前矩阵

import java.util.Scanner

public class Matrix {
	private int row,col;  //行和列
	private int a[][];
	public Matrix()					 //默认构造方法
	{
		a = new int[3][3];
		this.row = 3;
		this.col = 3;
		for(int i=0;i<this.row;i++)
		{
			for(int j=0;j<this.col;j++)
			{
				a[i][j] = 0;              //默认值全为0
			}
		}
	}
	public Matrix(int row,int column)   //带参数的构造方法
	{
		a = new int[row][column];
		this.row = row;
		this.col = column;
		for(int i=0;i<this.row;i++)
		{
			for(int j=0;j<this.col;j++)
			{
				a[i][j] = 0;              //默认值全为0
			}
		}
	}
	public void set(int row,int column,int value)		//用来设置Matrix类的值,将第row行第col列的元素赋值为value
	{
		if(row>=0&&column>=0)
		{
			this.row = row;
			this.col = column;
			a = new int[row][column];
			for(int i=0;i<this.row;i++)
			{
				for(int j=0;j<this.col;j++)
				{
					this.a[row][col] = value;
				}
			}
		}
	}
	public void set()   //set()函数重载
	{
		Scanner in = new Scanner(System.in); 
		System.out.print("请输入该矩阵的行数:");
		this.row = in.nextInt();
		System.out.print("请输入该矩阵的列数:");
		this.col = in.nextInt();
		int count = row*col;
		System.out.println("请输入"+count+"个元素");
		for(int i=0;i<this.row;i++)
		{
			for(int j=0;j<this.col;j++)
			{
				this.a[i][j] = in.nextInt();
			}
		}
	}
	public void getValue(int x,int y)	//输出第x行第y列的元素
	{
		if((x>0&&x<=this.row)&&(y>0&&y<=this.col))
		{
			for(int i=0;i<this.row;i++)
			{
				if(x-1==i)
				{
					for(int j=0;j<this.col;j++)
					{
						if(y-1==j)
						{
							System.out.print("第"+x+"行,第"+y+"列的元素是");
							System.out.println(this.a[i][j]);
						}	
					}
				}
			}
		}
	}
	public void getValue()  	//getValue()函数重载
	{
		Scanner in = new Scanner(System.in); 
		System.out.print("请依次输入您要查询的元素所在的行、列:");
		int x = in.nextInt();
		int y = in.nextInt();
		if((x>0&&x<=this.row)&&(y>0&&y<=this.col))
		{
			for(int i=0;i<this.row;i++)
			{
				if(x-1==i)
				{
					for(int j=0;j<this.col;j++)
					{
						if(y-1==j)
						{
							System.out.print("第"+x+"行,第"+y+"列的元素是");
							System.out.println(this.a[i][j]);
						}
					}
				}
			}
		}
	}
	public int height()
	{
		return this.row;
	}
	public int width()
	{
		return this.col;
	}
	public void print()				//输出Matrix类中所有元素值
	{
		for(int i=0;i<this.row;i++)
		{
			for(int j=0;j<this.col;j++)
			{
				System.out.print(a[i][j]+" ");              
			}
			System.out.println();
		}
	}
	public Matrix transpose()			//返回当前矩阵的转置矩阵
	{
		Matrix b = new Matrix(col,row);		
		for(int i=0;i<this.row;i++)
		{
			for(int j=0;j<this.col;j++)
			{
				b.a[j][i]=this.a[i][j];
			}
		}
		return b;
	}
	public boolean isTriangular()		//判断当前矩阵是否是上三角矩阵
	{
		for(int i=0;i<this.row;i++)
		{
			for(int j=this.col-i;j<this.col;j++)
			{
				if(i>j&&a[i][j]!=0)
				{
					return false;
				}
			}
		}
		return true;
	}
	public void add(Matrix b)     		//将当前矩阵与矩阵b相加
	{
		if(this.row!=b.row||this.col!=b.col)
		{
			System.out.println("Error!The matrixes must be the same in terms with size.");
		}
		for(int i=0;i<this.row;i++)
		{
			for(int j=0;j<this.col;j++)
			{
				this.a[i][j] += b.a[i][j];
			}
		}
	}
	public Matrix plus(Matrix b)		//返回当前矩阵与b相加后的矩阵,不改变当前矩阵
	{
		if(this.row!=b.row||this.col!=b.col)
		{
			System.out.println("Error!");
			return null;
		}
		Matrix c = new Matrix(row,col);
		for(int i=0;i<row;i++)
		{
			for(int j=0;j<col;j++)
			{
				c.a[i][j] = this.a[i][j]+b.a[i][j];
			}
		}
		return c;
	}
	public static void main(String[] args) {
		Matrix m = new Matrix();
		m.print();
		System.out.println();
		m.set(10,10,10);
		m.print();
		System.out.println();
		m.set();
		m.print();
		m.getValue(2, 1);
		m.getValue();
		m.set();
		m.print();
		System.out.println("该矩阵的转置矩阵为:");
		m.transpose().print();
		m.set();
		if(m.isTriangular())
		{
			System.out.println("该矩阵是上三角矩阵");
		}
		else
		{
			System.out.println("该矩阵不是上三角矩阵");
		}
		Matrix n = new Matrix();
		n.set(3,3,2);
		m.add(n);
		m.print();
		m.set();
		m.plus(n).print();
		System.out.println("当前矩阵的值依旧没有改变:");
		m.print();
	}
}

程序在执行主函数的以下语句时,运行界面如图所示。

Matrix m = new Matrix();
m.print();
System.out.println();
m.set(10,10,10);
m.print();

System.out.println();
m.set();
m.print();
m.getValue(2, 1);
m.getValue();
//若想要查询的行、列大于矩阵的行数、列数的话,会出现“下标越界”的提示。

m.set();
m.print();
System.out.println("该矩阵的转置矩阵为:");
m.transpose().print();

  

m.set();
if(m.isTriangular())
{
	System.out.println("该矩阵是上三角矩阵。");
}
else
{
	System.out.println("该矩阵不是上三角矩阵。");
}

 

m.set();
Matrix n = new Matrix();
n.set(3,3,2);
m.add(n);
m.print();
//这是将当前矩阵与另一个矩阵相加之后的运行界面。

m.set();
m.plus(n).print();
System.out.println("当前矩阵的值依旧没有改变:");
m.print();
//这是将当前矩阵与另一矩阵相加,且不改变当前矩阵的值的运行界面。

这道题到这就写完了。

猜你喜欢

转载自blog.csdn.net/weixin_42449444/article/details/83184083