
/* Simulation of console-I/O program ThreeN,
   using ConsoleApplet as a basis.  See the file
   ConsoleApplet.java for more information.
   
   David Eck
   eck@hws.edu
   
   August 11, 1996
*/

public class ThreeN2Console extends ConsoleApplet {

   public void init() {
      title = "Sample program \"ThreeN\" (#2)";
      super.init();
   }

   final static int numberOfColumns = 5;  // constant specifying number
                                          // of terms on each output line
                
   final static int columnWidth = 8;  // constant specifying the width,
                                      // in number of characters, of each
                                      // column in the output
                                     
   protected void program() {
   
     /*
             A program that computes and displays several 3N+1
             sequences.  Starting values for the sequences are
             input by the user.  Terms in a sequence are printed
             in columns, with several terms on each line of output.
             After a sequence has been displayed, the number of
             terms in that sequence is reported to the user.
     */

       console.putln("This program will print out 3N+1 sequences");
       console.putln("for starting values that you specify.");
       console.putln();
       int K=0;
       do {
          console.putln("Enter a starting value;");
          console.put("To end the program, enter 0: ");
          K = console.getInt();  // get starting value from user
          if (K > 0)   // print sequence, but only if K is > 0
             Print3NSequence(K);
       } while (K > 0);   // continue only if K > 0     
     
    } // end program()
    
    
    void Print3NSequence(int startingValue) {

        // prints a 3N+1 sequence on the console, using
        // startingValue as the initial value of N

        int N = startingValue;  // N represents a term in the sequence.
        int count = 1;   // count is the number of terms found
        int onLine = 1;  // onLine counts terms on current output line.

        console.putln("The 3N+1 sequence starting from " + N);
        console.putln();
        console.put(N, columnWidth);  // print initial term of sequence

        while (N > 1) {
            N = nextN(N);  // compute next term
            count++;   // count this term
            if (onLine == numberOfColumns) { // if current output line is full
               console.putln();  // then output a carriage return
               onLine = 0;   // and note that there are no terms on the new line
            }
            console.put(N, columnWidth);  // print this term
             onLine++;   // add 1 to record of number of terms on this line
         }

         console.putln();  // end current line of output
         console.putln();  // and then add a blank line
         console.putln("There were " + count + " terms in the sequence.");

    }  // end of Print3NSequence()
                         
                         
    int nextN(int currentN) {
       // computes and returns the next term in a 3N+1 sequence
       if (currentN % 2 == 1)
            return 3 * currentN + 1;
       else
            return currentN / 2;
    }  // end of nextN()             

}  //end class GuessingGameConsole
