# Two-Dimensional Arrays...

• Goal of this Homework

Reinforce your knowledge of two-dimensional arrays and static (class) variables.

• Relevant Material: Chapter 8 and 9 in book

• Preparation

Save the following files in your project directory (preferably, make a directory called "hw7" in your "cs170" directory, and save these files there (right click and select save target as)

You can also get the files using the command:

```    cp  /home/cheung/teaching/web/170/Projects/hw7/Handouts/*   ~/cs170/hw7
```

In the remainder of the homework description, I will assume that you have saved these file in your ~/cs170/hw7 directory.

• If you use BlueJ

• If you are using BlueJ for the lab, you must open hw7 as "Non BlueJ"

• Homework Assignment

• A matrix in Mathematics is a m x n grid of numbers

In this assignment, we will write a Matrix class that you can use to represent 3 x 3 matrices.

The following is how each element is number in a 3x3 matrix:

```	A11    A12    A13
A21    A22    A23
A31    A32    A33
```

• Write in the program file "Matrix.java", the following methods:

1. A constructor Matrix() that takes 9 double values and constructs a 3 x 3 matrix.

The first 3 values is the values in the first row of the matrix.

The second set of 3 values is the values in the second row of the matrix.

And the third set of 3 values is the values in the third row of the matrix.

The representation must use a two-dimensional array of doubles - and the size of the array is 3x3. (Look inside the file Matrix.java - I have already defined the array variable; you need to initialize it !).

2. A method determinant() that returns the determinant of a matrix.

The determinant of a 3x3 matrix can be computed as follows:

```	A11*(A22*A33 - A23*A32)
- A21*(A12*A33 - A13*A32)
+ A31*(A12*A23 - A13*A22)
```

The method determinant() must count how many times it has been invoked.

3. A method getCountDet() that returns the number of times that the method determinant() has been invoked.

4. A method multiply(Matrix m) that returns a new Matrix which is the multiplication of the given matrix and the matrix m.

This is how you multiply two 3x3 matrices A and B:

```       +-              -+
|A11    A12    A13|
A = |A21    A22    A23|
|A31    A32    A33|
+-              -+
```

```       +-              -+
|B11    B12    B13|
B = |B21    B22    B23|
|B31    B32    B33|
+-              -+
```

Then:

```              +-              -+
|C11    C12    C13|
C =  A*B = |C21    C22    C23|
|C31    C32    C33|
+-              -+

where:

Cij = Ai1*B1j + Ai2*B2j + Ai3*B3j    (for i = 1, 2, 3 and j = 1, 2, 3)
```

Some programming help in writing multiply()

Inside the multiply() method, you must create a Matrix object !

Here's how the multiply() method will look like:

```    public Matrix multiply(Matrix m)
{
Matrix Result = Matrix(0, 0, 0, 0, 0, 0, 0, 0, 0);

... compute Result = this matrix multiple with Matrix m...

return(Result);
}
```

5. A method getCountMult() that returns the number of times that the method multiply() has been invoked.

• Correct output

```	Determinant of A = 1.0
getCountDet() = 1.0

Determinant of B = -3.0
getCountDet() = 2.0

Determinant of C = 1.0
getCountDet() = 3.0

++ getCountMult() = 1.0

Determinant of R = -3.0
getCountDet() = 4.0

++ getCountMult() = 2.0

Determinant of R = 1.0
getCountDet() = 5.0

++ getCountMult() = 3.0

Determinant of R = -3.0
getCountDet() = 6.0

```

• Turnin

• Turn in the homework with the following command:
```   /home/cs170001/turnin   ~/cs170/hw7/Matrix.java  hw7

```