CASE STUDY^615
returnresult;
}
}
publicMatrix multiply(Matrix two) throwsMatException
// Returns matrix times two.matrix
// Throws MatException if the matrices cannot be multiplied
// or overflow occurs
{
if(matrix[ 0 ].length != two.matrix.length)
throw newMatException("Illegal matrix multiplication.");
else
{
Matrix result =
newMatrix(matrix.length, two.matrix[ 0 ].length);
for(introw = 0 ; row < matrix.length; row++)
for(intcol = 0 ; col < two.matrix[ 0 ].length; col++)
{
result.matrix[row][col] = dotProduct(row,col,two);
if(Double.isInfinite(result.matrix[row][col]))
throw newMatException("Multiplication overflow");
}
returnresult;
}
}
private doubledotProduct(introw, intcol, Matrix two)
// Returns the dot product of row of matrix and column of two.matrix
{
doubletotal = 0 ;
for(intindex = 0 ; index < two.matrix.length; index++)
total = total + matrix[row][index]*two.matrix[index][col];
returntotal;
}
}
Testing:Because the branching statements only check for errors and throw exceptions
if they occur, a clear- or white-box testing strategy is appropriate. The end cases for ad-
dition and subtraction would be for sizes of one by one and something larger. For multi-
plication, the outer dimensions should be one and the inner dimensions should be
something else, and the inner dimensions should be one and the outer dimensions
should be something else. Then the error conditions must all be checked. A fuller test
plan is left as a Case Study Follow-Up exercise.