

import java.util.Random;
import java.util.Scanner;

/**
 *
 * @author Mark Young (A00000000)
 */
public class Common {

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

    /**
     * Generate an array of random numbers.
     *
     * @param howMany the number of elements in a list.
     * @return an array with {@code howMany} integer values in the range
     * [{@code begin}, {@code end}).
     */
    public static int[] randomNumbers(int howMany, int begin, int end) {
        int[] result = new int[howMany];
        for (int i = 0; i < howMany; ++i) {
            result[i] = randomNumber(begin, end);
        }
        return result;
    }

    /**
     * Generate a random number in the given range.
     *
     * @param begin the lower bound of the random value (included)
     * @param end the upper bound of the random value (excluded)
     * @return a random value in the range [{@code begin}, {@code end})
     */
    public static int randomNumber(int begin, int end) {
        return RAND.nextInt(begin, end);
    }

    // ---------- Swapping Array Entries ---------- //
    /**
     * Swap two elements of an array.
     *
     * @param arr the array to swap elements of
     * @param one one index to swap
     * @param other the other index to swap
     */
    public static void swap(int[] arr, int one, int other) {
        int temp = arr[one];
        arr[one] = arr[other];
        arr[other] = temp;
    }

    // ---------- Printing Arrays and Portions Thereof ---------- //
    /**
     * Print the elements of an array in a nice table.
     *
     * @param arr the array to print
     */
    public static void printArray(int[] arr) {
        printArray(arr, 0, arr.length);
    }

    /**
     * number of numbers printed per line
     */
    private static final int PER_LINE = 10;

    /**
     * number of spaces per number
     */
    private static final int SPACES = 80 / PER_LINE;

    /**
     * format string for place fillers
     */
    private static final String STR_FORMAT = "%" + SPACES + "s";
    private static final String INT_FORMAT = "%" + SPACES + "d";

    /**
     * Print an array showing only the numbers in one part of it. Numbers before
     * the starting point/after the ending point are replaced with "filler".
     *
     * @param arr the array to print
     * @param lo positions less than lo are "filled"
     * @param hi positions hi and greater are "filled"
     */
    public static void printArray(int[] arr, int lo, int hi) {
        int thisLine = 0;
        for (int i = 0; i < arr.length; i++) {
            if (thisLine == PER_LINE) {
                System.out.println();
                thisLine = 0;
            }
            if (lo <= i && i < hi) {
                System.out.printf(INT_FORMAT, arr[i]);
            } else {
                System.out.printf(STR_FORMAT, "...");
            }
            ++thisLine;
        }
        System.out.println();
    }

    /**
     * Print exactly two elements of an array. Items not in those positions are
     * replaced with "filler".
     *
     * @param arr the array to print
     * @param one the index of one position to print
     * @param other the index of the other position to print
     */
    public static void printTwoOf(int[] arr, int one, int other) {
        int thisLine = 0;
        for (int i = 0; i < arr.length; i++) {
            if (thisLine == PER_LINE) {
                System.out.println();
                thisLine = 0;
            }
            if (one == i || other == i) {
                System.out.printf(INT_FORMAT, arr[i]);
            } else {
                System.out.printf(STR_FORMAT, "...");
            }
            ++thisLine;
        }
        System.out.println();
    }

    /**
     * Print equally spaced elements of an array. Elements not to be printed are
     * replaced with "filler".
     *
     * @param arr the array to print
     * @param mod the distance between elements
     * @param off the offset from zero of the elements to print
     */
    public static void printOffset(int[] arr, int mod, int off) {
        int thisLine = 0;
        for (int i = 0; i < arr.length; i++) {
            if (thisLine == PER_LINE) {
                System.out.println();
                thisLine = 0;
            }
            if (i % mod == off) {
                System.out.printf(INT_FORMAT, arr[i]);
            } else {
                System.out.printf(STR_FORMAT, "...");
            }
            ++thisLine;
        }
        System.out.println();
    }

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

    /**
     * Prompt the user and wait for them to press the enter key.
     */
    public static void pause() {
        System.out.print("\n...press enter...");
        KBD.nextLine();
        System.out.println();
    }

}
