Question: My question is Constructor of matrix public class Matrix { private int nRow, nCol; private double[] data; // must store as linear array unless 0

My question is Constructor of matrix

public class Matrix

{

private int nRow, nCol;

private double[] data; // must store as linear array unless 0

//getter for NRow

public int getNRow()

{

return nRow;

}

//getter for NCol

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 = 0;

else

nRow = r;

}

/**

* 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 = 0;

else

nCol = c;

}

/**

*

* @return true if both nRow and nCol are zero, false otherwise

*/

public boolean isEmpty()

{

if(nRow == 0 && nCol ==0)

return true;

else

return false;

}

/**

* 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()

{

//to be completed

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!