Manipulation of 2 dimensional arrays is testable on the AP Computer Science A Exam. Multiple choice and free response questions are fair game. The MatrixManipulator practice problem is intended as practice with 2D arrays and with the nested loops commonly used to traverse them.

The MatrixManipulator practice problem requires extensive manipulation of 2D arrays of integers, including declaration, traversal and partial traversal. You must declare new 2D arrays with the correct dimensions and fill them with the correct values. MatrixManipulator is not intended to simulate an AP style free response question and should not be used as part of a practice test or for timing.

The AP CS Pictures Lab features 2D arrays.

## MatrixManipulator overview

Download the MatrixManipulator skeleton code. Follow the instructions below to complete the matrix manipulation methods and the test class methods.

The skeleton code includes method headers and documentation for the classes below. Sample output is provided in lieu of documentation for one of the classes.

Class MatrixManipulator is a collection of static methods that perform the following operations on integer matrices.

- Determine if two matrices can be added by matrix addition.
- Perform matrix addition on two addable matrices.
- Multiply a matrix by a scalar.
- Determine if two matrices can be multiplied by matrix multiplication.
- Perform matrix multiplication on two multipliable matrices.
- Multiply a single row of one matrix by a single column of another matrix. (This functionality is only exposed to allow it to be tested independently.)

Class MatrixManipulatorTester is a collection of static methods that tests the following operations of MatrixManipulator with valid input.

- Matrix addition
- Scalar multiplication
- Multiplication of a row by a column
- Matrix multiplication

Throughout the rest of the instructions, the classes are referred to as MM and MMT, respectively.

## Instructions for Matrix Addition

Order is important. Complete each step before moving on to the next. The sample output to which the instructions refer is on this page immediately below the instructions for all 3 parts.

- Review the sample output for MMT.testSum. Make sure you are familiar with both the result of matrix addition and the process. If you are unsure of the process, there are many excellent resources online. If you haven’t done it in a while, perform the addition manually on the operands and verify your result.
- Code the missing parts of MMT.testSum. The missing parts have been replaced with explicit comments. You will code the initialization of the two test matrices. Refer to the sample output for the size and values.
- Run MMT.testSum. Verify that it prints both matrices correctly. Fix any errors. (At this point, the sum should be null.)
- Review the code for the private method MM.atLeastOneByOne. The method name and code serve in lieu of documentation. Be aware of what the method checks. You’ll need to

call it from several other methods. - Review the documentation for MM.addable. Make sure you are clear on when matrix addition can be performed as well as exactly what the method checks. If you are unsure, look it up online. Take all possible inputs, including null, into account.

(Assume the array is not ragged. If you don’t know what that means, don’t

worry about it.) - Code MM.addable. Make sure your code makes sense and compiles. The method is used as a precondition later, so you’ll test it then.
- Review the documentation for MM.add. Make sure you are clear exactly how the method behaves before you start coding it. You are responsible for throwing an exception when appropriate.
- Code MM.add. Be careful to construct a sum array of the correct dimensions.
- Run MMT.testSum. Verify that MM.add and MMT.testSum work correctly. Fix any errors.

## Instructions for Scalar Multiplication

- Review sample output for MMT.testScalarMultiplication.
- Code the missing parts of MMT.testScalarMultiplication.
- Run testScalarMultiplication. Verify the output.
- Review the documentation for MM.multiplyByScalar. Make sure you’re clear.
- Code MM.multiplyByScalar.
- Verify that MM.multiplyByScalar and MMT.testScalarMultiplication work correctly. Fix any errors.

## Instructions for Matrix Multiplication

- Start with MMT.testRowColMultiplication. (MM.multiply returns 0 until coded.)
- Complete MM.multiply for a single row and column. Be careful not to try the method that multiplies two entire matrices yet.
- Test MMT.testRowColMultiplication and MM.multiply by running MMT.testRowColMultiplication and verifying the output.
- Complete MM.multipliable.
- Complete MMT.testMultiplication.
- Complete MM.multiply.
- Test MMT.testRowColMultiplication, MM.multipliable and MM.multiply by running MMT.testRowColMultiplication.

## Sample output for MMT.testSum

[1, 2, 3, 4, 5] [6, 7, 8, 9, 10] + [11, 12, 13, 14, 15] [16, 17, 18, 19, 20] = [12, 14, 16, 18, 20] [22, 24, 26, 28, 30]

## Sample output for MMT.testScalarMultiplication

[1, 2, 3, 4, 5] [6, 7, 8, 9, 10] * 2 = [2, 4, 6, 8, 10] [12, 14, 16, 18, 20]

## Sample output for MMT.testRowColMultiplication

first: [20, 21, 22, 23] [24, 25, 26, 27] [28, 29, 30, 31] second: [32, 33] [34, 35] [36, 37] [38, 39] MatrixManipulator.multiply(first, 0, second, 0): 3020 MatrixManipulator.multiply(first, 0, second, 1): 3106 MatrixManipulator.multiply(first, 1, second, 0): 3580 MatrixManipulator.multiply(first, 1, second, 1): 3682

## Sample output for MMT.testMultiplication

[20, 21, 22, 23] [24, 25, 26, 27] [28, 29, 30, 31] * [32, 33] [34, 35] [36, 37] [38, 39] = [3020, 3106] [3580, 3682] [4140, 4258]

See the MatrixManipulator solution or review it with AP CS Tutor Brandon Horn.