patternjavaMinor
addMatrix() method
Viewed 0 times
addmatrixmethodstackoverflow
Problem
Can anyone review my
These are the instructions:
This is a
Make sure you check if the dimensions of this
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[][] add
addMatrix() method to see if I am following the instructions correctly?These are the instructions:
This is a
public method (I'm calling it addMatrix()) that has only one parameter for a DoubleMatrix to add this doubMatrix (not changing this doubMatrix) and the parameter's doubMatrix and return a new DoubleMatrix (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
doubMatrix and the parameter's doubMatrix are the same (if not, return a new DoubleMatrix calling 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[][] add
Solution
-
I think you should operate your matrices as objects, not as two-dimensional arrays, so you could change your method
to
and use the object for further operations.
-
Then your method
could be just a constructor:
-
I really can't understand the purpose of the following code:
if you have no matrix params defined so:
assuming that you will transform your
-
After all this your failed dimension check could just return
I think you should operate your matrices as objects, not as two-dimensional arrays, so you could change your method
public double[][] addMatrix(double[][] doubMatrix)to
public DoubleMatrix add(DoubleMatrix secondMatrix)and use the object for further operations.
-
Then your method
makeDoubleMatrix(int row, int col)could be just a constructor:
public DoubleMatrix(int row, int col)-
I really can't understand the purpose of the following code:
int row;
int col;
if(row > 0 && col > 0)if you have no matrix params defined so:
public DoubleMatrix(){
this(1,1);
}assuming that you will transform your
makeDoubleMatrix() to constructor.-
After all this your failed dimension check could just return
new DoubleMatrix() which will construct and new a 1x1 DoubleMatrix.Code Snippets
public double[][] addMatrix(double[][] doubMatrix)public DoubleMatrix add(DoubleMatrix secondMatrix)makeDoubleMatrix(int row, int col)public DoubleMatrix(int row, int col)int row;
int col;
if(row > 0 && col > 0)Context
StackExchange Code Review Q#18026, answer score: 4
Revisions (0)
No revisions yet.