Lecture 17, CS 302-7 and 8, February 29

 

  1. Reminders
    1. Exam 1 – March 8
    2. Program 2 – Coming soon
  2. Review
    1. The call stack
    2. Void methods
  3. Stepwise Refinement
    1. Think of your program as a sort of flowchart – you want to think of each possible case, and what happens after it
    2. http://xkcd.com/627/

                                                               i.      Squares and diamonds can be thought of as methods, not just statements and conditions

    1. The idea is to break a complicated problem down into manageable cases, and solve those
    2. Think of these small cases as methods/black boxes – abstract out the details.
    3. Example – LeapDay.java

                                                               i.      Via stepwise refinement – think about each possible case

  1. Variable scope
    1. Definition – the part of the program in which you can access it
    2. Based on where you declare it (int a)
    3. Within the block you declare it in

                                                               i.      Local variable – variable that is defined within a method

                                                             ii.      As opposed to static variables – outside of methods.  Later…

    1. Scopes must not overlap

                                                               i.      Think of your program as a tree of blocks…

    1. Can use same name as long as scope doesn’t overlap
    2. Example that doesn’t work: int I in a for loop within a method that already has defined int I
    3. But, int I within separate for loops works.
  1. The call stack
    1. What actually happens when we call a method…

                                                               i.      main method that calls mystery

                                                             ii.      Vocab: Caller and callee

    1. Definition: Activation record.  This stores information about the method, including all local variables (including params).  Think of it as a box.
    2. So…when we call a method…suspend main, do mystery, when mystery is completed, return to main

                                                               i.      Generally: suspend caller, do callee, return to caller

    1. Think of mystery as another box [AR] that we put on top of main.  We only do the top most box at any given time, and then we drop one etc.
  1. More technical details:
    1. Call-execute-return sequence

                                                               i.      Start up called method

1.      allocate an activation record for the called method

2.      allocate space in the new AR for param vars

3.      copy param vals into corresponding param vars

a.       pass by value

                                                             ii.      execute method

                                                            iii.      return to caller

1.      remove called method’s AR (free up memory)

2.      replace method call with its return value (if not void)

                                                           iv.      continue in caller…

  1. Passing arrays
    1. General point – we can do it J

                                                               i.      Example – Averages.java

  1. HW – read and think about 6.5