// // COMP102 // Example 3: Constrained Multi-dimensional Array // Storing and Printing Giant Digits // // Paul E. Dunne 12/10/1999 // public class GIANT_TERNARY { // // Fields // protected char fg='*'; // Default Foreground Symbol protected char bg=' '; // Default Background Symbol protected char[][][] AllDigits= new char[3][9][9]; // Representation // // Single Constructor // public GIANT_TERNARY() { // // This probably looks a bit meaningless, but is easiest // mechanism for instantiating a default setting. // // Using fg='*' and bg=' ', the appearance encoded // is: // // *** *** ***** // ***** * ** ** ** // ** ** * ** ** ** // ** ** ** ** ** // ** ** ** ** // ** ** ** ** // ** ** ** ** // ***** ******* ****** // *** ******* ******* // // // // N.B. Layout convention which allows easy checking // that all elements of the 3-dimensional array have // been accounted for. // final char[][][] TempDigits = { { {bg,bg,bg,fg,fg,fg,bg,bg,bg}, {bg,bg,fg,fg,fg,fg,fg,bg,bg}, {bg,fg,fg,bg,bg,bg,fg,fg,bg}, {bg,fg,fg,bg,bg,bg,fg,fg,bg}, {bg,fg,fg,bg,bg,bg,fg,fg,bg}, {bg,fg,fg,bg,bg,bg,fg,fg,bg}, {bg,fg,fg,bg,bg,bg,fg,fg,bg}, {bg,bg,fg,fg,fg,fg,fg,bg,bg}, {bg,bg,bg,fg,fg,fg,bg,bg,bg} }, { {bg,bg,bg,fg,fg,fg,bg,bg,bg}, {bg,bg,fg,bg,fg,fg,bg,bg,bg}, {bg,fg,bg,bg,fg,fg,bg,bg,bg}, {bg,bg,bg,bg,fg,fg,bg,bg,bg}, {bg,bg,bg,bg,fg,fg,bg,bg,bg}, {bg,bg,bg,bg,fg,fg,bg,bg,bg}, {bg,bg,bg,bg,fg,fg,bg,bg,bg}, {bg,fg,fg,fg,fg,fg,fg,fg,bg}, {bg,fg,fg,fg,fg,fg,fg,fg,bg} }, { {bg,bg,fg,fg,fg,fg,fg,bg,bg}, {bg,fg,fg,bg,bg,bg,fg,fg,bg}, {bg,fg,fg,bg,bg,bg,fg,fg,bg}, {bg,fg,fg,bg,bg,bg,fg,fg,bg}, {bg,bg,bg,bg,bg,fg,fg,bg,bg}, {bg,bg,bg,bg,fg,fg,bg,bg,bg}, {bg,bg,bg,fg,fg,bg,bg,bg,bg}, {bg,bg,fg,fg,fg,fg,fg,fg,bg}, {bg,fg,fg,fg,fg,fg,fg,fg,bg} } }; // Now instantiate the appropriate field for the class; AllDigits = TempDigits; } //********************************************************** // Instance Methods * //********************************************************** // // Change the representation used by default, to employ // the specified foreground symbol (fore) and background // symbol (back). // public void ChangeOver (char fore, char back) { for (int i=0; i<3; i++) for (int j=0; j<9; j++) for (int k=0; k<9; k++) if (AllDigits[i][j][k]==fg) AllDigits[i][j][k]=fore; else AllDigits[i][j][k]=back; fg=fore; bg=back; } // ChangeOver // // Given a digit (0,1, or 2), output the // specified row (in row_value) of its // representation as a String, without a new line being thrown // after printing. // public void PrintRow( int row_value, int ternary_digit ) { System.out.print(String.valueOf(AllDigits[ternary_digit][row_value])); } // PrintRow // // Print out the representation of a ternary digit. // public void PrintAll (int ternary_digit ) { for (int i=0; i<9; i++) { PrintRow(i,ternary_digit); System.out.println(); }; } // PrintAll } // GIANT_TERNARY