前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >声明一个Matrix类表示矩阵

声明一个Matrix类表示矩阵

作者头像
喜欢ctrl的cxk
发布2022-05-07 14:38:15
5180
发布2022-05-07 14:38:15
举报
文章被收录于专栏:Don的成长史

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

代码语言:javascript
复制
       public void print()          //输出Matrix类中所有元素值
       public Matrix transpose()    //返回当前矩阵的转置矩阵
       public boolean isTriangular()  //判断当前矩阵是否是上三角矩阵
       public void add(Matrix b)    //将当前矩阵与矩阵b相加
       public Matrix plus(Matrix b)   //返回当前矩阵与b相加后的矩阵,不改变当前矩阵
代码语言:javascript
复制
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();
	}
}

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

代码语言:javascript
复制
Matrix m = new Matrix();
m.print();
System.out.println();
m.set(10,10,10);
m.print();
代码语言:javascript
复制
System.out.println();
m.set();
m.print();
m.getValue(2, 1);
m.getValue();
//若想要查询的行、列大于矩阵的行数、列数的话,会出现“下标越界”的提示。
代码语言:javascript
复制
m.set();
m.print();
System.out.println("该矩阵的转置矩阵为:");
m.transpose().print();
代码语言:javascript
复制
m.set();
if(m.isTriangular())
{
	System.out.println("该矩阵是上三角矩阵。");
}
else
{
	System.out.println("该矩阵不是上三角矩阵。");
}
代码语言:javascript
复制
m.set();
Matrix n = new Matrix();
n.set(3,3,2);
m.add(n);
m.print();
//这是将当前矩阵与另一个矩阵相加之后的运行界面。
代码语言:javascript
复制
m.set();
m.plus(n).print();
System.out.println("当前矩阵的值依旧没有改变:");
m.print();
//这是将当前矩阵与另一矩阵相加,且不改变当前矩阵的值的运行界面。

这道题到这就写完了。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-10-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档