/**
 * A class that uses recursion to print a number in any base/radix
 * (in the interval [2, 6])
 *
 * This version prints to System.out one character at a time
 *
 * @author  Prof. David Bernstein, James Madison Unversity
 * @verions 1.0
 */
 public class RadixPrinter
 {
    private final int    MAX_BASE    = 36;
    private final char[] DIGIT_TABLE = 
	 				{
					'0','1','2','3','4','5','6','7','8',
					'9','A','B','C','D','E','F','G','H',
					'I','J','K','L','M','N','O','P','Q',
					'R','S','T','U','V','W','X','Y','Z'
               };

    /**
     * The "exposed" interface.  This function does
     * initialization and error-checking.
     *
     * @param n    The number to print
     * @param base The base to use
     */
     public void printInBase(int n, int base)
     {
	
	     if ((base <= 1) || (base > MAX_BASE)) 
	     {
	        System.out.println("Invalid base: "+base);
	     } 
		  else 
	     {
	    		if (n < 0) 
	    		{
					System.out.print("-");
					n = -n;
	    		}
	    		printRecursively(n, base);
			} // end else
    } // end printInBase


    /**
     * The recursive function
     *
     * @param n    The number to print
     * @param base The base to use
     */
     private void printRecursively(int n, int base)
     {
			if (n >= base) 
         {
	    		printRecursively(n/base, base);
			}

			System.out.print(DIGIT_TABLE[n % base]);
     } // end printRecursively
    
} // end RadixPrinter
