/**
 * TestArray.java -- This is an application/mainclass that illustrates how an
 * array is passed to a method, how to declare and create an array of primitie
 * data types, or reference data types, and how to return an array.  It has
 * several key issues here.
 *
 * Test 1 shows how to declare and create an array of doubles.
 * Test 2 shows how to declare and create an array of reference data types:
 * objects of the class Jedi.
 * Test 3 shows how to print information of an array's elements.
 * Test 4 shows how to search through an array.
 * Test 5 shows how arrays are returned from a method and how array pointers
 * work.
 * Test 6 shows how array pointers work and how arrays are passed into a
 * method.
 *
 * CSCE 155 Fall 2005
 * @author Leen-Kiat Soh
 * @version 1.0
 */

class TestArray {

   /**
    * This method creates an array of 2 integers, initializes them, and returns
    * the array.
    */

   public static int[] obtainInt()  {
      int[] number;
      number = new int[2];
      number[0] = 5;
      number[1] = 4;
      return number;

   }  // end obtainInt

   /**
    * This method has two tests.  Test 6.1 shows how arrays work when they are
    * used as reference data types in an assignment.  Pointers are key here.
    * Test 6.2 shows how the array elements that are of primitve data type work
    * just like an ordinary primitive data type -- changes to it do not affect
    * other variables.
    */

   public static void processInt(int[] number) {
 
      // Test 6.1
      int[] myNumber = number;

      // Test 6.2
      /*
      int[] myNumber = new int[2];
      myNumber[0] = number[0];
      myNumber[1] = number[1];
      */
 
      myNumber[0] = myNumber[0] + myNumber[1];
      myNumber[1] = myNumber[0]*myNumber[1];

      System.out.println("myNumber[0] = " + myNumber[0]);
      System.out.println("myNumber[1] = " + myNumber[1]);

   }  // end processInt

   /**
    * This is a way of handling adding an element to an array in an elegant
    * way:  This method handles the counter of the array -- keeping it updated
    * at all times so that one knows the actual number of elements in the
    * array.
    * @params Jedi[] council -- an array of Jedi objects
    * @params Jedi newJedi -- the Jedi to be added to the array
    * @params int counter -- the counter that keeps track of the number of Jedi
    * objects in the array.
    */

   public static int  addJediToCouncil(Jedi[] council, Jedi newJedi, 
				       int counter)  {

      council[counter] = newJedi;
      counter++;
      return counter;

   }  // end addJediToCouncil


   /**
    * New method!
    */

   public static int removeJediFromCouncil(Jedi[] council, int indexToBeDeleted, 
				       int counter)  {

      council[indexToBeDeleted] = council[counter - 1];
      counter--;
      return counter;

   }  // end removeJediFromCouncil

   /**
    * This main method has 4 tests.  It calls three other static methods.
    * This is not object-oriented programming.  It is procedural.  Do not
    * imitate the programming style here.  This is mainly written to illustrate
    * array manipulation.
    */

   public static void main (String[] args)  {


      /*
      // Test 1
      //
      double[] rainfall;
      rainfall = new double[12];
      rainfall[0] = 13.0;
      rainfall[1] = 12.5;
      for (int i = 0; i < 12; i++)
	  System.out.println("rainfall at [" + i + "] is " + rainfall[i]);
      System.out.println(rainfall.length);

      String[] months;
      months = new String[12];
      months[0] = "January";
      months[1] = "February";
      for (int i = 0; i < 12; i++)
	  System.out.println("months at [" + i + "] is " + months[i]);
      System.out.println(months.length);

      */


      // Test 2
      //
      Jedi[] council;
      council = new Jedi[12];
      Jedi luke = new Jedi(7,"luke");
      int arrayCount = 0;
      arrayCount = addJediToCouncil(council, luke, arrayCount);
      System.out.println("arrayCount = " + arrayCount);

      Jedi anakin = new Jedi(10,"anakin");
      arrayCount = addJediToCouncil(council, anakin, arrayCount);
      System.out.println("arrayCount = " + arrayCount);

      Jedi obiwan = new Jedi(5,"obiwan");
      arrayCount = addJediToCouncil(council, obiwan, arrayCount);
      System.out.println("arrayCount = " + arrayCount);


      // Test 3
      for (int i = 0; i < arrayCount; i++)
         council[i].printInfo();

      /*

      // Test 4
      int bestSoFar = -1;
      int bestSoFarIndex = -1;
      for (int i = 0; i < arrayCount; i++)  {
	  if (council[i].getForce() > bestSoFar)  {
	     bestSoFar = council[i].getForce();
	     bestSoFarIndex = i;
	     }
	  }

      System.out.println("The best Jedi is: ");
      council[bestSoFarIndex].printInfo();

      // council[1] = null;
      arrayCount = removeJediFromCouncil(council, 1, arrayCount);

      for (int i = 0; i < arrayCount; i++)
         council[i].printInfo();

      System.out.println("length of council array is " + council.length);
      System.out.println("number of Jedis array is " + arrayCount);


      */

      // Test 5
      int[] num = new int[3];
      num = obtainInt();
      System.out.println("initial values");
      System.out.println(num[0]);
      System.out.println(num[1]);
      System.out.println("length of num is "  + num.length);

     
      // Test 6
      processInt(num);

      System.out.println("processed values");
      System.out.println(num[0]);
      System.out.println(num[1]);

      /*
      Jedi persons[];
      persons = new Jedi[10];
      persons[0] = new Jedi(10,"Darth");
      persons[1] = new Jedi(10,"Obiwan");
      Jedi temp = new Jedi(10,"Luke");
      persons[0].printInfo();
      persons[1].printInfo();
      temp.printInfo();

      System.out.println("After swapping ... ");
      temp = persons[0];
      persons[0] = persons[1];
      persons[1] = temp;

      persons[0].printInfo();
      persons[1].printInfo();
      temp.printInfo();
    
      */



   }

}

