I have a class Mold
with the following constructor
public Mold(int[][] mold){
this.mold = mold;
}
I also have a method sum()
which looks like this:
public int sum(){
int sum = 0;
for( int i = 0; i <matrix.length; i++) {
sum += matrix[i][i];
}
return sum;
}
What I am trying to do is compute the sum of all the elements in Mold
but I am unsure as to what to put in the sum += matrix[i][i];
part in place of 'i'. I might even be doing it the complete wrong way. I have been stuck on this for a while and I cannot seem to figure it out.
An example of the output I would like to get is: int[][] test1 = { { 10, 5 }, { 2, 8 } };
should give 25 one the sum method has been applied.
If someone could push me in the right direction that would be great! Many thanks,