/********************************************************** * Program Name : Week 1 - Number Generator * Author : Terry Weiss * Date : January 22, 2016 * Course/Section : CSC264 - 001 * Program Description: This program will will generate a random number of integers. First, it will * generate a random number between 5 and 50 (inclusive) which represents the he number of * values to generate. Then, it generates that many values and stores them in an array. These * numbers will be between 0 and 100 (inclusive). Finally, it displays the numbers generated. * * Methods: * ------- * Main - Generates a random number of integers and displays them **********************************************************/ import java.util.Random; public class NumberGenerator { //class constants private static final int MIN_INTS = 5, MAX_INTS = 50; //Range of numbers to be generated private static final int MIN_RANGE = 0, MAX_RANGE = 100; //Legal range of generated numbers private static final int TABLE_COLUMNS = 5; //Number of columns in output table //class variables private static Random generator = new Random(); //Random number generator /********************************************************** * Method Name : NumberGenerator - Main * Author : Terry Weiss * Date : January 22, 2016 * Course/Section : CSC264 - 001 * Program Description: This method will generate a random number of integers between 5 and 10, * store them in the array, and then display them. * * BEGIN Main * Calc number of integers to generate between 5 and 50 (inclusive) * Init array the size of the generated number * Init current number to 0 * FOR (each number in array) DO * Assign random number between 0 and 100 to current cell * END FOR * Display title * Display top of table * WHILE (there are numbers not displayed) DO * Display left edge of table * FOR (each cell in table row) DO * IF (numbers are left in the number list) * Display formatted cell value and cell's right edge * ELSE * Display empty cell and cell's right edge * END IF * Increment current number * END FOR * Display EOL * END WHILE * Display bottom of table * END Main **********************************************************/ public static void main (String [] args) { //local constants final int NUMBERS; //Number of integers to generate final String BORDER = "+-----------------------------+"; //Top and bottom border //local variables int[] numberList; //Array of generated integers int tablePadding = 24; //Number of columns in front of output table int currentNumber; //Current number drawn in table // Library64 myLib = new Library64(); /********************************** Start main method ***********************************/ //Calc number of integers to generate between 5 and 10 (inclusive) NUMBERS = generator.nextInt(MAX_INTS - MIN_INTS + 1) + MIN_INTS; //Init array the size of the generated number numberList = new int[NUMBERS]; System.out.println("**DEBUG**: " + NUMBERS + " numbers will be generated."); //FOR (each number in array) DO for (int i = 0; i < NUMBERS; i++) { //Assign random number between 0 and 100 to current cell numberList[i] = generator.nextInt(MAX_RANGE + 1); //+1 to include max bound }//end for //Init current number to 0 currentNumber = 0; //Display title centered System.out.println(Util.setLeft(32, "Number Generator\n")); //Display top of table System.out.println(Util.setLeft(tablePadding, BORDER)); //WHILE (there are numbers not displayed) DO while (currentNumber < NUMBERS) { //Display left edge of table System.out.print(Util.setLeft(tablePadding, "|")); //FOR (each cell in table row) DO for (int i = 0; i < TABLE_COLUMNS; i++) { //IF (numbers are left in the number list) if (NUMBERS - currentNumber > 0) { //Display formatted cell value and cell's right edge System.out.printf(" %3d |", numberList[currentNumber]); } else { //Display empty cell and cell's right edge System.out.print(" |"); }//end if //Increment current number currentNumber++; }//end for //Display EOL System.out.println(); }//end while //Display bottom of table System.out.println(Util.setLeft(tablePadding, BORDER)); } //end main method } //end Template264