Can anyone review my addMatrix() method to see if I am following the instructions correctly?
These are the instructions:
This is a
publicmethod (I'm calling itaddMatrix()) that has only one parameter for aDoubleMatrixto add thisdoubMatrix(not changing thisdoubMatrix) and the parameter'sdoubMatrixand return a newDoubleMatrix(you'll need a local 2-dim. array to store the result of adding and pass to the constructor).Make sure you check if the dimensions of this
doubMatrixand the parameter'sdoubMatrixare the same (if not, return a newDoubleMatrixcalling the first constructor passing 1, 1).
I think I wrote the part where it said calling first constructor passing 1,1 wrong.
package homework3;
public class DoubleMatrix
{
private double[][] doubMatrix;
public DoubleMatrix()
{
int row;
int col;
if(row > 0 && col > 0)
{
makeDoubMatrix(1,1);
}
else
{
row = 1;
col = 1;
}
}
public DoubleMatrix(double[][] tempArray)
{
if(tempArray != null)
{
for(int i = 0; i < tempArray.length-1;i++)
{
if(tempArray[i].length == tempArray[i+1].length)
{
doubMatrix = tempArray;
}
}
}
else
{
makeDoubMatrix(1,1);
}
}
public int getDim1()
{
return doubMatrix.length;
}
public int getDim2()
{
return doubMatrix[0].length;
}
private void makeDoubMatrix(int row, int col)
{
double[][] tempArray = new double[row][col];
for(int i = 0;i < tempArray.length;i++)
for(int j = 0;j < tempArray[i].length;j++)
{
tempArray[i][j] = Math.random() * (100);
} //end for
tempArray = doubMatrix;
}
public double[][] addMatrix(double[][] doubMatrix)
{
this. doubMatrix = doubMatrix;
double[][] tempArray = null;
if(this.doubMatrix.length == doubMatrix.length)
if(this.doubMatrix[0].length == doubMatrix[0].length)
{
for(int i = 0; i< this.doubMatrix.length;i++)
for(int j = 0; j< this.doubMatrix[i].length;j++ )
{
tempArray[i][j] = this.doubMatrix[i][j] + doubMatrix[i][j];// add two matrices
}//end for
}
else
{
return tempArray = new double[1][1];
}
return tempArray;
}
}