23
1 Teaching Java —with OO first David Gries Computer Science Cornell University Ithaca, NY 14850

0 Teaching Java —with OO first David Gries Computer Science Cornell University Ithaca, NY 14850

Embed Size (px)

Citation preview

1

Teaching Java —with OO first

David Gries

Computer Science Cornell University

Ithaca, NY 14850

2

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.

Caspersen, M.E. Educating Novices in the Skills of Programming. PhD Dissertation, Computer Science,

Aarhus, Denmark, June 2007.

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.

3

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.

4

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

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!

"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."

6

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.

7

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."

8

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."

9

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 3mandates 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!!

10

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.”

11

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.

12

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.

13

A class is a file drawer. It contains manila folders, all having the same kind of information

C1

Patientname B. Clinton

address New York

owes $250.00

manila folder: an object or instance of the class

Whoever creates the folder gets to decide on the name that goes on the tab. Must be unique!

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.

14

A class is a file drawer. Its manila folder contain the same fields and instructions (methods)

Instructions in folder for people to carry out: methodsFunction: returns a valueProcedure: does something, does not return a value

Whoever creates the folder gets to decide on the name that goes on the tab. Must be unique!C1

Patientname B. Clinton

address New York

owes $250.00

getName() { … }Deposit(double) { … }

15

C1

Patientname B. Clinton

address New York

owes $250.00

getName() { … }Deposit(double) { … }

pat C1

variable containsthe name of thefolder

pat.getName() function call. Its value is “B. Clinton”

pat.deposit(250.0); procedure call. Subtract 250.0 from field owes.

16

C2

Patientname ?

address ?

owes 0.0

C2

getName() { … }Deposit(double) { … }

m ?

variable containsthe name of thefolder

new Patient() 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.

m= new Patient(); A statement: evaluate new Patient() and store its value (the name of the new folder) in variable m.

C2

C2

17

j a0

variable containsthe name of thefolder

A manila folder (object) of class javax.swing.JFrame() Maintains a window on your monitor, with methods(instructions) for manipulating it

a0

show() hide()setTitle(String) getTitle()getHeight() getWidth()setSize(int, int)getX() getY() setLocation(int, int)isResizable() setResizable(boolean)

javax.swing.JFrame

18

j a0

variable containsthe name of thefolder

a0

show() hide()setTitle(String) getTitle()getHeight() getWidth()setSize(int, int)getX() getY() setLocation(int, int)isResizable() setResizable(boolean)

javax.swing.JFrame

SquareJFramearea() {…}

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.

19

j a0 a0

JFrame …

area() …

SquareJFrame

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.

a1

JFrame …

area() …

SquareJFramek a1

a1

20

• 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.

Model helps, right from the beginning

21

w

a1

C

SC

m(int p) { int lv; while ( … ) { int n; … sv … n }}

v 2

4

sv m() { …}5

SC's file drawer

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.)

Inside-out rule used in just about every programming language and in predicate logic:

i

22

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

23

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.