Source of Common.java



  3: import java.util.Random;
  4: import java.util.Scanner;

  6: /**
  7:  *
  8:  * @author Mark Young (A00000000)
  9:  */
 10: public class Common {

 12:     // ---------- Random Array Generation ---------- //
 13:     public static final Random RAND = new Random();

 15:     /**
 16:      * Generate an array of random numbers.
 17:      *
 18:      * @param howMany the number of elements in a list.
 19:      * @return an array with {@code howMany} integer values in the range
 20:      * [{@code begin}, {@code end}).
 21:      */
 22:     public static int[] randomNumbers(int howMany, int begin, int end) {
 23:         int[] result = new int[howMany];
 24:         for (int i = 0; i < howMany; ++i) {
 25:             result[i] = randomNumber(begin, end);
 26:         }
 27:         return result;
 28:     }

 30:     /**
 31:      * Generate a random number in the given range.
 32:      *
 33:      * @param begin the lower bound of the random value (included)
 34:      * @param end the upper bound of the random value (excluded)
 35:      * @return a random value in the range [{@code begin}, {@code end})
 36:      */
 37:     public static int randomNumber(int begin, int end) {
 38:         return RAND.nextInt(begin, end);
 39:     }

 41:     // ---------- Swapping Array Entries ---------- //
 42:     /**
 43:      * Swap two elements of an array.
 44:      *
 45:      * @param arr the array to swap elements of
 46:      * @param one one index to swap
 47:      * @param other the other index to swap
 48:      */
 49:     public static void swap(int[] arr, int one, int other) {
 50:         int temp = arr[one];
 51:         arr[one] = arr[other];
 52:         arr[other] = temp;
 53:     }

 55:     // ---------- Printing Arrays and Portions Thereof ---------- //
 56:     /**
 57:      * Print the elements of an array in a nice table.
 58:      *
 59:      * @param arr the array to print
 60:      */
 61:     public static void printArray(int[] arr) {
 62:         printArray(arr, 0, arr.length);
 63:     }

 65:     /**
 66:      * number of numbers printed per line
 67:      */
 68:     private static final int PER_LINE = 10;

 70:     /**
 71:      * number of spaces per number
 72:      */
 73:     private static final int SPACES = 80 / PER_LINE;

 75:     /**
 76:      * format string for place fillers
 77:      */
 78:     private static final String STR_FORMAT = "%" + SPACES + "s";
 79:     private static final String INT_FORMAT = "%" + SPACES + "d";

 81:     /**
 82:      * Print an array showing only the numbers in one part of it. Numbers before
 83:      * the starting point/after the ending point are replaced with "filler".
 84:      *
 85:      * @param arr the array to print
 86:      * @param lo positions less than lo are "filled"
 87:      * @param hi positions hi and greater are "filled"
 88:      */
 89:     public static void printArray(int[] arr, int lo, int hi) {
 90:         int thisLine = 0;
 91:         for (int i = 0; i < arr.length; i++) {
 92:             if (thisLine == PER_LINE) {
 93:                 System.out.println();
 94:                 thisLine = 0;
 95:             }
 96:             if (lo <= i && i < hi) {
 97:                 System.out.printf(INT_FORMAT, arr[i]);
 98:             } else {
 99:                 System.out.printf(STR_FORMAT, "...");
100:             }
101:             ++thisLine;
102:         }
103:         System.out.println();
104:     }

106:     /**
107:      * Print exactly two elements of an array. Items not in those positions are
108:      * replaced with "filler".
109:      *
110:      * @param arr the array to print
111:      * @param one the index of one position to print
112:      * @param other the index of the other position to print
113:      */
114:     public static void printTwoOf(int[] arr, int one, int other) {
115:         int thisLine = 0;
116:         for (int i = 0; i < arr.length; i++) {
117:             if (thisLine == PER_LINE) {
118:                 System.out.println();
119:                 thisLine = 0;
120:             }
121:             if (one == i || other == i) {
122:                 System.out.printf(INT_FORMAT, arr[i]);
123:             } else {
124:                 System.out.printf(STR_FORMAT, "...");
125:             }
126:             ++thisLine;
127:         }
128:         System.out.println();
129:     }

131:     /**
132:      * Print equally spaced elements of an array. Elements not to be printed are
133:      * replaced with "filler".
134:      *
135:      * @param arr the array to print
136:      * @param mod the distance between elements
137:      * @param off the offset from zero of the elements to print
138:      */
139:     public static void printOffset(int[] arr, int mod, int off) {
140:         int thisLine = 0;
141:         for (int i = 0; i < arr.length; i++) {
142:             if (thisLine == PER_LINE) {
143:                 System.out.println();
144:                 thisLine = 0;
145:             }
146:             if (i % mod == off) {
147:                 System.out.printf(INT_FORMAT, arr[i]);
148:             } else {
149:                 System.out.printf(STR_FORMAT, "...");
150:             }
151:             ++thisLine;
152:         }
153:         System.out.println();
154:     }

156:     // ---------- Pause and the Scanner ---------- //
157:     /**
158:      * A Scanner on System.in. The only one needed or wanted!
159:      */
160:     public static Scanner KBD = new Scanner(System.in);

162:     /**
163:      * Prompt the user and wait for them to press the enter key.
164:      */
165:     public static void pause() {
166:         System.out.print("\n...press enter...");
167:         KBD.nextLine();
168:         System.out.println();
169:     }

171: }