CASE STUDY
612
Before we collect these methods into a complete class, have we forgotten anything?
The class has two constructors, three knowledge methods, a transformer that sets a
value in a specified row and column—but what happens if the specified row and
column are not within the bounds of the matrix? The class should check for this error
and throw an exception. What about the binary matrix operations? We know that these
numeric operations can cause underflow and overflow. If underflow occurs, the values
are automatically set to zero; if overflow occurs, the values are set to signed infinity. In
the case of overflow, the operation should throw an exception. TheDoubleclass has a
booleanclass method calledisInfinitethat we can use to determine if overflow has oc-
curred. In the following class, overflow is checked, but the other error conditions are left
to a Case Study Follow-Up exercise.
//************************************************
// Class MatException is thrown from class Matrix
// under certain error conditions.
//************************************************
packagematrix;
public classMatException extendsException
{
publicMatException()
{
super();
}
publicMatException(String message)
{
super(message);
}
}
//**************************************************************
// This class provides a basic matrix object. There are two
// constructors, one transformer, three knowledge methods, a
// print, and three binary operations. Certain errors are
// checked and MatException is thrown if they arise.
//**************************************************************
packagematrix;
importjava.io.*;
public classMatrix
{
// Private data field
private double[][] matrix;
publicMatrix(introws, intcolumns)