/* 
   This file defines a program, ConsoleFrameThreeN1, that is 
   identical to the program in ThreeN1.java, except that instead
   of using the TextIO class for input/output, it uses a separate
   "console window".  The console window is an object belonging to
   the class Console.  Any program that can be written using TextIO
   can easily be converted to one that uses a Console object.  The
   advantage is that Console does not depend on Java standard IO
   streams, System.out and System.in.  See the file Console.java for
   more information about console windows.  See the file ConsoleApplet.java
   if you would like to a similar style of input/output in an applet.
   
   To convert a TextIO program to a Console program, do the following:
   
       1.  add the line
       
                static Console consoleWin = new Console();
                
           to the class.  This creates the console window.  (You can
           use a different name from "consoleWin" if you want.)
           
       2.  At the end of the main() routine, add the line
       
                consoleWin.close();
                
           This removes the console from the screen.  If you don't
           do this, the window will stay open even after the program
           ends.
           
       3.  Everyplace in the file where "TextIO" was used, substitute
           "consoleWin".  For example, "TextIO.getln();" should be
           changed to "consoleWin.getln()".
           
    You must also place the compiled files Console.class and ConsoleCanvas.class
    in the same directory as your main class file.  (Alternatively, if you
    are using an integrated developement enviroment such as Visual J++ or
    CodeWarrior, you could add the source files Console.java and
    ConsoleCanvas.java to your project.)  Of course, you won't need
    TextIO.class or TextIO.java with your converted program.

*/

public class ConsoleFrameThreeN1 {

     /*  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.   */
         
     static Console consoleWin = new Console();  // console window to be
                                                 // for input/output

     public static void main(String[] args) {                
       
       int N;       // for computing terms in the sequence
       int counter; // for counting the terms
       
       consoleWin.put("Starting point for sequence: ");
       N = consoleWin.getlnInt();
       while (N <= 0) {
          consoleWin.put("The starting point must be positive. Please try again: ");
          N = consoleWin.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;
           consoleWin.putln(N);
           counter = counter + 1;
       }
       
       consoleWin.putln();
       consoleWin.put("There were ");
       consoleWin.put(counter);
       consoleWin.putln(" terms in the sequence.");
       
       consoleWin.close();
                          
    }  // end of main()
                
}  // end of class ThreeN
