/**
 * Demo array code from lecture on 3/2/2016
 * @author Nathan Sprague
 *
 */
public class ArrayDemo {

   public static void main(String[] args) {

      // Declare an array variable.
      int[] profits;

      // Create new array with 12 entries.
      profits = new int[12];

      // Modify the first and last entries.
      profits[0] = 1000;
      profits[11] = 10000;

      // Make an alias for profits (not a copy)...
      int[] boosted;
      boosted = profits; // This is an alias. Still only one array.

      increaseProfits(boosted);

      // Both will appear modified, because there is only one array.
      System.out.println("BOOSTED:");
      printArray(boosted);
      System.out.println("ORIGINAL:");
      printArray(profits);

      // Now make a proper copy of profits...
      int[] copy;
      copy = new int[profits.length];

      for (int i = 0; i < profits.length; i++) {
         copy[i] = profits[i];
      }

      increaseProfits(copy);

      // Only copy will be modified.
      System.out.println("\nCOPY:");
      printArray(copy);
      System.out.println("ORIGINAL:");
      printArray(profits);

      // In class-exercises...

      int[] test = { 1, 2, 3 };

      System.out.println("\nTOTAL:");
      System.out.println(total(test));

      System.out.println("MAX:");
      System.out.println(max(test));

      System.out.println("MAX:");
      System.out.println(maxBetter(test));

   }

   public static void increaseProfits(int[] money) {
      for (int i = 0; i < money.length; i++) {
         money[i] += 100;
      }
   }

   public static void printArray(int[] array) {

      for (int i = 0; i < array.length; i++) {
         System.out.print(array[i] + " ");
      }
      System.out.println();

   }

   /**
    * Returns the largest number in numbers.
    */
   public static int max(int[] numbers) {
      int biggest = -9999999;

      for (int i = 0; i < numbers.length; i++) {
         if (numbers[i] > biggest) {
            biggest = numbers[i];
         }
      }

      return biggest;

   }

   /**
    * Returns the largest number in numbers.
    */
   public static int maxBetter(int[] numbers) {
      int biggest = numbers[0];

      for (int i = 1; i < numbers.length; i++) {
         if (numbers[i] > biggest) {
            biggest = numbers[i];
         }
      }

      return biggest;

   }

   /**
    * Return the sum of all entries in the array.
    */
   public static int total(int[] numbers) {
      int sum = 0;

      for (int i = 0; i < numbers.length; i++) {
         sum += numbers[i];
      }

      return sum;

   }

}