【问题标题】:Java "The type DenseMatrix must implement the inherited abstract method Matrix.multiply(Vector)"Java“类型 DenseMatrix 必须实现继承的抽象方法 Matrix.multiply(Vector)”
【发布时间】:2015-10-18 18:37:14
【问题描述】:

我正在为学校做一个项目,其中给我们一个抽象类Matrix,我们必须在两个不同的类中实现Matrix 的方法。我正在上课DenseMatrix,但我不断收到错误:

类型 DenseMatrix 必须实现继承的抽象方法 Matrix.multiply(Vector)

即使我在DenseMatrix 中有一个方法multiply(Vector)。

代码(先是DenseMatrix,然后是Matrix):

import java.util.Vector;
import java.util.*;

public class DenseMatrix implements Matrix{

    private int size=0; // size of the matrix- number of rows/columns
    private int nnz=0; // number of non-zero elements
    private double[][] data;

    // Constructor used to initialize the matrix (all elements to zero)
    DenseMatrix(int size){
        this.size=size;
        data=new double[size][size]; // all elements take the values 0.0d
    }

    // Constructor with Random generator (using nnz random non-zero numbers           between 0.01<= x < 1.01
    // on any random row,column coordinates)
    DenseMatrix(int size,int nnz){
        ///=====> TO COMPLETE <===========///
        Random r = new Random();
        for(int i =0; i<nnz;i++){
            double randomValue = (double)(.01 + r.nextDouble());
            int xCord = (int)(r.nextInt()*size);
            int yCord = (int)(r.nextInt()*size);
            data[xCord][yCord] = randomValue;
        }
    }

    // Constructor from any other matrix storage using the interface to Dense storage
    DenseMatrix(Matrix A){
        this.size=A.getSize();
        this.nnz=A.getNnz();
        data=new double[size][size];
        for (int i = 0; i < size; i++){
            for (int j=0;j<size;j++)
            data[i][j]=A.get(i,j);
        }
    }

    // get the size of the matrix
    public int getSize(){return size;}

    // get the number of non-zero elements
    public int getNnz(){return nnz;}

    // Assign the value x to element i,j
    public void set(int i,int j, double x){
        if ((data[i][j]==0)&&(x!=0.0)) nnz++;
        if ((data[i][j]!=0)&&(x==0.0)) nnz--;
        data[i][j]=x;
    }

    // get the value of the element i,j
    public double get(int i, int j){
        return(data[i][j]);}

    // Print matrix using a specific format
    public void display(){
        System.out.println();
        System.out.println("Display in dense format");
        for (int i = 0; i < size; i++){
            for (int j=0;j<size;j++)
                System.out.format("%.4f ",get(i,j));
            System.out.println();
        }
        System.out.println();
    }

    // get the elements of the diagonal
    public double[] getDiagonal(){
        ///=====> TO COMPLETE <===========///
        double[] diag = new double[size];
        for(int i = 0;i<size;i++){
            diag[i] = get(i,i);}
        return diag;
    }

    public Vector multiply(Vector B){
        ///=====> TO COMPLETE <===========///
        double temp[] = new double[size];
        Vector result = new Vector(size);
        for(int y = 0;y<size;y++){
            for(int x = 0;x<size;x++){
                temp[y] = temp[y] + get(x,y);
            }
        }
        for(int i = 0;i<size;i++){
            result.set(i,(double)B.get(i) * temp[i]);
        }
        return result;
    }

}



public interface Matrix {
    // Assign the value x to element i,j
    void set(int i,int j, double x);
    // get the value of the element i,j
    double get(int i, int j);
    // Extract the diagonal of the matrix
    double[] getDiagonal();
    // get the size of the matrix-- number of rows
    int getSize();
    // get the number of non-zero elements
    int getNnz();
    // Multiply a matrix by a vector
    Vector multiply(Vector B);
    // Print matrix using a specific format
    void display();
}

【问题讨论】:

  • 您是否有另一个类Vector 用于Matrix 接口,而不是来自java.util 的类?也许是测试或其他项目遗留下来的东西?
  • 是的,我确实有一个矢量类,但它不使用矩阵接口
  • 你拥有的 Vector 类与 java.util.Vector 类不同?
  • 是的!谢谢你。删除了我拥有的矢量类,现在我没有错误!
  • 请不要自行破坏您的帖子。

标签: java abstract-class


【解决方案1】:

您可能使用了两个不同的Vector 类。方法DenseMatrix.multiply(Vector) 和import java.util.Vector;(从文件开头)实际上是DenseMatrix.multiply(java.util.Vector)。如果您在包mypackage 中有另一个Vector 类,并且您在Matrix 接口中使用它,则方法将是Matrix.multiply(mypackage.Vector)。 multiply(java.util.Vector) 与 multiply(mypackage.Vector) 不同。您需要确保对 DenseMatrix 和 Matrix 使用相同的 Vector 类。

【讨论】:

  • 所以我确实有另一个 Vector 类,但为了项目的目的,必须重新定义该类。无论如何,我可以使用我的 Vector 类而不是 java.util 中的类吗?
  • 只需删除 java.util.Vector 的导入并将其替换为 Vector 类的导入即可。如果您的 Vector 类与 DenseMatrix 在同一个包中,您可能不需要导入语句。 (导入 java.util.* 在导入 Vector 时可能会导致问题,您可能希望单独从 java.util 导入类,而不是使用通配符 *。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-12
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多