CS100J, Spring 2001
Thurs 3/8
Lecture 14
-------------------------------------------------------------------------------
Announcements:
+ P4 due Tues 3/13
+ T2 on Thurs 3/15
-------------------------------------------------------------------------------
Topics:
+ wrap-up on OOP:
  - $static$: class variables and class methods
  - method and constructor overloading
+ intro to arrays
-------------------------------------------------------------------------------
Summary from Lecture 13
+ encapsulation:
  use $private$ to modify "important" variables (usually instance variables)
  and methods used only by class (called UTITLITY METHODS)
+ $this$: a reference variable that means "a reference to the current object"
-------------------------------------------------------------------------------
$static$:
+ make an field visible at the class scope
+ terminology:
  static variable: class variable
  static method:   class method
+ use the class name to access and/or change the field
+ if the field is $private$, then only objects of the same class can use
  the $static$ field (that's why $static$ fields tend to also be $public$)
+ once a static variable is changed, the new value is shared by all objects
  that are instantiated from the class
+ an object can be used to change a $static$ variable!
+ typically $static$ variables are also $final$ to prevent chaotic changes
  (but $final$ and $static$ are completely different: $final$ prevents a
  variable value from changing)
+ see Math class for examples
+ why is $main$ modified as $static$? no object instantiated from another
  class when you run an application
-------------------------------------------------------------------------------
method/constructor overloading:
+ use same method name with different # of params, param types, order of params
+ overloading does not consist of:
  - renaming formal params but keeping everything else the same
  - changing the return type
+ constructors can be overloaded as well
-------------------------------------------------------------------------------
ARRAYS:
+ what are arrays, in general?
  - arrays are collections of items
  - think tables, spreadsheets
  - math definitions: see linear algebra
+ why arrays?
  - want to manipulate/move/handle many items
  - data storage
  - science/engineering models/experiments are discrete
  - programming: compound information of the same type
    cuts out a LOT of redundancy!
-------------------------------------------------------------------------------
Math Notation:

Imagine an experiment:

  i  |  x
-----+-----
  0  |  1      becomes an array  x = [ 1 17 4 ]
  1  |  17
  2  |  4

+ x    is the ith element in array x
   i 
+ i is the index or subscript 
+ may also see {elements} or [elements] in math
-------------------------------------------------------------------------------
Java's arrays:
+ arrays are objects!
+ all elements must have the same type (or class)
+ indicies must be integers or expressions that evaluate to integers
+ most basic array is a 1-D collection of items
-------------------------------------------------------------------------------
Creating arrays:
+ declare:
  type[] name
  type name[]
+ assign:
  name = new type[size]
+ shortcut version: 
  type[] name = new type[size]
  (more "versions" show up later)
-------------------------------------------------------------------------------
Important notes/features:
+ [] is operator and has highest precedence
+ can declare arrays in same statement but be careful:
  ex) int a[], b; -> b is NOT an array
+ why $new$? arrays are objects
+ size is number of elements
+ labeling of indicies starts at zero!
+ if you attempt to access index that does not exist, Java complains
+ can find length automatically with name.length
-------------------------------------------------------------------------------
How to think of objects?
+ arrays "live" in $type[]$ class
+ draw a box to represent the array
+ draw boxes to represent individual elements
+ elements get initial default values ("zeros")
+ each element resembles a reference variable
  (helps when dealing with arrays of objects)
+ arrays have built-in $length$ "instance variable"
-------------------------------------------------------------------------------
$for$ loop:
+ for(initialization; test; increment) 
     statements
+ initialization, test, and increment can multiple expressions separated
  by commas or be left blank
+ usually use for determinate loops 
  - think pure repetition or known stopping conditions
  - otherwise you need to use $break$ (which is usually bad)
+ variables declared inside initialization are LOCAL to the scope of the 
  $for$ loop
+ converting $while$ to $for$:

  int i = 1;
  while ( i < 4) {              becomes     for (int i = 1 ; i < 4 ; i++ )  
     System.out.println(i);		        System.out.println(i); 
     i++;
  }
-------------------------------------------------------------------------------