用java语言实现一个类Matrix,用来表示矩阵,所实现的类应该支持下面的操作:
●根据给定的行和列的数目创建一个矩阵。
●在某个给定的行和列的位置上取得或设置元素c
●对两个相容的矩阵进行相加和相乘运算:
并提供一个相应的测试程序。
这是我在一本书上看到的题,完全没头绪,请达人帮忙做一下.谢谢
●根据给定的行和列的数目创建一个矩阵。
●在某个给定的行和列的位置上取得或设置元素c
●对两个相容的矩阵进行相加和相乘运算:
并提供一个相应的测试程序。
这是我在一本书上看到的题,完全没头绪,请达人帮忙做一下.谢谢
* @param args
*/
private int[][] mar;
public Marix(int i,int j){
mar=new int[i][j];
}
public int getElement(int i,int j){
int value=0;
value=mar[i][j];
return value;
}
public void setElement(int i,int j,int val){
mar[i][j]=val;
}
public static void main(String[] args){
Marix m=new Marix(3,4);
m.setElement(1,2,3);
System.out.println(m.getElement(1,2));
}
其他方法.....
}
给我分 一分也不能少..我穷死了..
* @param args
*/
private int[][] mar;
private int row;
private int col;
public Marix(int i,int j){
this.mar = new int[i][j];
this.row = i;
this.col = j;
} public int getElement(int i,int j) throws Exception{
if(i>this.row || j>this.col)
throw new Exception("index out of bound.");
return mar[i][j];
} public void setElement(int i,int j,int val) throws Exception{
if(i>this.row || j>this.col)
throw new Exception("index out of bound.");
mar[i][j]=val;
} public Marix add(Marix another) throws Exception
{
if(this.row != another.row || this.col != another.col)
throw new Exception("the row or col is not equal.");
Marix ret = new Marix(this.row, this.col);
for(int i=0; i<this.row; i++)
{
for(int j=0; j<this.col; j++)
ret.setElement(i, j, this.getElement(i, j) + another.getElement(i, j));
}
return ret;
}
public Marix multiply(Marix another) throws Exception
{
if(this.row != another.col)
throw new Exception("the row is not equal to col.");
Marix ret = new Marix(this.row, this.col);
int temp =0;
for(int i=0; i<this.row; i++)
{
temp = 0;
for(int j=0; j<this.row; j++)
{
for(int k=0; k<another.col; k++)
temp += this.getElement(i, k) * another.getElement(k, j);
ret.setElement(i, j, temp);
}
}
return ret;
} public void display()
{
for(int i=0; i<this.row; i++)
{
for(int j=0; j<this.col; j++)
System.out.printf(mar[i][j]+ " ");
System.out.println("");
}
} public static void main(String[] args) throws Exception{
Marix m1=new Marix(4,4);
m1.setElement(1,2,3);
m1.display();
System.out.println(""); Marix m2 = new Marix(4,4);
m2.setElement(2,3,4);
m2.display();
System.out.println(""); m1= m1.add(m2);
m1.display();
System.out.println(""); m1= m1.multiply(m2);
m1.display();
System.out.println("");
}
}
用native调用matlab这个是最经典的了的根本没法再经典了