
/* 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
   
*/

public class ThreeN1Console extends ConsoleApplet {

   protected String getTitle() {
      return "Sample program \"ThreeN\"";
   }

   protected void program() {
   
     /*  This program prints out a 3N+1 sequence
        starting from a positive integer specified
        by the user.  It also counts the number
        of terms in the sequence, and prints out
        that number.   */


      int N;       // for computing terms in the sequence
      int counter; // for counting the terms
      
      console.put("Starting point for sequence: ");
      N = console.getlnInt();
      while (N <= 0) {
         console.put("The starting point must be positive.");
         console.put("Please try again: ");
         N = console.getlnInt();
      }
      // At this point, we know that N > 0
      
      counter = 0;
      while (N != 1) {
          if (N % 2 == 0)
             N = N / 2;
          else
             N = 3 * N + 1;
          console.putln(N);
          counter = counter + 1;
      }
      
      console.putln();
      console.put("There were ");
      console.put(counter);
      console.putln(" terms in the sequence.");

   }

}
