CPSC-115 Fall 2008
Lab 11
November 11-12, 2008
Professor Heidi Ellis

Be sure that you hand in the printouts of your work before leaving!!!

Objectives:

In this lab, you will create a class to represent a matrix of arbitrary height and width. You will also write methods to manipulate the matrix such as matrix addition, scalar multiplication and more.

There is no pre-lab for this lab.

Pair Programming:

  1. Locate your partner and introduce yourself:
  2. Tuesday Lab   Wednesday Lab
    Kristen AndersonJeff Young   Chelsea Bainbridge-DonnerCorazon Irizarry
    Nick DraguJake Elder   Greg VaughanJohn Wilsterman
        Jesse VazquezCatherine Doyle
        Jin Feng LiuRyan Ersland
  3. Select one person to start as "driver". This person will type at the keyboard for the first 20 mintues.
  4. Proceed through the process of completing the lab as described below. Be very careful to ensure that every item for both programs is completed.
  5. Swap pairs every 20 minutes.
  6. When you are done, be sure to email a copy of the code to the person whose account you are not working in. In other words, make sure that both partners have a copy of the code.

Matrices

A matrix is a rectangular array of elements divided into rows and columns which typically contain numbers. A Sudoku puzzle is an example of a matrix. Matrices are frequently used in the discipline of linear algebra. Matrices have certain operations such as adding a value to each of the elements in the matrix. Or transposing the matrix which involves inverting the rows and columns.

Identity Matrix

One special kind of matrix is a square matrix that has ones across the diagonal from upper left to lower right and zeros on all of the other locations:
 1  0  0 
 0  1  0 
 0  0  1 

Symmetric Matrix

Another special kind of matrix is a square matrix where the matrix is symmetric around the diagonal. In this matrix, the lower left half of the matrix is a mirror image of the upper right half. For example:
  5   4   8   2 
  4   3  10  12 
  8  10   5   1 
  2  12   1   8 

Scalar Multiplication and Division

One common operation to perform on a matrix is to multiply or divide all of the elements in the matrix by a scalar (single value). The matrix below:
 1  2  3 
 4  5  6 
 7  8  9 
Multiplied by 2 results in the following matrix:
  2   4   6 
  8  10  12 
 14  16  18 
Division works analogously.

Matrix Transpose

Another common operation that is performed on matrices is to transpose a matrix. Transposing involves "swapping" the rows and colunms such that the first row becomes the first column in the matrix and the second row becomes the second column in the matrix. For instance, given the matrix:
  1   2   3   4 
  5   6   7   8 
  9  10  11  12 
The transpose of this matrix is:
  1   5   9 
  2   6  10 
  3   7  11 
  4   8  12 

Matrix Addition

Two matricess that are of the same size may be added together. In matrix addition, the value in a particular location in the first matrix is added with the value in the same location in the second matrix. For instance, given the two matrices below:
 1  2  3 
 4  5  6 
 7  8  9 

 5  3  3 
 4  2  1 
 3  6  0 
Addition of these two matrices would result in the following matrix:
  6   5   6 
  8   7   7 
 10   14   9 

Part 1: Create a Matrix Class

In this lab, you must create a class called Matrix. The Matrix class contains a two-dimensional array that holds integers. There are also two constants used to create the height and width of the default array. You must also define methods to manipulate the matrix including determining if the matrix is an identity matrix, determining if the matrix is a symmetric matrix, matrix addition and more. You must create a separate MatrixTest class that contains your main method. The UML class diagram for the Matrix class is shown below:

Matrix
+ theMatrix: int[][]
+ MAX_HEIGHT static final int
+ MAX_WIDTH static final int
+ Matrix()
+ Matrix(h: int, w: int)
+ getHeight(): int
+ getWidth(): int
+ enterData(): void
+ isIdentityMatrix(): boolean
+ isSymmetric(): boolean
+ multiplyScalar(scalar: int): void
+ divideScalar(scalar: int): void
+ transpose(): void
+ addMatrix(matrx: int[][]): void
+ toString(): String


The descriptions of each method are found below: Code incrementally! One suggested approach is:
  1. Create your class skeleton (class header and attributes) for the Matrix class. Compile and run.
  2. Create the no-value constructor. Compile and run.
  3. Create the toString method. Compile and run.
  4. Create your MatrixTest class. In the main method, create an instance of the Matrix class and call the toString method on it to make sure that your matrix holds what you think it does.
  5. Now incrementally add methods (likely starting with the constructor that takes parameters and enterData) and test after adding each method.
When you have successfully completed your application, the main method should do the following:
  1. Create a new Matrix instance.
  2. Call the enterData method to prompt the user to enter data.
  3. Print the original matrix.
  4. Determine if the matrix is the identity matrix and print out the results.
  5. Determine if the matrix is symmetric and print out the results.
  6. Call the multiplyScalar method to multiply the matrix by some integer value.
  7. Print out the matrix.
  8. Call the divideScalar method to divide the matrix by some integer value.
  9. Print out the matrix.
  10. Call the transpose method to transpose the matrix.
  11. Create another 2D array of the same size as the matrix. Pass this array to the addMatrix method.
  12. Print out the matrix.
A sample execution of the main method might appear as below. I have used a 3 X 3 array to simplify the output.
Enter data for a row (one value at a time):
Enter an integer: 1
Enter an integer: 2
Enter an integer: 3
Enter data for a row (one value at a time):
Enter an integer: 4
Enter an integer: 5
Enter an integer: 6 
Enter data for a row (one value at a time):
Enter an integer: 7
Enter an integer: 8
Enter an integer: 9

The Original Matrix:
1  2  3  
4  5  6  
7  8  9  

Is the matrix the identity matrix? false
Is the matrix symmetric?false
The matrix multiplied by 10: 
10  20  30  
40  50  60  
70  80  90  

The matrix above divided by 2: 
5  10  15  
20  25  30  
35  40  45  

The transposed matrix:
5  20  35  
10  25  40  
15  30  45  

Adding the matrix below:
1   2   3 
10  20  30 
5   4   3
The matrix after adding the above matrix: 
6  22  38  
20  45  70  
20  34  48  

If You Have Time:

If you have completed all of the above, add a multiplyMatrix method to your Matrix class. The multiplyMatrix method takes a two-dimensional array as a parameter and performs matrix multiplication on the two matrices, returning the resulting array. What does the method header look like?

Matrix multiplication involves creating an entirely new array. The element in location [0,0] in the new matrix, is the result of multiplying each value in the first row of the first array by each value in the first column in the second array and adding the results together. Similarly, the element in loation [1,0] is found by multiplying eacy value in the second row of the first array by each value in the first row in the second array. A more complete description of matrix multiplication may be found at MathWorld.

Test your code carefully to make sure that it operates correctly under a variety of test cases. Your code should have good programming style and follow all Java conventions. Don't forget to use constants where appropriate.

You're done!! Don't forget to log out and hand in your printouts to the professor before you leave!!