
import java.util.*;
import java.io.*;

public class HelpArrays
{
  public static void main(String [] args)
  {
    System.out.println("Java Arrays");
    System.out.println();
    System.out.println();

/*
An Array is a class!
Remember that a class is a container.
It contains mainly variables and methods.
(and constants and constructors)

The Array class
===============

A class is a container.  
It contains ????


Variables
---------
A list of elements or items that we define.
They must be all of the same data type.
You can not change the length of an array!
Once created, that is it.

Example 1: int [ ] list = new int[5];
           
           Creates 5 int variables (boxes).
           The variables are all set to 0.

Think of a series of boxes (int variables)
indexes       0      1       2      3      4
          -------------------------------------
 list --> |   0  |   0   |   0  |   0  |   0  |
          -------------------------------------
 
Example 2: int [ ] list = { 98, 100, 76, 85, 99 };
           Creates 5 int variables (boxes).

 Think of a series of boxes (int variables)
 indexes      0      1       2      3      4
          -------------------------------------
 list --> |  98  |  100  |  76  |  85  |  99  |
          -------------------------------------
  
  
Constants (Variables marked as final)
-------------------------------------
length   (public final int length = ?;)
This final int variable holds the number of
items or elements.  
NOTE: IT IS NOT A METHOD, so no ( )

Methods in an Array Object:
---------------------------
None (of it's own, however it does inherit some methods)
(More about inheritance later)
*/

    
    // *********************************************
    // Example 3:
    // *********************************************
    //   Strings actually store the chars in an array.
    //   Think of a series of boxes (char variables).
    //   indexes       0       1       2
    //             ------------------------
    // letters --> |   B   |   o   |   b  |
    //             ------------------------
    
    char [ ] letters = {'B', 'o', 'b'};
    
    letters[0] = 'R'; // changes 'B' to 'R' in position 0
    
    // prints out:
    // R
    // o
    // b 
    System.out.println();
    System.out.println("Example 3: letters");
    for (int i=0; i < letters.length; i++)
    {
      System.out.println(letters[i]);
    }
    


    // *********************************************
    // Example 4:
    // *********************************************
    //
    //   Think of a series of boxes (String variables)
    //   indexes      0       1     2
    //             ----------------------
    // players --> | Bill  | Sue | Jill |
    //             ----------------------

/*
    String [ ] players = {"Bill", "Sue", "Jill"};
    
    players[0] = "Tim"; // changes "Bill" to "Tim" in position 0
    
    // prints out:
    // Tim
    // Sue
    // Jill 
    System.out.println();
    System.out.println("Example 4: players");
    for (int i=0; i < players.length; i++)
    {
      System.out.println(players[i]);
    }
    
*/
    

/*
    // *********************************************
    // Example 5:
    // *********************************************
    //
    //   Think of a series of boxes (String variables)
    //   indexes      0       1     2
    //             ----------------------
    // players --> | Bill  | Sue | Jill |
    //             ----------------------
    
    String  [ ]  players = new String[3]; 
    players[0] = "Bill";
    players[1] = "Sue";
    players[2] = "Jill";    
    players[0] = "Tim"; // changes "Bill" to "Tim" in position 0
    
    // prints out:
    // Tim
    // Sue
    // Jill 
    System.out.println();
    System.out.println("Example 5: players");
    
    // A for each loop!
    // Variable player will receive 1 item from players
    // each time through the loop.
    
    // General Structure of a for each loop.
    // for ( variable to hold 1 item : the list)
    
    for (String player : players)
    {
      System.out.println(player);
    }
*/
    
    

    // *********************************************
    // Example 6:
    // *********************************************
    //
    //   Think of a series of boxes (int variables)
    //   indexes       0      1       2
    //             -----------------------
    //  grades --> |  98  |  100  |  95  |
    //             -----------------------
    
    int [ ] grades = new int[3]; 
    grades[0] = 98;
    grades[1] = 100;
    grades[2] = 95;    
    grades[0] = 99; // changes 98 to 99 in position 0
    
    // prints out:
    // 99 100 95
    System.out.println();
    System.out.println("Example 6: grades");
    for (int i=0; i < grades.length; i++)
    {
      System.out.print(grades[i] + " ");
    }
    System.out.println();

    System.out.println();
    double sum = 0;
    for (int i=0; i < grades.length; i++)
    {
        sum = sum + grades[i];
    }
    System.out.println("Your grade is: " + sum/grades.length);
    System.out.println();
 
    
    
    
    // Let's count the number of even numbers
    // *********************************************
    // Example 7:
    // *********************************************
    //
    //   Think of a series of boxes (int variables)
    //   indexes         0      1       2      3      4
    //               -------------------------------------
    //   numbers --> |  98  |  100  |  95  |  72  |  99  |
    //               -------------------------------------
    
    int [ ] numbers = new int[5]; 
    numbers[0] = 98;
    numbers[1] = 100;
    numbers[2] = 95;    
    numbers[3] = 72; 
    numbers[4] = 99; 
    
    
    // prints out:
    // 98 100 95 72 99
    System.out.println();
    System.out.println("Example 7: Even numbers");
    for (int i=0; i < numbers.length; i++)
    {
      // print out 1 number from the list numbers
      System.out.print(numbers[i] + " ");
    }
    System.out.println();

    int numEvens = 0;
    for (int i=0; i < numbers.length; i++)
    {
        // Check to see if the ith number in the numbers 
        // list is an even number.  If so, add 1 to the 
        // variable numEvens.
        if (numbers[i] % 2 != 0)
            numEvens++;
              
    }
    System.out.println("Number of Evens: " + numEvens);
    System.out.println();
       

    
    
    
    // Let's find our shopping list bill.
    // *********************************************
    // Example 8:
    // *********************************************
    //
    //   Think of a series of boxes (double variables)
    //   indexes         0      1      2      3      4
    //               ------------------------------------
    //     items --> | 9.40 | 1.29 | 0.50 | 4.99 | 2.79 |
    //               ------------------------------------


    double  [ ]  items =  new double[5]; 
    items[0] = 9.40;
    items[1] = 1.29;
    items[2] = .50;    
    items[3] = 4.99; 
    items[4] = 2.79; 
    
    
    // prints out:
    // 9.4 1.29 0.5 4.99 2.79
    System.out.println();
    System.out.println("Example 7: Prices");
    for (int i=0; i < items.length; i++)
    {
      // print out 1 item
      System.out.print(items[i] + " ");
    }
    System.out.println();

    double totalBill = 0;
    for (int i=0; i < items.length; i++)
    {
       totalBill += items[i]; 
    }
    System.out.println("Total Bill: " + totalBill);
    System.out.println();
       
    
    
    
    
    
    
    
  } // end of method main
  
} // end of public class HelpArrays



