teaching machines

CS1: Lecture 12 – Method Mechanics

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:

• y
y chooses the minimum number.
• z
z chooses the maximum number.
• x
x computes a + b + c - minimum - maximum.

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.

public class Padding {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

String token = in.next();
int length = in.nextInt();
char filler = in.next().charAt(0);

}

public static String pad(String word, int length, char filler) {
int difference = Math.max(0, length - word.length());
String fillerAsString = "" + filler;
String padding = fillerAsString.repeat(difference);
}
}

• Cylinder
public class Cylinder {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double radius = in.nextDouble();
double height = in.nextDouble();
double volume = cylinderVolume(radius, height);
System.out.println(volume);
}

public static double cylinderVolue(double radius, double height) {
double area = circleArea(radius);
double volume = height * area;
return volume;
}

public static double circleArea(radius) {
double area = Math.PI * radius * radius;
return area;
}
}

• Growth
public class Growth {
public static void main(String[] args) {
int x = 10;
x = dubs(x);
x = dubs(x);
x = trips(x);
System.out.println(x);
}

public static int dubs(int y) {
return y + y;
}

public static int trips(int y) {
int x = y + y + y;
return x;
}
}

• Negater
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;
}
}

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:

• CS 145, lab 4 is posted. You are invited to start early. Recall that lab 3 must be done and done correctly before you arrive to lab 4.
• In lab this week, we will conduct peer reviews for homework 1. These are discussions about your code that give qualitative feedback that the SpecChecker can’t give. You must attend one of these to claim your homework credit. Only those that completed the homework may attend.

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…