Principled approach to teaching OO first
David GriesComputer Science, Cornell
1
Principled approach to teaching OO firstPrincipled approach to teaching OO first
Teach programming, not programs
Teach skills, not just knowledge
2
• Stepwise refinement (stepwise improvement)top-down programming• Clear, precise specification before writing a routine
• Enforce JUnit testing
• Class invariants, loop invariants, and their use in the programming process: correctness concerns done informally
Tony HoareKlaus Wirth Edsger W DijkstraDavid
Gries
3. Present concepts at the appropriate level of abstraction
3
The computer itself is not the right level of abstraction for beginners. Give them a model they can understand without mentioning the computer and its memory.
The computer must always know the type of value to be stored in the memory location associated with a variable.
An object reference variable actually stores the address where the object is stored in memory.
An object has its own unique identity, which distinguishes it from all other objects in the computer’s memory …. An object’s identity is handled behind the scenes by the Java virtual machine and should not be confused with the variables that might refer to that object.(1) gives impression that only computers can execute programs.(2) confuses people who have little idea of memory, virtual machines, and how computers work.
3. Present concepts at the appropriate level of abstraction
4
Algol 60 language definition does not mention the computer.
“The purpose of the algorithmic language is to describe computational processes. …
A variable is a designation given to a single value.
Assignment statements serve for assigning the value of an expression to a variable …. The process will … be understood to take place in three steps as follows:
4.2.3.1. Any subscript expressions occurring in the left part variable are evaluated in sequence from left to right.
4.2.3.2. The expression of the statement is evaluated.
4.2.3.3. The value of the expression is assigned to the left part variable, with any subscript expressions having values as evaluated in step 4.2.3.1.
3. Present concepts at the appropriate level of abstraction
5
Problem: students don’t know how to execute the assignment statement.
Variable: 1. A name associated with a value.2. A named box with a value inside it.
x 5
x= x+2;
To execute the assignment:(1) evaluate the expression and(2) store its value in the variable.
The computer itself is not the right level of abstraction for beginners. Give them a model that they can understand without mentioning the computer and its memory.
To evaluate new C(...)(1) create an object of class C(2) execute constructor call C(...)(3) yield as the value of the expression the “name” of the new object
To execute procedure call p(...)(1) draw a frame for the call(2) assign arg values to pars(3) execute method body(4) erase the frame for the call
4. Order material to minimize the introduction of terms or topics without explanation
6
Two aspects to a program
Structural-organizational Algorithmic Expression
AssignmentConditional statementLoopSubroutine call
Subroutine (method)ObjectClassInterface
When using Java, this principle almost requires teaching OO first
Requires an IDE that removes the need for a method main or an applet
DrJava BlueJ
I don’t mention method main or applets until week 11, and then I can do both on the same day.
5. Use unambiguous, clear, precise terminology
7
Formal parameter — actual parameter
Pointer-reference
A parameter is a variable that is declared within the parentheses of a method header.
An argument is an expression that occurs within the parentheses of a method call.
y
x
an object
y= x;
a1
a2
a1
a1
Expression x evaluates to a1
6. Name the things you want to talk about
8
an object
a1
x a1
Use a standard format for drawing object. Force students to use it
9
x a1
an object
a1JFrame
w 4
get Title()setTitle(String)getWidth()show() ....
An object contains all the instance components (variables and methods) defined in the class and inherited by the
class—see later how to include inherited components in the
object
a6JFrame
w 7
get Title()setTitle(String)getWidth()show() ....
Objects of a subclass
10
a1JFrame
get Title()setTitle(String)getWidth()show() ....
a1
Demo
public class Demo {
}
extends JFrame
private int w;
w 4
/** = area of window */public int area() { return getWidth() * getHeight();}
area(){ return getWidth()*getHeight(); }
bottom-up rule:Search for a component
from the bottom up —gives the overriding
method
toString()
toString()
The inside-out rule for determining which component is referenced
11
a7JFrame
get Title()setTitle(String)getWidth()show() ....
Demo
w 7
area(){ return getWidth()*getHeight(); }
a1JFrame
get Title()setTitle(String)getWidth()show() ....
Demow 4
area(){ return getWidth()*getHeight(); }
Look in the current construct, its surrounding construct, its surrounding construct, etc., until a matching component is found.
file drawer for class Demo
s 6
m(){ ... }
static fields,methods
Contents of first lectures
12
01. Expressions, variables, assignmentLab: practice: types int, double, boolean, string;
casting; assignment 02. Objects (students see JFrame objects)
03. The class and subclass definitions (simple function/proc decs with return statements and method callsLab: write simple function/proc declarations in a
subclass of JFrame04. Fields, getter/setter methods, simple constructors
05. Static components, the class hierarchy, JUnit testingLab: use JUnit test cases to find and fix errors in a
given program; practice with static components
06. How a method call is executed. if- and if-else statements. local variables.
Two lectures per week, with a closed, mandatory lab between them
Contents of first lectures
13
07. Inside-out rule; super-this; stepwise refinementLab: practice: write functions (if- and if-else; no
loops!) 08. Constructors in subclasses; stepwise refinement
09. Wrapper classes; stepwise refinementLab: Learn about class Vector
10. Recursion
11. RecursionLab: Writing recursive functions
12. Casting among class-types; operator instanceof; function equals.
Two lectures per week, with a closed, mandatory lab between them
Assignments
14
1. Maintain rhinos. Storyline on endangered species. fields, setter/getter methods, boolean expressions, JUnit testing.
/** = true if r not null and this rhino is older than r; false otherwise */public boolean isOlder(Rhino r) { return r != null && (year < r.year || (year == r.year && month < r.month));
} 2. Maintain rhinos. extension of 1 to include a static variable(number of Rhinos created) and more comparison methods.
3. Write several functions using assignment, if, if-else. JUnit testingA. Translate between rgb, cmyk, hsv color modelsB. TimeZone: an instance maintains a time in one of 12 time zonesC. Maintain dates in various forms and translate between themD. Bowling: an instance maintains a bowling frame. The calculation of the score in the frame requires thinking.
Assignments
15
4. Turtle graphics. Draw shapes, bouncing balls.Loops: repetitive polygons, spirals, radiating linesRecursion: Koch snowflakes, Sierpinski triangles
5. Mozart dice game. Create a random waltz, building the mp3 file from mp3 files for the individual measures, and play it.
6. Manipulate jpg files —reflect, transpose, convert to gray scale, etc.
7. Hide a message in a jpg file.
A principled approach to teaching programming
16
• Using Java, OO comes first• Allows OO stuff to be used (and understood!!!) throughout the course
• Interesting, inspiring assignments generate enthusiasm
• Stress good programming practices —good specifications, class invariants, stepwise refinement, integrated testing using JUnit• First assignment requires mastery, everyone gets 100/100 Allows students to learn from their mistakes, without penalty
Cornell: I teach 130 to 160 students each semester.At least 50% have not programmed before.Mainly engineering, but from all other disciplines, too