Java "The type DenseMatrix must implement the inherited abstract method Matrix.multiply(Vector)"

标签 java abstract-class

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

The type DenseMatrix must implement the inherited abstract method 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 类。方法 DenseMatrix.multiply(Vector)import java.util.Vector; (从文件开头开始)实际上是 DenseMatrix.multiply(java .util.Vector)。如果您在包 mypackage 中有另一个 Vector 类,并且您在 Matrix 接口(interface)中使用该类,则该方法将为 Matrix .multiply(mypackage.Vector)multiply(java.util.Vector)multiply(mypackage.Vector) 不同。您需要确保对 DenseMatrix 和 Matrix 使用相同的 Vector 类。

关于Java "The type DenseMatrix must implement the inherited abstract method Matrix.multiply(Vector)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33201748/

相关文章:

java - 我怎样才能将这个可怕的具有多个泛型的抽象java类重构为 "better"?

python - 是否可以在 Python 中创建抽象类?

java - 在查询中使用带间隔的 prepareStatement 时出错

grails - 在抽象域上使用Grails createCriteria

java - 如何在Spring MVC中创建属性文件的List对象?

java - 为什么当我尝试在数据库中插入新对象时出现错误?

java - 一个 ArrayList 中的多个对象类型

java - 在 Java 中向抽象基类添加成员的影响

java - 如何在 eclipse kepler (Sts 3.4) 中添加 JBOSS EAP 7.2 服务器,因为 7.2 没有可用的适配器服务

java - ArrayList 内的 ArrayList 被覆盖