import java.util.Scanner;

/**
 * Some simple array manipulations.
 *
 * @author Mark Young (A00000000)
 */
public class ArrayBasics {

    /**
     * Run this program.
     *
     * @param args command lines arguments (ignored)
     */
    public static void main(String[] args) {
        // introduce self
        System.out.println("\n"
            + "Just doing some array things. "
            + "Look at the code and compare it to the output.\n");

        // an array type is just another data type
        int anIntVar;                   // type int
        int[] aBunchOfIntVars;          // type int[]

        // you can make an array out of /any/ data type
        double[] aBunchOfDoubleVars;    // doubles
        String[] aBunchOfStringVars;    // Strings
        Scanner[] aBunchOfScannerVars;  // Scanners
        int[][] aBunchOfIntArrayVars;   // int[]s (!)

        // like other variables, array vars need to be given values
        // like Scanners, use the word "new" to make the value
        Scanner kbd = new Scanner(System.in);
        // unlike Scanners, it's []s after the data type, not ()s
        double[] someNumbers = new double[20];
        String[] someWords = new String[1000];

        // size of the array can be a variable
        System.out.print("How many lines will you be entering? ");
        int numLines = kbd.nextInt();   kbd.nextLine();
        String[] theLines = new String[numLines];

        // Array usually used as a group
        // (doing the same thing to each element)
        for (int i = 0; i < someNumbers.length; i++) {
            // pick out array elements using []
            someNumbers[i] = Math.sqrt(i);
            // number inside the brackets is called an index
        }
        // but you can use array elements as individual variables
        System.out.println("someNumbers[10] == " + someNumbers[10]);

        // read some lines
        System.out.println("\n"
            + "Let's enter those lines, now:");
        for (int i = 0; i < numLines; i++) {
            theLines[i] = kbd.nextLine();   // no need to tidy up
        }

        // print the lines (reversed)
        System.out.println("\n"
            + "Here are the lines you entered, in reverse order:");
        for (int i = numLines - 1; i >= 0; i--) {
            System.out.println("\t\"" + theLines[i] + "\"");
        }
        System.out.print("\n---Press Enter to continue---");
        kbd.nextLine();

        // print the square roots
        System.out.println("\n"
            + "And here are the square roots I calculated earlier:");
        for (int i = 0; i < someNumbers.length; i++) {
            System.out.println("\tsqrt(" + i + ") is " + someNumbers[i]);
        }
        System.out.println();
    }

}
