// // COMP102 // Example 4: Printing GIANT_TERNARY representation // // Paul E. Dunne 13/10/99 // import java.io.*; import GIANT_TERNARY; // Ternary Digit Class // public class GiantTernaryOut { // Instnatiate Input streams public static InputStreamReader input = new InputStreamReader(System.in); public static BufferedReader keyboardInput = new BufferedReader(input); // static GIANT_TERNARY NumberForm=new GIANT_TERNARY(); static char fore; // For user-defined foreground symbol static char back; // For user-defined background symbol static int InputValue; // User supplied integer to be printed in ternary // using GIANT_TERNARY representation. static String TernaryValue; // This will contain the ternary value of InputValue // as a String of {0,1,2} symbols. //*************************************************************** // Read user-defined foreground and background * // symbols. * //*************************************************************** public static void Get_fore_and_back() throws IOException { String input_line; System.out.print("Foreground Character (hit return for default value):"); input_line = keyboardInput.readLine(); if (input_line.length()==0) // Empty String read: use default. { fore='*'; } else { fore = input_line.charAt(0); // otherwise take first character. }; System.out.print("Background Character (hit return for default value):"); input_line = keyboardInput.readLine(); if (input_line.length()==0) // Empty String read: use default. { back=' '; } else { back = input_line.charAt(0); // otherwise take first character. }; } // Get_fore_and_back //************************************************************* // Convert integer to ternary representation returning String * // of the encoding digits as the result. * //************************************************************* public static String IntegerToTernary( final int n ) { String res = new String(); int tn = n; // Save value of input parameter. int next_digit; // The next ternary digit to be stored in res. if (tn==0) // Can convert 0 immediately. { res= "0"+res; } else { while (tn>0) { next_digit = tn%3; // Find the least significant ternary digit left; tn=tn/3; // and `shift' this off the current value of tn. // // Append the correct symbol to the result String. // We could do this directly (i.e. avoiding case statetement) // by using an integer-character encoding, however, this // assumes a specific encoding scheme (ASCII say) and //is a bit opaque. // switch(next_digit) { case 0: res = "0"+res; break; case 1: res = "1"+res; break; case 2: res = "2"+res; }; }; }; return res; } // IntegerToTernary // // Main Method // public static void main( String[] args ) throws IOException { Get_fore_and_back(); NumberForm.ChangeOver(fore,back); // Modify the instantiation to // use the new foreground/background // symbols. System.out.print("Enter non-negative integer value:"); InputValue = new Integer(keyboardInput.readLine()).intValue(); TernaryValue = IntegerToTernary(InputValue); // // Output Stage // // Proceeding from left-to-right output // the current (i'th) row of the GIANT_TERNARY digit // corresponding to each digit in the ternary representation // of the integer InputValue // System.out.println(); // Start printing on a new line for (int i=0; i<9; i++) { for (int j=0; j