CS100J, Spring 2001
Thurs 2/7
Lecture 6
-------------------------------------------------------------------------------
Announcements:
+ P2 due now
+ T1 (prelim 1) Tues 2/13
+ new section: Tues (today) 12:20-1:10 Philips 407
+ Groups sessions: more problem solving; provide more details on lecture;
                   practice quizzes; go over solutions together; limit < 20
		   people
+ main review session: 3-5 pm Upson B17
+ unclaimed P1 retrievable on 5pm Fri
-------------------------------------------------------------------------------
Topics:
+ practice with loops
+ control flow reminder
+ removing redundancy with methods (think functions)
-------------------------------------------------------------------------------
Summary from Lecture 5:
+ repetition statements:
  indefinite and definite loops
+ patterns in processing input
-------------------------------------------------------------------------------
Loop Example from past prelim
-------------------------------------------------------------------------------
Redundancy and clarity:
+ Statements: expression, assignment, selection, repetition
+ very powerful -> can solve many, many problems
+ problem:     spaghetti code! (thing FORTRAN)
+ solution:    pull repeated tasks into their own "chunks"
+ abstraction: chunkification, move to more abstract levels, hide "the dirt"
-------------------------------------------------------------------------------
Method invocation as a method:
+ think function
  enter $name(inputs)$
  generates output

+ without programming:
    ____
  \/ 4    => 2

  sqrt(4) => 2

  4 is the input/argument/parameter
  2 is the output/returned value

  the action of performing the function/method: INVOCATION/CALLING/

  NOTE: the returned value REPLACES the invoked function
        how? what about sqrt(4) + 3 ? 
        you would add 2 + 3 -> 5
        so, the 2 replaced sqrt(4)
-------------------------------------------------------------------------------
Examples in Java:
  $Math.random()$  
  $System.out.println(_stuff_);$
  $System.out.print(_stuff_);$
  $main(String[] args)$

+ interesting features:
  - not all have inputs
  - some don't actually give a value
    (just perform an action)

+ 2 general kinds:
  - performs action without returning a value
  - returns value (and maybe also performs action)
-------------------------------------------------------------------------------
How to use:
+ perform action:
  System.out.println("Hello");
  
  note: common source of confusion: output is NOT a returned value
  proof: try $int x = System.out.print("Hello");$

+ return a value:
  $int x = Math.random()*10;$
  
  Java would compute the rhs and then store the result in $x$

+ order of actions for operations that use methods (Control Flow of Methods)
  - Java invokes methods
  - replaces method call with the returned value
-------------------------------------------------------------------------------
How to design:
+ for now, write methods in the Main Class
+ need to invent name
+ need to describe the inputs to the method
+ need to write tasks for BODY of method (what the method does)
+ need to decide if there's a value to return

+ syntax:
 $public static _returntype_ name(_arguments_)$ {
      statements;
 }

+ Java is strongly typed, so arguments 
 _arguments_ composed of series of inputs with form:
      $_type_ _var1_, _type_ _var2_, ..., _type_ _varn_$
 
+ example:

  public static void myPrint(String s) {
      System.out.println(s);
  }

+ usage:
 
  public class L6 { 
     public static void myPrint(String s) {
        System.out.println(s);
     } 

     public static void main(String[] args) {
        myPrint("The value: " + (1+1) );
     }
  }     

+ control flow:
  - Java starts at $main$ in Main Class (assuming you set Main Class target)
  - Java works top-down, statement-to-statement
  - the myPrint call:
    o do expressions in methods (follow precedence, associativity)
    o do method call
    o find myPrint
    o check if type of input matches expected type
    o perform actions in invoked method
  - return control back to the point when/where method was invoked
------------------------------------------------------------------------------
Coming soon:
- actual and formal parameters
- call by value
- scope