编写一个Java程序,实现矩阵类,至少包含三个属性:行数,列数,存储数据的二维数组;至少两个构造函数,实现矩阵的数乘,加法,减法以及两个矩阵的乘法。

package juzhen;

public class juzhenlei {
private int col;
private int row;
private int m;
int data[][];
public juzhenlei() {

}
public juzhenlei(int m) {
	this.m=m;
}
public juzhenlei(int data[][]) {
	this.row=data.length;
	this.col=data[0].length;
	this.data=new int [row][col];
	for(int i=0;i<this.row;i++)
		for(int j=0;j<this.col;j++)
		{
			this.data[i][j]=data[i][j];
		}
	
}
public int getCol() {
	return col;
}
public void setCol(int col) {
	this.col = col;
}
public int getRow() {
	return row;
}
public void setRow(int row) {
	this.row = row;
}
public int[][] getData() {
	return data;
}
public void setData(int[][] data) {
	this.data = data;
}
public int get(int i,int j) {
	return this.data[i][j];
}
public void setM(int m) {
	this.m = m;
}
public int getM() {
	return m;
}
public static juzhenlei shucheng(juzhenlei a,juzhenlei m) {
	juzhenlei c=null;
	int [][]data=new int [a.getRow()][a.getCol()];
	int n=m.getM();
	for(int i=0;i<a.getRow();i++)
		for(int j=0; j<a.getCol();j++)
			{
				data[i][j]=n*a.get(i, j);
			}
	c=new juzhenlei(data);
	return c;
}
public static juzhenlei add(juzhenlei a,juzhenlei b) {
	juzhenlei c =null;
	if(a.getRow()!=b.getRow()||a.getCol()!=b.getCol())
	{
		System.out.println("两个矩阵不能相加");
		return c;
	}
	int data[][]=new int [a.getRow()][a.getCol()];
	for(int i=0;i<a.getRow();i++)
		for(int j=0;j<a.getCol();j++)
		{
			data[i][j]=a.get(i,j)+b.get(i,j);
		}
	c=new juzhenlei(data);
	return c;
}
public static juzhenlei sub(juzhenlei a,juzhenlei b) {
	juzhenlei c =null;
	if(a.getRow()!=b.getRow()||a.getCol()!=b.getCol())
	{
		System.out.println("两个矩阵不能相减");
		return c;
	}
	int data[][]=new int [a.getRow()][a.getCol()];
	for(int i=0;i<a.getRow();i++)
		for(int j=0;j<a.getCol();j++)
		{
			data[i][j]=a.get(i,j)-b.get(i,j);
		}
	c=new juzhenlei(data);
	return c;
}
public static juzhenlei multiply(juzhenlei a,juzhenlei b) {
	juzhenlei c=null;
	if(a.getCol()!=b.getRow())
	{
		System.out.println("两个矩阵不能相乘");
		return c;
	}
	int [][]data=new int [a.getRow()][b.getCol()];
	for(int i=0;i<a.getRow();i++)
		for(int j=0; j<b.getCol();j++)
			for(int k=0;k<a.getCol();k++)
			{
				data[i][j]+=a.get(i, k)*b.get(k, j);
			}
	c=new juzhenlei(data);
	return c;
}
public void print() {
	for(int i=0;i<this.row;i++) {
		for(int j=0;j<this.col;j++)
			System.out.print(" "+this.data[i][j]);
	System.out.println();}
}
public static void main(String[] args) {
	// TODO Auto-generated method stub
	
	int a[][]={{5,5,5},{6,6,6},{7,7,7}};
	juzhenlei pm= new juzhenlei(a);
	pm.print();
	System.out.println();
	int b[][]={{1,1,1},{2,2,2},{3,3,3}};
	juzhenlei pn= new juzhenlei(b);
	pn.print();
	System.out.println();
	int g=2;
	juzhenlei p= new juzhenlei(g);
	System.out.println();
	juzhenlei mc = juzhenlei.shucheng(pm,p);
	mc.print();
	System.out.println();
	juzhenlei md = juzhenlei.add(pm,pn);
	md.print();
	System.out.println();
	juzhenlei me = juzhenlei.sub(pm,pn);
	me.print();
	System.out.println();
	juzhenlei mf = juzhenlei.multiply(pm,pn);
	mf.print();
}

}

发布了6 篇原创文章 · 获赞 0 · 访问量 818

猜你喜欢

转载自blog.csdn.net/weixin_46424591/article/details/105176970