CS100J, Spring 2001
Tues 2/13
Lecture 7
-------------------------------------------------------------------------------
Announcements:
+ T1 (prelim 1) Tues 2/13
+ P3 posted tonight/early tomorrow
+ Groups sessions: more problem solving; provide more details on lecture;
                   practice quizzes; go over solutions together; limit < 20
		   people
-------------------------------------------------------------------------------
Topics:
+ removing redundancy with methods (think functions)
+ designing methods in main class
+ issues: returning value, not returning value, passing by value, scope
-------------------------------------------------------------------------------
Summary from Lecture 6
+ functions that return value
+ methods as functions
-------------------------------------------------------------------------------
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"
-------------------------------------------------------------------------------
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
------------------------------------------------------------------------------
Terminology:
+ Formal parameters:
  - variables written inside method header

+ Actual parameters:
  - anything passed into a method

+ Call by value:
  - value of actual parameter copied into formal parameter
  - how to avoid confusion of clashing variable names?

+ Scope:
  - the visibility of a element of code (variable, method)
------------------------------------------------------------------------------