Teaching Java —with OO first David Gries Computer Science Cornell University Ithaca, NY 14850 0 Principle 1. Reveal the programming process, in order to ease and promote the learning of programming. Principle 2. Teach skills, and not just knowledge, in order to promote the learning of programming. Michael Caspersen discusses these in his PhD thesis. Approach based on cognitive science, educational psychology, cognitive skill acquisition, research in programming methodology. Introduces stepwise improvement. Caspersen, M.E. Educating Novices in the Skills of Programming. PhD Dissertation, Computer Science, Aarhus, Denmark, June 2007. 1 Principle 1. Reveal the programming process, in order to ease and promote the learning of programming. Principle 2. Teach skills, and not just knowledge, in order to promote the learning of programming. Most texts teach programs, not programming. They focus largely on knowledge, not skill. CS people in general put little emphasis on the programming process. Every lecture should deal in some way with the skill of program development. 2 Principle 3. Present concepts at the appropriate level of abstraction. • Kim Bruce. Using abstractions to make concepts concrete. SIGCSE Education Award lecture, 2005. • Peter Denning et al. Computing as a discipline. CACM 1989. Three major paradigms, or cultural styles, used in CS: • theory • abstraction (modeling) • design 3 Principle 3. Present concepts at the appropriate level of abstraction. The computer is not the appropriate level of abstraction to describe variables, assignment, and other programming language concepts! "A variable is name for a memory location used to hold a value of some particular data type." "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." 4 Principle 3. Present concepts at the appropriate level of abstraction. The computer is not the appropriate level of abstraction to describe variables, assignment, and other programming language concepts! Computer-centric view: • Can create unnecessary and confusing detail —especially for a novice, who doesn't yet know about much about computers and computing. • Gives the impression that only the computer can execute a program. 5 Principle 3. Present concepts at the appropriate level of abstraction. The computer is not the appropriate level of abstraction to describe variables, assignment, and other programming language concepts! "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." 6 Algol 60 language definition —CACM, Jan 1963. "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 … variable …. The process will be understood to take place in three steps: 4.2.3.1. 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." 7 Principle 4. Order the material so as to minimize the introduction of terms or topics without explanation —as much as possible, define a term when you introduce it. When teaching Java, principle 3 mandates teaching OO first. /** Print "Hello World" */ public class FirstClass { public static void main(String[] pars) { System.out.println("Hello World); } Almost every line of a Java program } deals with a class or an object!! 8 Principle 5. Use unambiguous, clear, precise, terminology. Don't use pointer and reference. Novices do not know what they mean. Use inheritance properly. Java version: private components are not inherited. Better version: all components are inherited from the superclass, because they all appear in each object of the subclass. Use parameter – argument, not formal parameter – actual parameter. These sentences from texts completely confuse the issue: “The semantics of an assignment for primitive types and for objects is different.” “When an object is passed to a method, we are actually passing a reference to that object.” 9 Facilitating the teaching of OO first • An IDE that does not require a Java application —e.g. DrJava or BlueJ. • An appropriate notational machine —model of execution. • Constructive alignment: Course outcomes, teaching/learning activities, and assessment methods are all aligned. • Biweekly quizzes: Students are told exactly what will be on the quiz and are expected to get 100/100 on them. • Closed labs. • One-on-one sessions. 10 Notational machine Two aspects: • Structural/organizational —ØØ • Procedural —algorithms, conditionals, loops, arrays, etc. When teaching Java, principle 3 mandates teaching OO first. We provide a model for classes and objects that (1) Can be taught after the students know only about (a) types and expressions, (b) variables, (c) the declaration <type> <variable>; and (d) the assignment statement. (2) Does not mention the computer. (3) Is in terms of a concept with which students are already familiar. 11 A class is a file drawer. It contains manila folders, all having the same kind of information C1 Whoever creates the folder gets to decide on the name that goes on the tab. Must be unique! name B. Clinton address New York owes $250.00 Patient Names that go on tabs of manila folder form a new type. Importance cannot be overestimated. Allows us to eliminate terminology like pointers and references. manila folder: an object or instance of the class 12 A class is a file drawer. Its manila folder contain the same fields and instructions (methods) C1 Whoever creates the folder gets to decide on the name that goes on the tab. Must be unique! name B. Clinton address New York owes $250.00 Patient getName() { … } Deposit(double) { … } Instructions in folder for people to carry out: methods Function: returns a value Procedure: does something, does not return a value 13 pat C1 variable contains the name of the folder C1 name B. Clinton address New York owes $250.00 Patient getName() { … } Deposit(double) { … } pat.getName() function call. Its value is “B. Clinton” pat.deposit(250.0); procedure call. Subtract 250.0 from field owes. 14 C2 m ? C2 variable contains the name of the folder C2 new Patient() name ? address ? owes 0.0 Patient getName() { … } Deposit(double) { … } An expression: create a new folder (put it in file-drawer Patient) and give as the value of the expression the name of the folder. C2 m= new Patient(); A statement: evaluate new Patient() and store its value (the name of the new folder) in variable m. 15 j a0 variable contains the name of the folder a0 show() hide() setTitle(String) getHeight() setSize(int, int) getX() getY() isResizable() javax.swing.JFrame getTitle() getWidth() setLocation(int, int) setResizable(boolean) A manila folder (object) of class javax.swing.JFrame() Maintains a window on your monitor, with methods (instructions) for manipulating it 16 j a0 variable contains the name of the folder a0 show() hide() setTitle(String) getHeight() setSize(int, int) getX() getY() isResizable() javax.swing.JFrame getTitle() getWidth() setLocation(int, int) setResizable(boolean) SquareJFrame area() {…} setHeightToWidth() { …} Drawing a folder (object) for a subclass. Students learn how to draw such folders. It is important for later understanding that they know that the complete method resides in the folder. 17 j a1 a0 a1 a0 … k a1 JFrame SquareJFrame area() … … JFrame SquareJFrame area() … Assignment is seen to follow the normal rules for an assignment: To execute j= k; evaluate expression k (gives the value a1) and store its value in j. 18 Model helps, right from the beginning • Students understand what an object is, right from the beginning. The explanation is not in terms of a computer. • The pictorial nature of the model is important. The name on the tab is most important. Our students learn to draw objects for subclasses, and they use it later to learn various concepts (inheritance, overriding, the inside-out rule for determining what declaration a method call or variable refers to. • Pointer and reference are not used. UML diagrams are inadequate because they do not have the equivalent of the name on the tab of an object. Although it would be easy to extend UML diagrams for objects to include it. 19 i Inside-out rule: To determine to which variable declaration a variable reference refers, search in the current scope, the enclosing scope, its enclosing scope, etc. until it is found. (Similar rule for method declarations and calls.) a1 v 2 C 4 SC w p) { m(int int lv; while ( … ) { int n; … sv … n } } sv Inside-out rule used in just about every programming language and in predicate logic: 5 m() { …} SC's file drawer 20 Closed labs, after lectures 1, 3, 5, …, give practice with concepts. Lecture 1. Types, expressions, variables, assignments Lecture 2. Objects Lecture 3. Class def. Subclass of JFrame with 2 methods Lecture 4. Fields, getter/setter methods. Junit testing Lecture 5. Class hierarchy. Static variables Lecture 6. Methods; method bodies; local variables; if-statement Lecture 7. Super-this; inside-out rule. Stepwise refinement Lecture 8. Overriding; constructors in subclasses Stepwise refinement Lecture 9 and 10. Recursion Lecture 11. Casting about; instanceof Lecture 12. Loops 21 David and Paul Gries. A Multimedia Introduction to Programming Using Java. Springer Verlag, NY 2005. Comes with a CS that has 250-odd 2-5 minute lectures with synched animation. Here, we can really concentrate on program development. Webpage for last Spring's course. http://www.cs.cornell.edu/courses/cs100j/2007sp/ You can get slides of lectures and labs, assignments, etc. 22