home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / JAVA / NOTES / SOURCE / threen2.jav < prev    next >
Text File  |  1996-12-20  |  3KB  |  92 lines

  1.  
  2. /* Simulation of console-I/O program ThreeN,
  3.    using ConsoleApplet as a basis.  See the file
  4.    ConsoleApplet.java for more information.
  5.    
  6.    David Eck
  7.    eck@hws.edu
  8.    
  9.    August 11, 1996
  10. */
  11.  
  12. public class ThreeN2Console extends ConsoleApplet {
  13.  
  14.    public void init() {
  15.       title = "Sample program \"ThreeN\" (#2)";
  16.       super.init();
  17.    }
  18.  
  19.    final static int numberOfColumns = 5;  // constant specifying number
  20.                                           // of terms on each output line
  21.                 
  22.    final static int columnWidth = 8;  // constant specifying the width,
  23.                                       // in number of characters, of each
  24.                                       // column in the output
  25.                                      
  26.    protected void program() {
  27.    
  28.      /*
  29.              A program that computes and displays several 3N+1
  30.              sequences.  Starting values for the sequences are
  31.              input by the user.  Terms in a sequence are printed
  32.              in columns, with several terms on each line of output.
  33.              After a sequence has been displayed, the number of
  34.              terms in that sequence is reported to the user.
  35.      */
  36.  
  37.        console.putln("This program will print out 3N+1 sequences");
  38.        console.putln("for starting values that you specify.");
  39.        console.putln();
  40.        int K=0;
  41.        do {
  42.           console.putln("Enter a starting value;");
  43.           console.put("To end the program, enter 0: ");
  44.           K = console.getInt();  // get starting value from user
  45.           if (K > 0)   // print sequence, but only if K is > 0
  46.              Print3NSequence(K);
  47.        } while (K > 0);   // continue only if K > 0     
  48.      
  49.     } // end program()
  50.     
  51.     
  52.     void Print3NSequence(int startingValue) {
  53.  
  54.         // prints a 3N+1 sequence on the console, using
  55.         // startingValue as the initial value of N
  56.  
  57.         int N = startingValue;  // N represents a term in the sequence.
  58.         int count = 1;   // count is the number of terms found
  59.         int onLine = 1;  // onLine counts terms on current output line.
  60.  
  61.         console.putln("The 3N+1 sequence starting from " + N);
  62.         console.putln();
  63.         console.put(N, columnWidth);  // print initial term of sequence
  64.  
  65.         while (N > 1) {
  66.             N = nextN(N);  // compute next term
  67.             count++;   // count this term
  68.             if (onLine == numberOfColumns) { // if current output line is full
  69.                console.putln();  // then output a carriage return
  70.                onLine = 0;   // and note that there are no terms on the new line
  71.             }
  72.             console.put(N, columnWidth);  // print this term
  73.              onLine++;   // add 1 to record of number of terms on this line
  74.          }
  75.  
  76.          console.putln();  // end current line of output
  77.          console.putln();  // and then add a blank line
  78.          console.putln("There were " + count + " terms in the sequence.");
  79.  
  80.     }  // end of Print3NSequence()
  81.                          
  82.                          
  83.     int nextN(int currentN) {
  84.        // computes and returns the next term in a 3N+1 sequence
  85.        if (currentN % 2 == 1)
  86.             return 3 * currentN + 1;
  87.        else
  88.             return currentN / 2;
  89.     }  // end of nextN()             
  90.  
  91. }  //end class GuessingGameConsole
  92.