All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class rebelsky.util.Matrix

java.lang.Object
   |
   +----rebelsky.util.Matrix

public class Matrix
extends Object
A simple implementation of variable-size matrices. These matrices are allowed to shrink and grow. While Java numbers most things starting at 0, the indices for rows and columns in these matrices begin at 1.

Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved.

Version:
1.0 of February 1998
Author:
Samuel A. Rebelsky

Constructor Index

o Matrix(int, int)
Build a matrix with n rows and m columns and empty (null) cells.
o Matrix(Object, int, int)
Build a matrix with n rows, m columns, and all cells set to some default object.

Method Index

o clear()
Clear the matrix.
o clone()
Make a copy of the current vector.
o columns()
Get the number of columns.
o deleteColumn(int)
Delete one column of the matrix.
o deleteRow(int)
Delete one row of the matrix.
o draw()
Convert the matrix to a two-dimensional string (including carriage returns).
o equals(Object)
Determine if another object is equal to this matrix.
o fillLine(Object, int, int, int, int, int, int)
Fill along a "line" in the matrix.
o get(int, int)
Get the value stored at (row,col).
o get(MatrixIndex)
Get the value stored at a particular point.
o hashCode()
Compute a hash value for this matrix.
o insertColumn(Vector, int)
Insert a new column into the matrix before a particular column.
o insertRow(Vector, int)
Insert a new row into the matrix before a particular row.
o rows()
Get the number of rows.
o set(Object, int, int)
Set the value at (row,col) to obj.
o set(Object, MatrixIndex)
Set the value at a point to obj.
o toString()
Convert this matrix to a string.
o verifyColumn(int)
Verify that the column is between 1 and cols.
o verifyRow(int)
Verify that the row is between 1 and rows.

Constructors

o Matrix
 public Matrix(int rows,
               int cols)
Build a matrix with n rows and m columns and empty (null) cells.

o Matrix
 public Matrix(Object obj,
               int rows,
               int cols)
Build a matrix with n rows, m columns, and all cells set to some default object. Does not currently make copies of the object but maydo so eventually.

Methods

o clear
 public void clear()
Clear the matrix.
pre: (none)
post: # of rows is 0.
post: # of columns is 0.

o columns
 public int columns()
Get the number of columns.
pre: (none)
post: the number of columns is returned

Returns:
the number of columns.
o deleteColumn
 public void deleteColumn(int col) throws BoundsException, Exception
Delete one column of the matrix.
pre: 1 <= col <= columns.
post: the matrix has one fewer column. In particular, the specified column is deleted.

Throws: BoundsException
when the column is invalid. In this case, the matrix is not affected.
Throws: Exception
when an unknown error occured. In this case, the matrix may be seriously damaged.
o deleteRow
 public void deleteRow(int row) throws BoundsException
Delete one row of the matrix.
pre: 1 <= row <= rows.
post: the matrix has one fewer row (in particular, the specified row is deleted).

Throws: BoundsException
when the row is invalid. In such cases, the matrix isn't affected.
o draw
 public String draw()
Convert the matrix to a two-dimensional string (including carriage returns).
pre: The matrix is nonempty.
post: A two-dimensional version is returned.

o fillLine
 public void fillLine(Object val,
                      int start_row,
                      int start_col,
                      int delta_row,
                      int delta_col,
                      int end_row,
                      int end_col) throws BoundsException
Fill along a "line" in the matrix.
pre: 1 <= start_row <= number of rows
pre: 1 <= end_row <= number of rows
pre: 1 <= start_col < number of columns
pre: 1 <= end_col <= number of columns
pre: delta_row >= 0
pre: delta_col >= 0
pre: at least one of delta_row and delta_col is positive
pre: (end_row,end_col) is a whole number of "steps" from (start_row,start_col), where a step is defined by (delta_row,delta_col).
post: every position between (start_row,start_col) and (end_row,end_col) using an offset of (delta_row,delta_col) is set to val.

o get
 public Object get(int row,
                   int col) throws BoundsException
Get the value stored at (row,col).
pre: 1 <= row <= rows.
pre: 1 <= col <= cols.
post: (none)

Returns:
the object stored at (row,col)
Throws: BoundsException
if the preconditions are not met
o get
 public Object get(MatrixIndex index) throws BoundsException
Get the value stored at a particular point.
pre: 1 <= row <= rows.
pre: 1 <= col <= cols.
post: (none)

Returns:
the object stored at (row,col)
o insertColumn
 public void insertColumn(Vector new_col,
                          int col) throws BoundsException, Exception
Insert a new column into the matrix before a particular column. If the specified column is immediately after the last column in the matrix, inserts the new column as the last column in the matrix. If the matrix is empty and the column is 1, makes a linear matrix corresponding to the column.
pre: 1 <= col <= columns+1.
pre: the length of the new column is the same as the number of rows in the matrix (or the matrix is empty).

Throws: BoundsException
when the preconditions are not met.
Throws: Exception
when something goes wrong and it's possible we now have a non-square matrix.
o insertRow
 public void insertRow(Vector new_row,
                       int row) throws BoundsException
Insert a new row into the matrix before a particular row. If that particular row is immediately after the last row in the matrix, inserts the new row as the last row in the matrix. If the matrix is empty and the row is 1, makes a linear matrix corresponding to the row.
pre: 1 <= row <= rows+1.
pre: the length of the new row is the same as the number of columns in the matrix (or the matrix is empty).

Throws: BoundsException
when the preconditions are not met.
o rows
 public int rows()
Get the number of rows.
pre: (none)
post: the number of rows is returned.

Returns:
the number of rows.
o set
 public Object set(Object obj,
                   int row,
                   int col) throws BoundsException
Set the value at (row,col) to obj.
pre: 1 <= row <= rows.
pre: 1 <= col <= cols.
post: this[row,col] is obj

Returns:
the object previously stored at (row,col)
Throws: BoundsException
if the preconditions are not met
o set
 public Object set(Object obj,
                   MatrixIndex index) throws BoundsException
Set the value at a point to obj.
pre: 1 <= row <= rows.
pre: 1 <= col <= cols.
post: this[row,col] is obj

Returns:
the object previously stored at (row,col)
Throws: BoundsException
if the preconditions are not met
o clone
 public Object clone()
Make a copy of the current vector.
pre: (none)
post: (none)

Returns:
a copy of the current vector.
Overrides:
clone in class Object
o equals
 public boolean equals(Object other)
Determine if another object is equal to this matrix.
pre: (none)
post: (none)

Returns:
true if the other object is a matrix and that matrix is equal to this one (according to the rules of vectors); false otherwise
Overrides:
equals in class Object
o hashCode
 public int hashCode()
Compute a hash value for this matrix.
pre: (none)
post: (none)

Returns:
an appropriate value
Overrides:
hashCode in class Object
o toString
 public String toString()
Convert this matrix to a string.
pre: (none)
post: (none)

Returns:
a string of the form [[row1], [row2], ..., [rown]]
Overrides:
toString in class Object
o verifyColumn
 public void verifyColumn(int col) throws BoundsException
Verify that the column is between 1 and cols.
pre: 1 <= col <= cols.
post: (none)

Throws: BoundsException
when the column is not between 1 and cols
o verifyRow
 public void verifyRow(int row) throws BoundsException
Verify that the row is between 1 and rows.
pre: 1 <= row <= rows.
post: (none)

Throws: BoundsException
when the row is not between 1 and rows

All Packages  Class Hierarchy  This Package  Previous  Next  Index