/**
 * TestRecursion.java -- this is an application/main class that show cases 
 * two recursive functions.
 * One is to print out a mutated string.  One is to convert an integer 
 * into a binary number.
 *
 * CSCE 155 Fall 2005
 * @author Leen-Kiat Soh
 * @version 1.0
 *
 */

class TestRecursion {

   /**
    * In the following method/function, think about the stopping condition,
    * end case, recursive step, and integrative step.
    * Also, if I re-order the statements in the else block, what happens?
    */

   public static void mysteryFunc(String str)  {

      int numOfChars = str.length();
      if (str.length() == 0)                         // stopping condition
	 System.out.print("-");                      // end case
      else  {
	 System.out.print(str.charAt(0));            // integrative step
	 mysteryFunc(str.substring(1,numOfChars));   // recursive  & 
                                                     //   integrative step
	 System.out.print(str.charAt(0));            // integrative step
      }

   }  // end mysteryFunc


   /**
    * In the following method/function, think about the stopping condition,
    * end case, recursive step, and integrative step.
    * What are they?  Now, what if the "if-else block" of  the end case is
    * replaced by "System.out.print("")", does the function still work
    * properly?  Why do we need to include a boolean "callItSelf"?
    */

   public static void convert(int number, boolean callItSelf) {

      if (number == 0)
	 if (!callItSelf)  {
	    System.out.print("0");
	    System.out.println("going back");
	    }
         else  {
	    System.out.print("");
	    System.out.println("going back");
	    }
      else  {
         convert(number/2, true);
         System.out.print(number%2);
	 System.out.println("returned from calling convert with number = " + number/2);
      }

   }  // end convert

   /**
    * What does this function do???
    */

   public static void convert2(int number) {

      if (number == 0)  {
	 // System.out.print("0");
         }
      else  {
         System.out.print("   ");
         System.out.println("Calling method with number = " + number/2);
         convert2(number/2);
         // System.out.print(number%2);
      }

   }  // end convert2


   public static void main(String args[])  {

      int choice = Integer.parseInt(args[0]); 

      if (choice == 1)  {   // if choice == 1, expect another number

         int convertNumber = Integer.parseInt(args[1]);
         convert(convertNumber, false); 
         // convert2(convertNumber); 
         System.out.println(""); 

      }  else if (choice == 2)  {  // if choice = 2, expect another string

	 String tryString = args[1];
	 mysteryFunc(tryString);
         System.out.println("");

      }

   }  // end main
       
}  // end TestRecursion



