CS100J, Spring 2001
Thurs 1/25
Lecture 2
-------------------------------------------------------------------------------
Announcements:
+ get handout (overview, in basement lab of Carpenter)
+ follow instructions on overview ASAP
+ Project 1 (P1) due Tues 1/30
+ CS100J sections start this week
+ don't go to Section 5 (use 6 or another instead)! (temporarily cancelled)
+ reading: Savitch Chaps 1-2 this week (see Syllabus for details)
+ software demos -- Thurs 7:30-9:30 Olin 255, Fri 4:30-6:30 Upson B17
-------------------------------------------------------------------------------
Hints on P1:
+ use help command
+ for deflections, separate values with commas
+ see help plot
-------------------------------------------------------------------------------
Topics:
+ computer languages
+ Java language concepts
+ character set: UNICODE, ASCII text
+ variety of tokens
+ statements
+ variables and assignments
-------------------------------------------------------------------------------
Summary from lecture 1:
+ introduction to DIS
+ introduction to CS100
+ course policies and procedures
+ definition of programming
+ problem solving with algorithms
+ computer language as words/tokens and sentences/statements
-------------------------------------------------------------------------------
Overview today:
+ the Java language (tokens and statements)
+ elements of Java
+ examples of some elements:
  comments, white space, punctuation, identifiers, operators,
  literals, strings, reserved words, escape characters
+ examples of statements:
  declaration, assignment
-------------------------------------------------------------------------------
Language Elements:
+ computer language usually written (in the future, spoken)
+ has "grammar" (syntax) and "meaning" (semantics)
+ use character set to build tokens
  - usually keyboard characters (ASCII text)
  - ASCII has 128 characters 
  - some are nonprinting -- see ascii.txt for full list
  - also, try entering $char(0:127)$ in MATLAB (do not enter $s: I use
    the $ notation to indicate a code fragment or statement to enter) 
  - technically, Java uses UNICODE
  - over 65000 characters
  - see p88, Appendix 3, website: Material->Java->Miscellaneous->UNICODE
  - special non-printing characters can be reproduced with ESCAPE CHARACTERS
    o see pp86-88
    o ex) \n (newline)
+ characters build tokens: comments, white space, punctuation,
  identifiers, operators, literals, strings, reserved words, escape characters
+ tokens form statements
===============================================================================
Tokens:
-------------------------------------------------------------------------------
+ White Space:                                                     
  - blanks, tabs                                                   
    ignored by Java compiler                                       
    o so, use as many as you want                                  
    o how much you use usually dictated by style                   
  - do NOT separate another token, thoguh                          
    o Java will treat as different tokens!                         
    o ex) i_am_a_token (the undersore _ is not a blank!)           
          this is a collection of tokens (a blank space is a blank)
-------------------------------------------------------------------------------
+ Comments (pp99-100):
  - inert
  - may place anywhere in code
  - compiler ignores
  - single line:

    // I am a comment
    /* I am a comment */

  - multiline:

    /* blah blah
       blah blah
       blah blah
     */

    /*  
     *  This version is more aestically pleasing
     */

  - nesting (comment within a comment)
    o cannot have /* /* stuff */ */
    o can nest // comments inside /* */

  - lots of style issues to address
    o generally, you must comment your code
    o for now, copy style the instructors/text uses
-------------------------------------------------------------------------------
+ Identifiers (p29, chap 2):
  - names for things in code
  - store values, act as commands you activate
  - begin with letter or underscore (_)
  - may also begin with currency symbols ($ and others) but NOT recommended
  - may not begin with a number
  - use as many numbers,letters,and _ inside an indentifier as you wish
  - CASE SENSITIVE!!!
  - ex)
	I_am_an_identifier
	I am five differnent identifiers
-------------------------------------------------------------------------------
+ Reserved words (sometimes called keywords):
  - part of the language
  - not available for use as identifiers (where users redefine)
  - ex) public, class, void
-------------------------------------------------------------------------------
+ Primitive data:
  - boolean, char, byte, short, int, long, float, double
  - the values are also called CONSTANTS or LITERALS
  - one "solid" value (not composite)
  - ex) true --> boolean, also a reserved word (same with false)
        'a'  --> char   (character -> single ASCII character)
        123  --> int    (integer   -> whole number)
        1.23 --> double (decimal number)
  - we'll use boolean, char, int, double and skip the rest
-------------------------------------------------------------------------------
+ Strings
  - collections of characters
  - different kind of token: sometimes referred to as string literal
  - ex) "I am a string"
  - looks like a constant and seems to be a value
  - actually, really an object, but we're not dealing with objects yet
-------------------------------------------------------------------------------
+ Operators: (Appendix 2 for full list) 
  - +-=,*% (and more)
-------------------------------------------------------------------------------
+ Punctuation:
  - ; () {}
  - separate tokens, statements
-------------------------------------------------------------------------------
+ example of tokens at work:
  System.out.println(1+19);
  - tells Java to print out value of 1+19
  - name(stuff) is a METHOD CALL (also invocation)
  - first, what's a statement? (we'll deal with methods later)
===============================================================================
Statements:
+ combine tokens to create "sentences"
+ each "sentence" forms an instruction/command
+ "sentences" are called statements
+ end statements with semicolon
+ going to a new line does NOT mean you have a new statement!
+ Java executes each statement top-down, left-to-right
  (within a statement, operator precedence determines direction of execution)
+ see Java in Nutshell pp43-44 for full list
-------------------------------------------------------------------------------
+ Empty statement
  ex) ; 
  - doesn't actually do anything
-------------------------------------------------------------------------------
+ Expression:
  - combine constants with operators
  - precedence: some operators come before others
  - associativity: operators "work" left to right or right to left
  ex) 1 + 1;
      2 * 2 - 4; // technically means (2*2) - 4 because of precedence
      2 - 1 - 3; // technically means (2-1) - 3 due to associativity
  - use () to force some expressions to evaluate first
  ex) (1 + 1); // () not needed
      2 * (2 - 4); 
      2 - (1 - 3);
  - beware of mixing types!
    ex) System.out.println(1/2) yields 0, not 0.5
  - mixing doubles and ints makes doubles
    ex) System.out.println(1.0/2) yields 0.5
  - use CAST to force different value
  - syntax: (type) expression
  - ex) System.out.println( (int) 9.8) yields 9 (an integer)
-------------------------------------------------------------------------------
+ Declaration:
  - want identifiers to represent other values
  - variables (identifiers) can hold values
  - values can be primitive types 
  - can also be references, but that deals with objects, so we'll skip this
    for now
  - Java is strongly typed, so must tell Java about variables before using
  - syntax: variabletype var ;
  - ex) char c;
	int value;
-------------------------------------------------------------------------------
+ Assignment:
  - special kind of expression statement
  - store value in a variable that's been declared
  - cannot use variable until it's declared!
  - syntax: declaredvar = value
  - the value's type MUST match the variable's type
  - ex) int x;
        x = 1;
  - now, x has the value 1
  - think x <- 1 because assign operator associates right-to-left
  - can say in English "x gets 1"
  - later in code, Java "knows" x means 1
  - cannot redeclare variable unless "elsewhere" in code
    (too hard to explain for now -- so, just don't redeclare!)
  - shortcut:
    o can combine syntax for shortcut:
    o syntax: type var = value
    o ex) int y = 7;
    o called INITIALIZING a variable (declaring and assigning initial value)
-------------------------------------------------------------------------------
+ Style issues:
  - usually one statement per line
  - leave space between operators and operands (values operated upon)
  - use descriptive variable names
  - avoid hard coding values (using specific values that)
    o use named constants
    o variables initialized "early" in code and used instead of the constants
-------------------------------------------------------------------------------