# Solution to MatrixManipulator practice problem for 2D arrays

Complete the MatrixManipulator practice problem before reviewing the solution.

Review the MatrixManipulator solution with AP CS Tutor Brandon Horn.

The method documentation from the original practice problem has been omitted. Throwing exceptions is not part of the AP Computer Science A subset and is not included on the exam.

## MatrixManipulator class

```public class MatrixManipulator
{
public static boolean addable(int[][] first, int[][] second)
{
if (!(atLeastOneByOne(first) && atLeastOneByOne(second)))
return false;

return first.length == second.length && first[0].length == second[0].length;
}

public static int[][] add(int[][] first, int[][] second)
{
throw new IllegalArgumentException();

int[][] sum = new int[first.length][first[0].length];

for (int row = 0; row < sum.length; row++)
for (int col = 0; col < sum[0].length; col++)
sum[row][col] = first[row][col] + second[row][col];

return sum;
}

public static int[][] multiplyByScalar(int scalar, int[][] matrix)
{
if (!atLeastOneByOne(matrix))
throw new IllegalArgumentException();

int[][] scalarProduct = new int[matrix.length][matrix[0].length];
for (int row = 0; row < scalarProduct.length; row++)
for (int col = 0; col < scalarProduct[0].length; col++)
scalarProduct[row][col] = matrix[row][col] * scalar;

return scalarProduct;
}

public static boolean multipliable(int[][] first, int[][] second)
{
if (!(atLeastOneByOne(first) && atLeastOneByOne(second)))
return false;

return first[0].length == second.length;
}

public static int[][] multiply(int[][] first, int[][] second)
{
if (!multipliable(first, second))
throw new IllegalArgumentException();

int[][] product = new int[first.length][second[0].length];
for (int row = 0; row < product.length; row++)
for (int col = 0; col < product[0].length; col++)
product[row][col] = multiply(first, row, second, col);

return product;
}

public static int multiply(int[][] first, int row, int[][] second, int col)
{
if (!(atLeastOneByOne(first) && atLeastOneByOne(second)))
throw new IllegalArgumentException();

if (first[row].length != second.length)
throw new IllegalArgumentException();

int product = 0;

for (int i = 0; i < first[row].length; i++)
product += first[row][i] * second[i][col];

return product;
}

private static boolean atLeastOneByOne(int[][] matrix)
{
return matrix != null && matrix.length > 0 && matrix[0].length > 0;
}
}
```

## MatrixManipulatorTester class

```public class MatrixManipulatorTester
{
public static void main(String[] args)
{
testSum();
testScalarMultiplication();
testRowColMultiplication();
testMultiplication();
}

public static void testSum()
{
int[][] first = new int[2][5];
int[][] second = new int[2][5];

int num = 0;

for (int row = 0; row < first.length; row++)
for (int col = 0; col < first[0].length; col++)
{
first[row][col] = ++num;
second[row][col] = num + 10;
}

printMatrix(first);
System.out.println("+");
printMatrix(second);
System.out.println("=");
printMatrix(sum);
}

public static void testScalarMultiplication()
{
int[][] matrix = new int[2][5];
int scalar = 2;

int num = 0;

for (int row = 0; row < matrix.length; row++)
for (int col = 0; col < matrix[0].length; col++)
matrix[row][col] = ++num;

int[][] scalarProduct = MatrixManipulator.multiplyByScalar(scalar, matrix);

printMatrix(matrix);
System.out.println("*");
System.out.println(scalar);
System.out.println("=");
printMatrix(scalarProduct);
}

public static void testRowColMultiplication()
{
int[][] first = new int[3][4];
int[][] second = new int[4][2];

int num = 20;

for (int row = 0; row < first.length; row++)
for (int col = 0; col < first[0].length; col++)
first[row][col] = num++;

for (int row = 0; row < second.length; row++)
for (int col = 0; col < second[0].length; col++)
second[row][col] = num++;

int product = MatrixManipulator.multiply(first, 0, second, 0);
System.out.println("first:");
printMatrix(first);
System.out.println("\nsecond:");
printMatrix(second);
System.out.println();
System.out.println("MatrixManipulator.multiply(first, 0, second, 0):");
System.out.println(product);

product = MatrixManipulator.multiply(first, 0, second, 1);
System.out.println("\nMatrixManipulator.multiply(first, 0, second, 1):");
System.out.println(product);

product = MatrixManipulator.multiply(first, 1, second, 0);
System.out.println("\nMatrixManipulator.multiply(first, 1, second, 0):");
System.out.println(product);

product = MatrixManipulator.multiply(first, 1, second, 1);
System.out.println("\nMatrixManipulator.multiply(first, 1, second, 1):");
System.out.println(product);
}

public static void testMultiplication()
{
int[][] first = new int[3][4];
int[][] second = new int[4][2];

int num = 20;

for (int row = 0; row < first.length; row++)
for (int col = 0; col < first[0].length; col++)
first[row][col] = num++;

for (int row = 0; row < second.length; row++)
for (int col = 0; col < second[0].length; col++)
second[row][col] = num++;

int[][] product = MatrixManipulator.multiply(first, second);

printMatrix(first);
System.out.println("*");
printMatrix(second);
System.out.println("=");
printMatrix(product);
}

public static void printMatrix(int[][] matrix)
{
for (int row = 0; row < matrix.length; row++)
System.out.println(java.util.Arrays.toString(matrix[row]));
}
}
```