/**
 * A rectangular array of objects
 *
 */
public class RectangularArrayTS <T>
{
    private int       columns, rows;
    private T[]  data;  // matches whatever T is specified in the constructor
    

    /**
     * Explicit Value Constructor
     *
     * @param rows    The number of rows
     * @param columbs The number of columns
     */
    public RectangularArrayTS (int rows, int columns)
    {
      this.columns = columns;
		this.rows    = rows;
		data = (T[]) new Object [rows * columns];
		   // we still have an array of objects but we are 
			// casting it to an array of T
    }// end RectangularArray constructor


    /**
     * Returns the number of columns in this RectangularArray
     *
     * @return   The number of columns
     */
    public int columns()
    {
       return columns;  
    } // end columns
    
    /**
     * Get the element at a particular row and column
     *
     * @param row     The row index
     * @param column  The column index
     * @return        The Object at the given row and column
     */
    public T getElementAt(int row, int column)
	 								throws IndexOutOfBoundsException
    {
        T myObject;
		  int i;
		  i = index(row, column);
  	     myObject = data[i];
		  return myObject;
    }// end getElementAt
    
    /**
     * This implementation uses an ordinary array as the underlying
     * data structure.  This converts from VALID row and column indexes
     * to an index into the underlying data structure.
     *
     * @param row     The row index
     * @param column  The column index
     * @return        The corresponding index in the data array
     */
    private int index(int row, int column) 
    {                  
	     int i;
		  i = row*columns + column;         
        if (row > this.rows || column > this.columns || i > data.length)
		     throw new IndexOutOfBoundsException 
			  (" row: " + row + " column: " + column); // parameter is message
		  else
		     return i;
    }// end index
    

    /**
     * Returns the number of rows in this RectangularArray
     *
     * @return   The number of rows
     */
    public int rows()
    {
       return rows;
    } // end rows
    
  
    /**
     * Set the element at a particular row and column
     *
     * @param row     The row index
     * @param column  The column index
     * @param o       The Object to put in the array
     */
    public void setElementAt(int row, int column, T anObject)
									 throws IndexOutOfBoundsException
    {
          int location;
	       location = index(row,column);
		    data[location] = anObject;		    
    } // end setElementAt
}// end class
