【发布时间】:2018-07-27 19:02:01
【问题描述】:
当我使用添加矩阵的 add 方法比较具有相同值的 2 个对象时,为什么会出现断言失败错误。下面是我的 add 方法(来自 AbstractMatrix):
public abstract class AbstractMatrix implements Matrix{
private static MatrixFactory factory = new DefaultMatrixFactory();
private Matrix getNewMatrixInstance(Matrix kind,int numberOfRows, int numberOfColumns)throws MatrixException{
return factory.getInstance(kind.getClass(), numberOfRows, numberOfColumns);
}
public static void setFactory(MatrixFactory aFactory){factory = aFactory;}
public final boolean isSameSize(final Matrix m){
return (getNumberOfRows() == m.getNumberOfRows()) && (getNumberOfColumns() == m.getNumberOfColumns());
}
public final Matrix add(final Matrix m) throws MatrixException{
if (!isSameSize(m)){
throw new MatrixException("Trying to add matrices of different sizes");
}
final Matrix result = getNewMatrixInstance(this, getNumberOfRows(), getNumberOfColumns());
for (int row = 0; row < getNumberOfRows(); row++){
for (int column = 0; column < getNumberOfColumns(); column++){
final double value = getElement(row, column) + m.getElement(row, column);
result.setElement(row, column, value);
}
}
return result;
}
这是我的 JUnit 测试:
@Test
void testAdd() throws MatrixException {
Matrix m1 = factory.getInstance(ArrayMatrix.class,new double[][]{{0,0},{0,0}});
Matrix m2 = factory.getInstance(ArrayMatrix.class,new double[][]{{0,0},{0,0}});
Matrix m3 = m1.add(m2);
Matrix m4 = factory.getInstance(ArrayMatrix.class,new double[][]{{0,0},{0,0}});
assertEquals(true, m3.equals(m4));
}
显然 m3 是与 m4 相同的矩阵,但我得到一个 AssertionFailedError 告诉我它是错误的。
其他类:
public class ArrayMatrix extends AbstractMatrix{
private double[][] elements;
public ArrayMatrix(final int rows, final int columns) throws MatrixException{
// Initialise a new matrix with all the elements set to 0.0
if (rows < 0 || columns < 0) {
throw new MatrixException("Negative rows or columns are not allowed");
}
this.elements = new double[rows][columns];
int i,j;
for (i=0;i<rows;i++) {
for (j=0;j<columns;j++) {
elements[i][j]= 0.0;
}
}
}
public ArrayMatrix(double[][] content) throws MatrixException{
// Initialise a new matrix storing the data provided by the
// double[][] parameter.
int rows = content.length;
int columns = content[0].length;
elements = new double[rows][columns];
int i,j;
for (i=0;i<rows;i++) {
for(j=0;j<columns;j++) {
elements[i][j] = content[i][j];
}
}
}
public int getNumberOfRows(){
// Number of rows in matrix
int noRows = elements.length;
return noRows;
}
public int getNumberOfColumns(){
// Number of columns in matrix
int noColumns = elements[0].length;
return noColumns;
}
public double getElement(final int row, final int column) throws MatrixException{
// Return the element at the specified position or throw an exception
if (elements.length<=row) {
throw new MatrixException("Attempt to access invalid element ("+row+","+column+")");
}
if (elements[0].length<column){
throw new MatrixException("Attempt to access invalid element ("+row+","+column+")");
}
else {return elements[row][column];}
}
public void setElement(final int row, final int column, final double value) throws MatrixException{
// Set the element at the specified position or throw an exception
if (elements.length<=row) {
throw new MatrixException("Attempt to access invalid element ("+row+","+column+")");}
if (elements[0].length<column){
throw new MatrixException("Attempt to access invalid element ("+row+","+column+")");}
else {elements[row][column] = value;}
}
}
界面:
public interface Matrix {
public int getNumberOfRows();
public int getNumberOfColumns();
public double getElement(final int row, final int column)throws MatrixException;
public void setElement(final int row, final int column, final double value)throws MatrixException;
public Matrix add(final Matrix m) throws MatrixException;
public Matrix subtract(final Matrix m) throws MatrixException;
public Matrix multiply(final Matrix m) throws MatrixException;
}
【问题讨论】:
-
你为什么使用 assertEquals(
, ) 而不是 assertEquals(m3, m4)? -
@K.Dackow 那也行不通。我收到此错误:imgur.com/a/UWfmZlC
-
请编辑您的问题并将您的代码发布为minimal reproducible example。不要在其他网站上发布指向代码图像、错误图像或代码的链接。当这些链接失效时,您的问题毫无意义。从 10 个不同的页面中抓取您的问题的信息也是一种痛苦。让他人轻松帮助您。
-
@Robert 注意,只是不想用代码堵塞整个页面,
-
这就是人们要求最小示例的原因
标签: java object multidimensional-array junit