I have a list ADT and I\'m supposed to write a matrix ADT and then use both ADTs
ID: 3630544 • Letter: I
Question
I have a list ADT and I'm supposed to write a matrix ADT and then use both ADTs to write a program.
Question: How do I write matrix ADT?
I'm fine with either actual java implementation (preferrably) or pseudocode. Thanks.
Matrix functions:
// Constructor ////////////////////////
Matrix(int n) // Makes a new n x n zero Matrix. pre: n>=1
// Access functions ///////////////////
int getSize() // Returns n, the number of rows and columns of this Matrix
int getNNZ() // Returns the number of non-zero entries in this Matrix
public boolean equals(Object x) // overrides Object's equals() method
// Manipulation procedures ////////////
void makeZero()
// sets this Matrix to the zero state
Matrix copy()
// returns a new Matrix having the same entries as this Matrix
void changeEntry(int i, int j, double x)
// changes ith row, jth column of this Matrix to x
// pre: 1<=i<=getSize(), 1<=j<=getSize()
Matrix scalarMult(double x)
// returns a new Matrix that is the scalar product of this Matrix with x
Matrix add(Matrix M)
// returns a new Matrix that is the sum of this Matrix with M
// pre: getSize()==M.getSize()
Matrix sub(Matrix M)
// returns a new Matrix that is the difference of this Matrix with M
// pre: getSize()==M.getSize()
Matrix transpose()
// returns a new Matrix that is the transpose of this Matrix
Matrix mult(Matrix M)
// returns a new Matrix that is the product of this Matrix with M
// pre: getSize()==M.getSize()
// Other functions ////////////////////
public String toString() // overrides Object's toString() method
Explanation / Answer
public class Matrix
{
// store the matrix
private double[][] mat;
// Constructor ////////////////////////
public Matrix(int n) // Makes a new n x n zero Matrix. pre: n>=1
{
mat = new double[n][n];
}
// Access functions ///////////////////
public int getSize() // Returns n, the number of rows and columns of this Matrix
{
return mat.length;
}
public int getNNZ() // Returns the number of non-zero entries in this Matrix
{
int count = 0;
for(int r = 0; r < mat.length; r++)
for(int c = 0; c < mat[r].length; c++)
if(mat[r][c] != 0)
count++;
return count;
}
public boolean equals(Object x) // overrides Object's equals() method
{
Matrix rhs = (Matrix)o;
// check size
if(rhs.getSize() != getSize())
return false;
// check each entry
for(int r = 0; r < mat.length; r++)
for(int c = 0; c < mat[r].length; c++)
if(mat[r][c] != rhs.mat[r][c])
return false;
return true;
}
// Manipulation procedures ////////////
public void makeZero() // sets this Matrix to the zero state
{
int size = mat.length;
mat = new double[size][size];
}
public Matrix copy() // returns a new Matrix having the same entries as this Matrix
{
Matrix copy = new Matrix(getSize());
// copy entry by entry
for(int r = 0; r < mat.length; r++)
for(int c = 0; c < mat[r].length; c++)
copy.mat[r][c] = mat[r][c];
return copy;
}
public void changeEntry(int i, int j, double x) // changes ith row, jth column of this Matrix to x
{
mat[i][j] = x
}
// pre: 1<=i<=getSize(), 1<=j<=getSize()
public Matrix scalarMult(double x) // returns a new Matrix that is the scalar product of this Matrix with x
{
Matrix output = new Matrix(getSize());
// entry by entry
for(int r = 0; r < mat.length; r++)
for(int c = 0; c < mat[r].length; c++)
output.mat[r][c] = mat[r][c]*x;
return output;
}
public Matrix add(Matrix M) // returns a new Matrix that is the sum of this Matrix with M
// pre: getSize()==M.getSize()
{
Matrix output = new Matrix(getSize());
// entry by entry
for(int r = 0; r < mat.length; r++)
for(int c = 0; c < mat[r].length; c++)
output.mat[r][c] = mat[r][c] + M.mat[r][c];
return output;
}
public Matrix sub(Matrix M) // returns a new Matrix that is the difference of this Matrix with M
// pre: getSize()==M.getSize()
{
return add(M.scalarMult(-1));
}
public Matrix transpose() // returns a new Matrix that is the transpose of this Matrix
{
Matrix output = new Matrix(getSize());
// entry by entry
for(int r = 0; r < mat.length; r++)
for(int c = 0; c < mat[r].length; c++)
output.mat[c][r] = mat[r][c];
return output;
}
public Matrix mult(Matrix M) // returns a new Matrix that is the product of this Matrix with M
// pre: getSize()==M.getSize()
{
Matrix output = new Matrix(getSize());
for(int r = 0; r < mat.length; r++)
for(int c = 0; c < mat[r].length; c++)
for(int k = 0; k < mat.length; k++)
output.mat[r][c] = mat[r][k]*M.mat[k][c];
return output;
}
// Other functions ////////////////////
public String toString() // overrides Object's toString() method
{
String output = "";
for(int r = 0; r < mat.size(); r++)
{
for(int c = 0; c < mat.size(); c++)
{
output += mat[r][c]+" ";
}
output += " ";
}
return output;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.