brainfrz

NumberGenerator

Jan 24th, 2016
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.35 KB | None | 0 0
  1. /**********************************************************
  2.  * Program Name   : Week 1 - Number Generator
  3.  * Author         : Terry Weiss
  4.  * Date           : January 22, 2016
  5.  * Course/Section : CSC264 - 001
  6.  * Program Description: This program will will generate a random number of integers. First, it will
  7.  *     generate a random number between 5 and 50 (inclusive) which represents the he number of
  8.  *     values to generate. Then, it generates that many values and stores them in an array.  These
  9.  *     numbers will be between 0 and 100 (inclusive).  Finally, it displays the numbers generated.
  10.  *
  11.  * Methods:
  12.  * -------
  13.  * Main - Generates a random number of integers and displays them
  14.  **********************************************************/
  15.  
  16. import java.util.Random;
  17.  
  18.  
  19. public class NumberGenerator
  20. {
  21.     //class constants
  22.     private static final int MIN_INTS  = 5, MAX_INTS  =  50;    //Range of numbers to be generated
  23.     private static final int MIN_RANGE = 0, MAX_RANGE = 100;    //Legal range of generated numbers
  24.    
  25.     private static final int TABLE_COLUMNS  =  5;               //Number of columns in output table
  26.    
  27.     //class variables
  28.     private static Random generator = new Random();             //Random number generator
  29.    
  30.  
  31.     /**********************************************************
  32.     * Method Name    : NumberGenerator - Main
  33.     * Author         : Terry Weiss
  34.     * Date           : January 22, 2016
  35.     * Course/Section : CSC264 - 001
  36.     * Program Description: This method will generate a random number of integers between 5 and 10,
  37.     *     store them in the array, and then display them.
  38.     *
  39.     * BEGIN Main
  40.     *     Calc number of integers to generate between 5 and 50 (inclusive)
  41.     *     Init array the size of the generated number
  42.     *     Init current number to 0
  43.     *     FOR (each number in array) DO
  44.     *         Assign random number between 0 and 100 to current cell
  45.     *     END FOR
  46.     *     Display title
  47.     *     Display top of table
  48.     *     WHILE (there are numbers not displayed) DO
  49.     *         Display left edge of table
  50.     *         FOR (each cell in table row) DO
  51.     *             IF (numbers are left in the number list)
  52.     *                 Display formatted cell value and cell's right edge
  53.     *             ELSE
  54.     *                 Display empty cell and cell's right edge
  55.     *             END IF
  56.     *             Increment current number
  57.     *         END FOR
  58.     *         Display EOL
  59.     *     END WHILE
  60.     *     Display bottom of table
  61.     * END Main
  62.     **********************************************************/
  63.     public static void main (String [] args)
  64.     {
  65.         //local constants
  66.         final int NUMBERS;                                          //Number of integers to generate
  67.         final String BORDER = "+-----------------------------+";    //Top and bottom border
  68.        
  69.  
  70.         //local variables
  71.         int[] numberList;       //Array of generated integers
  72.         int tablePadding = 24;  //Number of columns in front of output table
  73.         int currentNumber;      //Current number drawn in table
  74.        
  75.  
  76. //        Library64 myLib = new Library64();
  77.  
  78.         /**********************************  Start main method  ***********************************/
  79.  
  80.         //Calc number of integers to generate between 5 and 10 (inclusive)
  81.         NUMBERS = generator.nextInt(MAX_INTS - MIN_INTS + 1) + MIN_INTS;
  82.        
  83.         //Init array the size of the generated number
  84.         numberList = new int[NUMBERS];
  85.        
  86. System.out.println("**DEBUG**: " + NUMBERS + " numbers will be generated.");
  87.        
  88.  
  89.         //FOR (each number in array) DO
  90.         for (int i = 0; i < NUMBERS; i++)
  91.         {
  92.             //Assign random number between 0 and 100 to current cell
  93.             numberList[i] = generator.nextInt(MAX_RANGE + 1); //+1 to include max bound
  94.         }//end for
  95.        
  96.        
  97.         //Init current number to 0
  98.         currentNumber = 0;
  99.  
  100.         //Display title centered
  101.         System.out.println(Util.setLeft(32, "Number Generator\n"));
  102.        
  103.         //Display top of table
  104.         System.out.println(Util.setLeft(tablePadding, BORDER));
  105.        
  106.         //WHILE (there are numbers not displayed) DO
  107.         while (currentNumber < NUMBERS)
  108.         {
  109.             //Display left edge of table
  110.             System.out.print(Util.setLeft(tablePadding, "|"));
  111.            
  112.             //FOR (each cell in table row) DO
  113.             for (int i = 0; i < TABLE_COLUMNS; i++)
  114.             {
  115.                 //IF (numbers are left in the number list)
  116.                 if (NUMBERS - currentNumber > 0)
  117.                 {
  118.                     //Display formatted cell value and cell's right edge
  119.                     System.out.printf(" %3d |", numberList[currentNumber]);
  120.                 }
  121.                 else
  122.                 {
  123.                     //Display empty cell and cell's right edge
  124.                     System.out.print("     |");
  125.                 }//end if
  126.  
  127.                 //Increment current number
  128.                 currentNumber++;
  129.             }//end for
  130.            
  131.             //Display EOL
  132.             System.out.println();
  133.         }//end while
  134.        
  135.         //Display bottom of table
  136.         System.out.println(Util.setLeft(tablePadding, BORDER));
  137.     } //end main method
  138.  } //end Template264
Advertisement
Add Comment
Please, Sign In to add comment