// An implementation of a rectangular vector.
// (c) 1998, 2001 duane a. bailey
package structure;
/**
* An implementation of rectangular vectors.
* This implementation of a Matrix is minimal. Few operations
* are provided, and no support for mathematical operations
* is considered.
*
* Example Usage:
*
*
* public static void main(String[] argv){
* //create an array of arrays to bet copied into the matrix
* int[][] test = new int[][]{new int[]{1,2,3},
* new int[]{4,5,6},
* new int[]{7,8,9}};
*
* //create the matrix
* Matrix testMatrix = new {@link #Matrix(int, int) Matrix(test.length, test[0].length)};
*
* //copy the 2-d array into the matrix
* for(int i = 0; i < test.length; i++){
* for(int j = 0; j < test[0].length; j++){
* testMatrix.{@link #set(int, int, Object) set(i,j,new Integer(test[i][j]))};
* }
* }
*
* //print out the matrix
* System.out.println(testMatrix);
* }
*
* @version $Id: Matrix.java,v 1.2 2005/10/18 19:47:21 terescoj Exp $
* @author, 2001 duane a. bailey
*/
//+interface
public class Matrix
{
//-interface
//+constructor
protected int height, width; // size of matrix
protected Vector> rows; // vector of row vectors
//-constructor
/**
* Construct an empty matrix.
*
* @post constructs empty matrix
*
*/
public Matrix()
{
this(0,0);
}
//+constructor
//+interface
/**
* Constructs a matrix such that all values are null.
*
* @pre h >= 0, w >= 0;
* @post constructs an h row by w column matrix
*
* @param h Height of the matrix.
* @param w Width of the matrix.
*/
public Matrix(int h, int w)
//-interface
{
height = h; // initialize height and width
width = w;
// allocate a vector of rows
rows = new Vector>(height);
for (int r = 0; r < height; r++)
{ // each row is allocated and filled with nulls
Vector theRow = new Vector(width);
rows.add(theRow);
for (int c = 0; c < width; c++)
{
theRow.add(null);
}
}
}
//-constructor
//+interface
//+elementMethods
/**
* Fetch an element from the matrix.
* @pre 0 <= row < height(), 0 <= col < width()
* @post returns object at (row, col)
*
* @param row The row of the element
* @param col The column of the element
* @return Object located at matrix position (row, col)
*/
public ELTTYPE get(int row, int col)
//-interface
{
Assert.pre(0 <= row && row < height, "Row in bounds.");
Assert.pre(0 <= col && col < width, "Col in bounds.");
return rows.get(row).get(col);
}
//+interface
/**
* Change the value at location (row, col)
* @pre 0 <= row < height(), 0 <= col < width()
* @post changes location (row, col) to value
*
* @param value The new Object reference (possibly null).
* @param row The row of the value to be changed.
* @param col The column of the value to be changed.
*/
public void set(int row, int col, ELTTYPE value)
//-interface
{
Assert.pre(0 <= row && row < height, "Row in bounds.");
Assert.pre(0 <= col && col < width, "Col in bounds.");
rows.get(row).set(col,value);
}
//-elementMethods
//+interface
/**
* Add a new row, whose index will be r.
*
* @pre 0 <= r < height()
* @post inserts row of null values to be row r
*
* @param r The index of the newly inserted row.
*/
//+insertRowAt
public void addRow(int r)
//-interface
{
Assert.pre(0 <= r && r < width, "Row in bounds.");
height++;
Vector theRow = new Vector(width);
for (int c = 0; c < width; c++)
{
theRow.add(null);
}
rows.add(r,theRow);
}
//-insertRowAt
//+interface
/**
* Add a new column, whose index will be c.
*
* @pre 0 <= c < width()
* @post inserts column of null values to be column c
*
* @param c The index of the newly inserted column.
*/
public void addCol(int c)
//-interface
{
Assert.pre(0 <= c && c < width, "Col in bounds.");
width++;
for (int r = 0; r < height; r++)
{
rows.get(r).add(c,null);
}
}
//+interface
/**
* Remove the row whose index is r.
* The row is returned as a vector.
* @pre 0 <= r < height()
* @post removes row r and returns it as a Vector
*
* @param r The index of the to-be-removed row.
* @return A vector of values once in the row.
*/
public Vector removeRow(int r)
//-interface
{
Assert.pre(0 <= r && r < height,"There is a row to be removed.");
Vector result = rows.get(r);
height--;
rows.remove(r);
return result;
}
//+interface
/**
* Remove a column, whose index is c.
* @pre 0 <= c < width
* @post removes column c and returns it as a vector
*
* @param c The index of the column to be removed.
* @return A vector of the values removed.
*/
public Vector removeCol(int c)
//-interface
{
Assert.pre(0 <= c && c < width,"There is a column to be removed.");
Vector result = new Vector(height);
width--;
for (int r = 0; r < height; r++)
{
Vector theRow = rows.get(r);
result.add(theRow.get(c));
theRow.remove(c);
}
return result;
}
//+interface
/**
* Return the width of the matrix.
* @post returns number of columns in matrix
*
* @return The number of columns in the matrix.
*/
public int width()
//-interface
{
return width;
}
//+interface
/**
* Return the height of the matrix.
*
* @post returns number of rows in matrix
*
* @return The number of rows in the matrix.
*/
public int height()
//-interface
{
return height;
}
/**
* Construct a string representation of the matrix.
* @post returns string description of matrix.
*
* @return A string, representing the matrix.
*/
public String toString()
{
StringBuffer s = new StringBuffer();
s.append("");
}
s.append("\n");
}
s.append(">");
return s.toString();
}
//+interface
}
//-interface