我正在为学校做一个项目,我们给它一个抽象类Matrix
,我们必须在两个不同的类中实现Matrix
的方法。我正在上课DenseMatrix
而且我一直收到错误:
DenseMatrix类型必须实现继承的抽象方法Matrix.multiply(Vector)
即使我在multiply(Vector)
中有方法DenseMatrix
。
代码(先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();
}
答案 0 :(得分:1)
您可能使用了两个不同的Vector
类。方法DenseMatrix.multiply(Vector)
和import java.util.Vector;
(从文件开头)实际上是DenseMatrix.multiply(java.util.Vector)
。如果您在包Vector
中有另一个mypackage
类,并且您在Matrix
界面中使用该类,则该方法将为Matrix.multiply(mypackage.Vector)
。 multiply(java.util.Vector)
与multiply(mypackage.Vector)
不同。您需要确保为DenseMatrix和Matrix使用相同的Vector类。