Part 1: Matrix.java (in package matrix) For this part of the programming assignm
ID: 3679483 • Letter: P
Question
Part 1: Matrix.java (in package matrix)
For this part of the programming assignment, you will implement a Matrix class. Your Matrix class will provide methods to initialize a new matrix, add and subtract two matrices, produce the transpose of the matrix, and convert the matrix to a string representation.
The Matrix class should provide the following methods (use these names and parameter definitions please):
public Matrix(int numRows, int numCols, double [][] array2D) throws
IllegalArgumentException
This constructor intializes a new matrix as a numRows by numCols matrix with values
taken from the two dimensional array, array2D. The constructor should throw the IllegalArgumentException if the array in array2D does not have numRows elements in the first dimension or it does not have numCols elements in the second dimension. The Matrix object must have its own internal storage. That is, the Matrix object should not simply assign the pointer to array2D to an internal variable; it should copy the values into new storage
public Matrix(int numRows, int numCols)
This constructor initializes a new matrix as a numRows by numCols matris with all zero
values. This constructor does not throw an exception. public double [][] getArray()
This getter method returns a two dimensional array containing the values of the Matrix. The array returned must use different storage than the Matrix object.
public double getElement(int row, int col) throws IllegalArgumentException
This getter method returns the value at row, col of the Matrix. The row and column
parameters should be one-based. That is, row may range from 1 to numRows and col may range from 1 to numCols. If row or col are not valid, the method throws an IllegalArgumentException.
public void setElement(int row, int col, double newV) throws IllegalArgumentException
This setter method puts newV at location row, col of the matrix. The row and column parameters should be one-based. That is, row may range from 1 to numRows and col
may range from 1 to numCols. If row or col are not valid, the method throws an
IllegalArgumentException.
public Matrix add(Matrix m) throws IllegalArgumentException
This method creates a new matrix, places the sum of this matrix and m in it, and returns the new matrix. Neither the parameter matrix m nor the calling matrix this are changed. If the dimensions of this and m do not match, the method throws an IllegalArgumentException.
public Matrix subtract(Matrix) throws IllegalArgumentException Page 1 of 3
This method creates a new matrix, places the difference of this matrix and m in it (this - m), and returns the new matrix. Neither the parameter matrix m nor the calling matrix this are changed. If the dimensions of this and m do not match, the method throws an IllegalArgumentException.
public Matrix transpose()
This method returns a new matrix which contains the transpose of the calling matrix.
The transpose of a matrix simply does a swap of the following pairs of element mij is
swapped with mji. This method does not through any exceptions.
public String toString()
This method returns a string representation of the Matrix. Each row of the matrix must
be on a separate line. You can do this by using the new line character ' ' when you
concatenate the elements of the matrix.
To throw an IllegalArgumentException, you can use the following syntax:
throw new IllegalArgumentException(message);
where message is a String containing information about what caused the exception and where it was thrown. Provide as much information about the condition causing the exception as you can.
Part 2: SquareMatrix.java
In part 2, you will implement a class that represents a square matrix. That is, the number of
rows in the matrix must be the same as the number of columns. You must extend the Matrix
class to implement your SquareMatrix class. You do not need to override any of the methods of
the Matrix class, but you do need to create new constructors:
public SquareMatrix(int rc, double [][] array2D) throws IllegalArgumentException
This constructor initializes a new square matrix as an rc by rc matrix with values copies
from array2D. Your constructor should use the super class constructor to do all of the
work. If the super class constructor throws an exception, the SquareMatrix constructor
need do nothing; the exception will simply propagate to the caller.
public SquareMatrix(int rc)
This constructor initilizes a new square matrix as an rc by rc matrix with all zero values.
Again, your constructor should let the super class constructor do all the work. This
constructor does not throw an exception.
Explanation / Answer
import matrix;
class Matrix
{
public static void main(String args[])
{
int i,j,k;
int mat1 [][]={ {1,2,3}, {4,5,6}, {7,8,9} };
int mat2 [][]={ {10,11,12}, {13,14,15}, {16,17,18} };
//Matrix A
System.out.println(" Matrix A:");
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
System.out.print(" "+mat1[i][j]);
System.out.println("");
}
//Matrix B
System.out.println(" Matrix B:");
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
System.out.print(" "+mat2[i][j]);
System.out.println("");
}
System.out.println(" Operation ON Matrices 1.Addition ");
int sum [][] = new int [3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
sum[i][j] = mat1[i][j] + mat2[i][j];
System.out.print(" " + sum[i][j]);
}
System.out.println("");
}
System.out.println("2.Subtraction ");
int diff[][] = new int[3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
diff [i][j] = mat1[i][j] - mat2[i][j];
System.out.print(" "+ diff[i][j]);
}
System.out.println("");
}
System.out.println("3. Transpose Of A ");
int trans[][] = new int[3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
trans [i][j] = mat1[j][i];
System.out.print(" "+ trans[i][j]);
}
System.out.println("");
}
System.out.println("4.Multiplication ");
int prod[][] = new int[3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
prod[i][j] = 0;
{
for(k=0;k< 3;k++)
prod[i][j] = prod[i][j]+mat1[i][k]*mat2[k][j];
}
System.out.print(" "+ prod[i][j]);
}
System.out.println("");
}
}
}
/************* OUTPUT ***************
Matrix A:
1 2 3
4 5 6
7 8 9
Matrix B:
10 11 12
13 14 15
16 17 18
Operation ON Matrices
1.Addition
11 13 15
17 19 21
23 25 27
2.Subtraction
-9 -9 -9
-9 -9 -9
-9 -9 -9
3. Transpose Of A
1 4 7
2 5 8
3 6 9
4.Multiplication
84 90 96
201 216 231
318 342 366
*/