用java语言实现一个类Matrix,用来表示矩阵,所实现的类应该支持下面的操作:
●根据给定的行和列的数目创建一个矩阵。
●在某个给定的行和列的位置上取得或设置元素c
●对两个相容的矩阵进行相加和相乘运算:
并提供一个相应的测试程序。  
这是我在一本书上看到的题,完全没头绪,请达人帮忙做一下.谢谢

解决方案 »

  1.   

    这个有什么难的吗?矩阵就是一个多维数组,也就是说对矩阵的处理就是java中对多维数组的处理。关于矩阵的运算法则,数学上不是有公式嘛,把数学公式用java语言实现一下就可以了。
      

  2.   

    package com.jeremy;public class Marix { /**
     * @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));
    }
       其他方法.....
    }
    给我分  一分也不能少..我穷死了..
      

  3.   

    public class Marix { /**
     * @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("");
    }
    }
      

  4.   

    发现还是写错了一些了,那个col和row不能直接取得,呵呵
      

  5.   


    用native调用matlab这个是最经典的了的根本没法再经典了