CASE STUDY
606
The sum obtained by multiplying a row by a column is called the dot product. Another
way of stating multiplications is that
E[i][j] = dot product of row i and column j
Now that we understand the semantics of the operations, we are ready to determine
the responsibilities
Scenarios:If we were the users of the class, what facilities would we need? First, of
course, we would need to create the matrix itself, by telling it how many rows and
columns there should be. Next, we would need a way to put values into the slots of the
matrix. We would probably want to print out the matrix after it is constructed to
confirm that the values are correct. At that point, we would be ready to apply one of the
binary operations—say, addition. We would send the message to one matrix to add
itself to the matrix in the message parameter and return the result to us. We would fol-
low the same process for subtraction and multiplication.
Are there any states of the matrix object we might want to know about? Well, it
might be useful to view the value at a particular matrix position, so let’s add that task
to the list of responsibilities. We also might want to access the number of rows and
columns in a matrix.
What about error conditions? Matrix addition and subtraction require that the
matrices have the same dimensions, and matrix multiplication requires that the num-
ber of columns in the first matrix equal the number of rows in the second matrix. It
makes sense for the matrix that is told to perform an operation to confirm that the op-
eration is legal before complying with the request. If the operation is not legal, the ma-
trix can throw an exception.
CRC Card:We can summarize our observations in a CRC card:
C = 1
0
1
2
2
1
3
1
0
4
3
0
D = 1
0
2
4
1
1
3
2
C * D = 23
14
1
20
11
2