/** * The constructor must first check that r * c is equal to the length of arra
ID: 3875604 • Letter: #
Question
/**
* 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)
{
//to be completed
}
/**
* 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)
{
return 0; //to be completed
}
Explanation / Answer
public Matrix(int r, int c, double[] d)
{
int len = d.length;
if( r*c == len){
this.nRow = r;
this.nCol = c;
this.setData(d);
}
else{
this.nRow = 0;
this.nCol = 0;
Arrays.fill(this.data, 0);
}
}
since in matrix minimum size is 1X1 so if the size of the matrix is less than 1 then the matrix is not valid
public double get(int r, int c)
{
if(r < 1 || c < 1)
return 0; //to be completed
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.