/*************************************************************************/ // This is a class that implements a two-dimensional matrix of characters. // Examples of uses include representing a maze or the state of a // game of Tic-Tac-Toe. /*************************************************************************/ import java.io.*; class CharMatrix implements CharMatrixInterface { // Holds the entries of the two-dimensional character matrix ... private char [][] M; // Number of rows, columns in character matrix private int numRows, numColumns; /********************************************************************/ // Default constructor ... /********************************************************************/ public CharMatrix() { M = null; numRows = numColumns = 0; } /********************************************************************/ // Constructor that reads a character matrix from a file. The file // is a text file, and must be formatted as follows: // // - The first line of the file is an integer n that indicates the number // of rows of the matrix; // - The second line of the file is an integer that indicates the number // m of columns of the matrix; // - For each i from 3 to n+2, line i contains the characters for row // i-3 of the matrix. (The rows are numbered 0 through n-1 and the // columns are numbered 0 through m-1.) The line must contain exactly // m characters, not counting the newline character. // // Example: // ------------ // 3 // 4 // abcd // acdb // baad // ------------ /********************************************************************/ public CharMatrix(String filename) { try { BufferedReader inputStream = new BufferedReader(new FileReader(filename)); String line = inputStream.readLine(); line = line.trim(); numRows = Integer.parseInt(line); line = inputStream.readLine(); numColumns = Integer.parseInt(line); M = new char [numRows][numColumns]; for (int i = 0; i < numRows; i++) { line = inputStream.readLine(); // line = line.trim(); if (line.length() != numColumns) { System.out.println("ERROR: Line " + i + " is not of the right length"); System.exit(1); } for (int j = 0; j < numColumns; j++) setEntry(i,j,line.charAt(j)); } } catch (FileNotFoundException e) { System.out.print("\n\n"); System.out.println(e.getMessage()); System.out.print("\n\n"); System.exit(0); } catch(IOException e) { System.out.print("\n\n"); System.out.println(e.getMessage()); System.out.print("\n\n"); System.exit(0); } } /**********************************************************************/ // Get the number of rows of the matrix ... /**********************************************************************/ public int getNumRows() { return numRows; } /**********************************************************************/ // Get the number of columns of the matrix ... /**********************************************************************/ public int getNumColumns() { return numColumns; } /**********************************************************************/ // Get the entry in row i and column j ... /**********************************************************************/ public char getEntry(int i, int j) { return M[i][j]; } /**********************************************************************/ // Change the value of the entry in row i and column j to newValue .. /**********************************************************************/ public void setEntry(int i, int j, char newValue) { M[i][j] = newValue; } public String toString() { // Label the column numbers ... String Result = " "; for (int i = 0; i < numColumns; i++) Result += (i % 10); Result += "\n"; /* for (int i = -1; i <= numColumns; i++) Result += "-"; Result += "\n"; */ for (int i = 0; i < numRows; i++) { Result += (i % 10 + "|"); // label with the row number for (int j = 0; j < numColumns; j++) Result += getEntry(i,j); Result += "\n"; } /*Result += " "; for (int i = -1; i <= numColumns; i++) Result += "-"; Result += "\n";*/ return Result; } }