teaching machines

CS1: Lecture 12 – Method Mechanics

September 30, 2019 by . Filed under cs1, fall 2019, lectures.

Dear students,

Just as writers write paragraphs, programmers write methods. Like a good paragraph, a method has a clear and narrow purpose. Just as paragraphs decompose a grand thesis into digestable points, methods decompose a large problem into manageable subproblems.

Since so much of our time will be spent writing methods, it is paramount that we understand how they work. Today we will try to illustrate their behavior in a few different ways.

As a warmup, let’s reflect on what this program does:

public class Negater {
  public static void main(String[] args) {
    int n = 10;
    negate(n);
    System.out.println(n);
  }

  public static void negate(int n) {
    n = -n;
  }
}

Drama

Writing a program is in a sense writing a screenplay. Let’s let an actor stand in for each method in this short program and see what unfolds. Where the cues indicate that a character receives or drops a number, we will use six-sided dice. Not every detail will be revealed to you; consider this exercise a Blackbox.

And now, for your viewing pleasure, please enjoy the UWEC original Three-To-One:

main: (to the audience) Hast thou a numeral? It must needs be from 1 unto 6, inclusive. (receives a number, drops it into x’s first bucket) Hereat may thy pate not gray.

main: (to the audience) Prithee, hast thou another? Render it unto me! (receives a number, drops it into x’s second bucket)

main: (to the audience) Wouldst thou thrice condescend to the realm of the baseborn and proffer a numeral of like fashion? (receives a number, drops it into x’s third bucket)

main: (steps back from buckets) Friend x, I summon thee! (freezes)

x: (approaches buckets, drops copy of the number in its first bucket into y’s first bucket, drops copy of its second bucket into y’s second bucket, and drops copy of its third bucket into y’s third bucket, steps back from y’s buckets) Friend y, I summon thee! (freezes)

y: (approaches buckets, examines three numbers, completes secret action, announces number, hands die to x) I have done. (leaves)

x: (drops number from y in fourth bucket) I dub thee D! (drops copy of number in its first bucket into z’s first bucket, drops copy of its second bucket into z’s second bucket, and drops copy of its third bucket into z’s third bucket, steps back from z’s buckets) Friend z, I summon thee! (freezes)

z: (approaches buckets, examines three numbers, completes secret action, announces number, hands die to x) I have done. (leaves)

x: (drops number from z into fifth bucket) I dub thee E! (completes secret action, announces number, hands die to main) I have done. (leaves)

main: (receives number from x) Percent-D. (reads number from x) Percent-N. I have done. (leaves)

We will not reveal the secret actions of the players right away. They are as follows:

Mechanics of Method Calling

Having an accurate mental model of how a program behaves is helpful. An accurate model helps us direct our attention to the right places when bugs happen, and we come to better understand exactly what a method is and how it is called. Let’s develop that model by tracing memory through the following exercises.

When a method is called, a chunk of RAM is set aside for it called a stack frame. The stack frame contains a slot for each parameter and each local variable declared in the method. A method may only access data within its frame. When the method returns, the stack frame is disposed of and control returns to the caller and its stack frame.

The Java Visualizer offers nice visualizations of an executing program’s stack frames.

We will also examine IntelliJ’s debugger as a way to inspect a running program.

TODO

Here’s your TODO list for next time:

See you next class!

Sincerely,

P.S. It’s time for a haiku!

I thought I’d write books
Along came the computer
Which let me write it

P.P.S. Here’s the code we wrote together in class…