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:
- To understand two-dimensional arrays.
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:
- Locate your partner and introduce yourself:
| Tuesday Lab |
|
Wednesday Lab |
| Kristen Anderson | Jeff Young |
|
Chelsea Bainbridge-Donner | Corazon Irizarry |
| Nick Dragu | Jake Elder |
|
Greg Vaughan | John Wilsterman |
| | |   |
Jesse Vazquez | Catherine Doyle |
| | |   |
Jin Feng Liu | Ryan Ersland |
- Select one person to start as "driver". This person will type at the keyboard for the first 20 mintues.
- Proceed through the process of completing the lab as described below. Be very careful to ensure that every item for both programs is completed.
- Swap pairs every 20 minutes.
- 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:
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:
Multiplied by 2 results in the following matrix:
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:
Addition of these two matrices would result in the following matrix:
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:
- Matrix() : This default constructor creates a MAX_HEIGHT by MAX_WIDTH matrix. The values within the matrix are set to be the result of multiplying both indices together and then by two. (Or you may come up with your own formula for entering data into the array.)
- Matrix(h: int, w: int) : This constructor takes the height and width of the matrix to be constructed and creates a matrix with those dimensions. The matrix is not filled with any values.
- getHeight : Returns the height of the matrix. Remember that the height of the
matrix is the length of the outer array.
- getWidth : Returns the width of the matrix. The width of the array is the length of the first nested subarray.
- enterData : Uses a Scanner object to allow the user to enter data into an array from the keyboard. To make data entry more clear, this method is constructed such that the user is notified when a new row in the matrix is reached.
- isIdentityMatrix : This method determines whether the matrix is the identity matrix or not. What are the value of the width and height coordinates for the values of one? What are the values of the width and height coordinates for values that are zero?
- isSymmetric : This method determines whether the matrix is symmetric or not. What are the coordinates for the two mirror image values? How are they related?
- multiplyScalar : This method multiplies the matrix by the single scalar value that is passed into the method. The method multiplies each entry in the array by the provided value and stores the result back into the same location in the array.
- divideScalar : This method divides the matrix by the single scalar value that is passed into the method. The method divides each entry in the array by the provided value and stores the result back into the same location in the array.
- transpose : This method transposes the array by swapping rows and columns. This is done by creating a new array that is the height of the original array's width and the width of the original array's height. The transposed values are inserted into this new array and then the reference to the original array is set to this new array.
- addMatrix : This method adds a matrix to this matrix. The method first checks to make sure that the matrices are the same size. If the matrices are the same size, then the addition is carried out. If not, an error message is displayed.
- toString : This method is inherited from the Object class. You must override the method to construct a string that displays the matrix using proper rows and columns (i.e., not one long string).
Code incrementally! One suggested approach is:
- Create your class skeleton (class header and attributes) for the Matrix class. Compile and run.
- Create the no-value constructor. Compile and run.
- Create the toString method. Compile and run.
- 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.
- 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:
- Create a new Matrix instance.
- Call the enterData method to prompt the user to enter data.
- Print the original matrix.
- Determine if the matrix is the identity matrix and print out the results.
- Determine if the matrix is symmetric and print out the results.
- Call the multiplyScalar method to multiply the matrix by some integer value.
- Print out the matrix.
- Call the divideScalar method to divide the matrix by some integer value.
- Print out the matrix.
- Call the transpose method to transpose the matrix.
- Create another 2D array of the same size as the matrix. Pass this array to the addMatrix method.
- 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!!