不确定如何在被视为2D数组的1D数组中设置值

时间:2017-09-07 08:09:25

标签: java arrays

将元素设置为行r和列c的值为v该方法必须检查r和c是否在nRow()和nCol()的范围内。如果r或c超出矩阵维度的范围,它就不应该做任何事情。

我明白他们在问我什么,但不确定我的代码在做什么是不正确的。

public class Matrix {
private int nRow, nCol;
private double[] data; // must store as linear array unless 0

// getter for NRow - do not modify
public int getNRow() {
    return nRow;
}

// getter for NCol - do not modify
public int getNCol() {
    return nCol;
}

/**
 * set nRow to r if r is not negative, otherwise set nRow to 0
 * 
 * @param r:
 *            value to be assigned to nRow
 */
public void setNRow(int r) {
    if (r > 0)
        nRow = r;
    else
        nRow = 0;
    // to be completed
}

/**
 * set nCol to c if c is not negative, otherwise set nCol to 0
 * 
 * @param c:
 *            value to be assigned to nCol
 */
public void setNCol(int c) {
    if (c > 0)
        nCol = c;
    else
        nCol = 0;
    // to be completed
}

/**
 * 
 * @return true if numberOfRow() and numberOfColumn are zero
 */
public boolean isEmpty() {
    if (nRow == 0)
        if (nCol == 0)
            return true;

    return false;
    // to be completed
}

/**
 * DO NOT MODIFY if arr is null, instance array data should become null,
 * otherwise if arr.length is not equal to nRow * nCol, set nRow, nCol to 0 and
 * data to null, otherwise instantiate instance array data to be of the same
 * size as arr. then copy each item of arr into data. IMPORTANT: do not
 * re-declare data!
 * 
 * @param arr
 */
public void setData(double[] arr) {
    if (arr == null)
        data = null;
    else {
        if (nRow * nCol != arr.length) {
            nRow = 0;
            nCol = 0;
            data = null;
            return;
        }
        data = new double[arr.length];
        for (int i = 0; i < arr.length; i++) {
            data[i] = arr[i];
        }
    }
}

/**
 * Default constructor. instance variables nRow and nCol should be set to 0
 * using the setters. the data member data should be set to null
 */
public Matrix() {
    nRow = 0;
    nCol = 0;
    data = null;
    // to be completed
}

/**
 * Constructor a matrix which has r rows and c columns. data member nRow and
 * nCol should be set accordingly using setters. AFTER THAT, instance array data
 * should be instantiated to size nRow*nCol
 * 
 * @param r:
 *            number of rows
 * @param c:
 *            number of columns
 */
public Matrix(int r, int c) {
    setNRow(r);
    setNCol(c);
    // to be completed
}

/**
 * 
 * @return true if the matrix is a square matrix (a matrix for which number of
 *         rows and number of columns is the same)
 */
public boolean isSquare() {
    if (nRow == nCol)
        return true;
    return false; // to be completed
}

/**
 * 
 * @param other
 * @return true if calling object and parameter object have the same dimensions
 *         (they both have the same number of rows compared to each other, and
 *         the same number of columns compared to each other), false otherwise
 */
public boolean sameDimensions(Matrix other) {
    if (this.nRow == other.nRow)
        return true;
    return false; // to be completed
}

/**
 * @param r
 * @return true if r is a valid row number, false otherwise only row numbers 0
 *         to nRow-1 (inclusive on both sides) are valid
 */
public boolean isValidRowNumber(int r) {
    if (r < 0 || r > nRow - 1)
        return false;
    return true; // to be completed
}

/**
 * @param c
 * @return true if c is a valid column number, false otherwise only column
 *         numbers 0 to nCol-1 (inclusive on both sides) are valid
 */
public boolean isValidColumnNumber(int c) {
    if (c < 0 || c > nCol - 1)
        return false;
    return true; // to be completed
}

/**
 * The constructor must first check that r * c is equal to the length of array
 * d. if this requirement does not meet, member nRow and nCol should be set to 0
 * and data should be set to null. if the requirement is met, nRow should be set
 * to r and nCol should be set to c. d should be copied into data using
 * setData(double[] arr) method
 * 
 * @param r:
 *            number of rows
 * @param c:
 *            number of columns
 * @param d:
 *            array d to populate data
 */
public Matrix(int r, int c, double[] d) {
    if (r * c == d.length) {
        nRow = r;
        nCol = c;
        setData(d);
    } else {
        nRow = 0;
        nCol = 0;
        data = null;
    }
}

/**
 * The method must check that r and c are valid row numbers and column numbers
 * respectively. Note that row and column numbers begin with 0. It should return
 * 0 if r or c is out of range of the dimension of the matrix
 * 
 * @param r
 * @param c
 * @return an element of the Matrix at row r and column c
 */
public double get(int r, int c) {
    if (isValidRowNumber(r) && isValidColumnNumber(c))
        return data[(r * nCol) + c];
    return 0; // to be completed
}

/**
 * setting and element to row r and column c with value v The method must check
 * that r and c are in the range of numberOfRow() and numberOfColumn(). It
 * should not do anything if r or c is out of range of the dimension of the
 * matrix
 * 
 * @param r
 * @param c
 * @param v
 */
public void set(int r, int c, double v) {
    if(r >= 0 && r <= nRow - 1 && (c >= 0) && c<=nCol - 1)
            data[(r * nCol) + c] = v; 
    // to be completed
}

/**
 * DO NOT MODDIFY
 */
public String toString() {
    String result = "";
    for (int r = 0; r < nRow; r++) {
        result = result + "| ";
        for (int c = 0; c < nCol; c++) {
            result = result + String.format("%7.2f ", get(r, c));
        }
        result = result + "|\n";
    }
    return result;
}

}

提前致谢

Matrix m = new Matrix(2, 3);
        for(int r = 0; r < 2; r++)
        {
            for(int c = 0; c < 3; c++)
            {
                assertEquals(0, m.get(r, c), 0.01); //before TEST FAILED HERE
                m.set(r, c, (r+1)*(c+1));
                assertEquals((r+1)*(c+1), m.get(r, c), 0.01); //after
            }
        }

0 个答案:

没有答案