- In this exercise you will practice encapsulation in class
design.
- You are given an API for a class Matrix that represents matrices
of numeric values and the operations that can be performed on
them.
- You will produce an implementation of Matrix that
hides the internal matrix representation details.
- To start, download this zipped NetBeans project:
CS_2511_Class_Design.zip
- Then, unzip it and open the CS_2511_Class_Design project
in NetBeans
This section presents documentation of the application programming
interface (API) for the
Matrix class, as well as the source and
test classes for this exercise.
To understand the use of the
Matrix API, look
at
MatrixTest, a class that uses
Matrix and is written
for you.
Observe:
- How the Matrix constructor is used
- How the accessor get and mutator set are used
- How bad row and column indexes are handled
using MatrixException
- How matrices are tested for equality using equals
- How matrices are added and multipled using add
and multiply
A matrix is a two-dimensional structure of numerical
values
aij, where
i is the (zero-origin) row
index and
j is the (zero-origin) column index.
If
r is the number of rows and
c is the number of columns
in a matrix
A, then it has the form:
To add two matrices, they must have the same dimensions, i.e. the same
number of rows and columns.
The result of adding matrices
A and
B is a
matrix
C of the same dimensions, where
cij =
aij + bij.
For example:
To multiply two matrices, the number of columns in the first matrix
must equal the number of rows in the second matrix. Matrix
multiplication is not commutative.
The result of multiplying an
m×n matrix
A and an
n×p
matrix
B is an
m×p matrix
C
where
cij is the dot product of
the
ith row of
A and
the
jth column of
B.
For example,
- The third row (index 2) of matrix A below contains
the values 6 5 1
- The first column (index 0) of matrix B contains
the values 2 4 3
- The dot product of
6 5 1
and
2 4 3
=
6×2 + 5×4 +1×3
=
12 + 20 + 3
=
35
- So the first column of the third row
of C (c20) = 35
Your tasks, in order:
- Create an internal Java representation of a matrix
- Implement the Matrix constructor
- Implement the set mutator and
the get, getRows, and getColumns
accessors
- Implement the equals method
- Implement the add and multiply methods
The
set,
get,
add, and
multiply methods
must also throw
MatrixException objects where appropriate.
First decide how to internally represent a matrix. Suggestion:
Test the constructor,
set, and
get
by running the
testSetAndGet1 and
testSetAndGet2 methods in
the
MatrixTest class. Note:
- You need not handle bad row and column indexes in these
tests.
- testSetAndGet2 uses the toMatrix
and toString convenience methods to populate and inspect a
matrix. These methods are fully implemented for you in
the Matrix class.
Now add index bounds testing to the
get and
set methods:
Test your index bounds testing by running the
testBounds test.
Two matrices are equal if they have the same dimensions and all
elements are equal by the
== test.
To check for matrix equality your
equals method must iterate
through the matrices' rows and columns.
Suggestion:
- Instead of directly accessing the 2-D elements array when
iterating through a matrix, use the API
methods getRows, getColumns, and get.
- Why? If the internal representation of a matrix is ever changed from
the 2-D array representation, the equals method will not have to
be rewritten.
- Use the toString method as a model for how to iterate through a
matrix's rows and columns using just the API
methods getRows, getColumns, and get.
Test your equality testing by running the
testEquals test.
The
add and
multiply methods both require iterating
through rows and columns, so they also benefit from abstraction by
using just the API methods
getRows,
getColumns,
and
get.
Note that both
testAdd and
testMultiply
catch
MatrixException objects and indicate the exact wording of
the relevant error messages when the number of rows and columns in the
operand matrices do not meet the preconditions of the
add
or
multiply methods.
Test your matrix addition and multiplication operations by running
the
testAdd and
testMultiply methods.
When all test methods execute successfully your
Test Results
window should include:
To verify that your
Matrix implementation
throws
MatrixException objects when appropriate,
the
catch clauses in the test methods print the error messages
to
System.out.
In addition to reporting that all 6 tests passed, your
Test Results
window should also include:
When finished, zip your project folder as
your-login-LEX4.zip.
Email the zip file to your TA.
- Successful running
of testSetAndGet1, testSetAndGet2,
and testBounds: 3 points
- Successful running of testEquals: 2 points
- Successful running of testAdd: 2 points
- Successful running of testMultiply: 3 points