// Implements a 2-D array of characters
public class CharMatrix
{
// Fields:
private int x;
private int y;
// Constructor: creates a grid with dimensions rows, cols,
// and fills it with spaces
public CharMatrix(int rows, int cols)
{
x = rows;
y = cols;
char[] array = new char[x][y];
}
// Constructor: creates a grid with dimensions rows , cols ,
// and fills it with the fill character
public CharMatrix(int rows, int cols, char fill)
{
x = rows;
y = cols;
char[] array = new char[x][y];
for(int i = 0; i < x; i++)
for(int j = 0; j < y; j++)
array[ i ][ j ] = fill;
}
// Returns the number of rows in grid
public int numRows()
{
return x;
}
// Returns the number of columns in grid
public int numCols()
{
return y;
}
// Returns the character at row, col location
public char charAt(int row, int col)
{
return array[x][y];
}
// Sets the character at row, col location to ch
public void setCharAt(int row, int col, char ch)
{
array[x][y] = ch;
}
// Returns true if the character at row, col is a space,
// false otherwise
public boolean isEmpty(int row, int col)
{
int counter = 0;
for(int i = 0; i < x; i++)
for(int j = 0; j < y; j++)
if (array[i][j].equals('0'))
counter ++;
if(counter==x*y)
return true;
else return false;
}
// Fills the given rectangle with fill characters.
// row0, col0 is the upper left corner and row1, col1 is the
// lower right corner of the rectangle.
public void fillRect(int row0, int col0, int row1, int col1, char fill)
{
...
}
// Fills the given rectangle with SPACE characters.
// row0, col0 is the upper left corner and row1, col1 is the
// lower right corner of the rectangle.
public void clearRect(int row0, int col0, int row1, int col1)
{
...
}
// Returns the count of all non-space characters in row
public int countInRow(int row)
{
...
}
// Returns the count of all non-space characters in col
public int countInCol(int col)
{
...
}
}