Lecture 17, CS 302-6, October 12

  1. Reminders
    1. Exam 1 – October 20, 5-7 pm

                                                               i.      Room: Chemistry 1351

    1. Program 2 – October 28, 10 pm
  1. Review
    1. Void methods
    2. Stepwise Refinement
    3.  Example – LeapYear.java
  2. Variable scope
    1. Definition – the part of the program in which you can access it
    2. Based on where you declare it (where you do: int a)
    3. The scope is within the block you declare it in

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

                                                             ii.      As opposed to static variables – defined outside of methods.  We’ll get to this in a couple of weeks

    1. Variables scopes must not overlap
    2. Can use the same name for variables as long as their scope doesn’t overlap
    3. Example that doesn’t work: int I in a for loop within a method that already has defined int I
    4. But, int I within separate for loops works.
    5. Examples:

This works:

For(int I=1;I<10;i++)

{

            //Do something

}

for(int I=1;I<10;I++)

{

            //Do something else

}

 

This does not work:

For(int I=1;I<10;I++

{

            for(int I=1;I<10;I++)

{

//Do something

}

}

  1. The Call Stack and Call Stack Tracing
    1. What actually happens when we call a method…

                                                               i.      Consider a main method that calls some method mystery

    1. Activation record: stores information about the method, including all local variables (including params).  Think of it as a box with all of the method data in it.
    2. So, when we call ‘mystery’ from ‘main’, this is what happens:

                                                               i.      suspend main

                                                             ii.      do mystery

                                                            iii.      when mystery is completed, return to main

    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.  When we are done with it, we return to the box below, and cross it out.
    2. Example – CallStackTracing.pdf, CallStackTracing.java
  1. Call-execute-return sequence
    1. This is a step-by-step detail of what happens when we call a method
    2. Start up called method

                                                               i.      allocate an activation record for the called method

                                                             ii.      allocate space in the new AR for param vars

                                                            iii.      copy param vals into corresponding param vars

1.      pass by value

    1. execute method
    2. return to caller

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

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

    1. continue executing statements in the caller method
  1. Passing arrays as parameters
    1. We’ve already learned about how we can pass integers, doubles, Booleans, etc as arguments (parameters) to methods.
    2. We can also pass arrays of values as arguments
    3. Example – Averages.java
  2. HW – read and think about 6.5