Teaching OO to Beginners David Gries Computer Science
Teaching OO to Beginners David Gries Computer Science, Cornell Past 3 -4 years: • Teach computing using Java • 130 -200 students each semester • Mostly Engineering, but also Arts & Sciences, Human Ecol, Arch, Agr & Life Sciences • 1/2 of them have never programmed before • 2 50 -minute lectures per week • 1 50 -minute closed, required lab per week
Teach OO first Two aspects to Based on pedagogical programming: principals, I teach Structural/organizational structural/organizational aspect first: OO Procedural 1. Java expressions and assignment: int, double, boolean, String 2. Objects --method calls. 3. Class definition (a subclass), with a function decl. and a procedure decl. 4. Fields, constructors, getter/setters, JUnit testing 5. JUnit testing, class Object, static variables Up to this point, the only statements they know are method call, assignment, and return. 6. Methods: first look at if statements 7. super-this. Inside-out rule. Stepwise refinement 8. Constructors in subclasses. Stepwise refinement 9. Wrapper classes. Stepwise refinement 10. Recursion 11. Recursion 12. Casting, instanceof, function equals
Let students know where the course is heading We teach in basically a bottom-up style, introducing one new “feature” at a time. Bad for “global learners”, who need to see the big picture. Compensate by giving overviews, show where course is going
Teaching OO to Beginners: Principals • Reveal the programming process • Teach skills, and not just knowledge Michael Caspersen discusses these in his Ph. D thesis, done at Aarhus, June 2007. Approach based on cognitive science, educational psychology, cognitive skill acquisition, research in programming methodology. Introduces stepwise improvement • Present material at the right level of abstraction Need a good model of classes and objects. Need a good model of execution of method calls. Neither should be in terms of the computer and memory.
Teaching OO to Beginners: Principals • 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. /** Print "Hello World" */ public class First. Class { public static void main(String[] pars) { System. out. println("Hello World); } Almost every line of a } Java program deals with a class or object!
Teaching OO to Beginners: Principals • Use unambiguous, clear, precise terminology —and force the students to learn it and use it. • In the beginning, don’t use pointer or reference • Parameter: variable declared within parens of method header. Argument: expression within parens of method call. • Use the word inheritance properly. All components are inherited from the superclass, not just the public ones. • x= x+2; Read as “x becomes x+1”, not “x equals x+2”. • These sentences, from texts, completely confuse the issue: “Semantics of assignment for primitive types and for objects differ. ” “When an object is passed to a method, we are actually passing a reference to that object. ”
Teach OO first. Have first assignment require mastery —grade/resubmit until is is right Allow you to establish some important groundrules with code that is short and straightforward. No one gets penalized for misunderstanding.
Teach OO first. Have first assignment require mastery Allow you to establish some important groundrules with code that is short and straightforward. No one gets penalized for misunderstanding. Accompanying the field declarations should be comments that describe what each field means —what it contains. E. g. on decl. of the gender field, state that it is restricted to ‘M’ for male and ‘F’ for female. Collection of comments on field: the class invariant. private String name; private char gen; private int month; private int year; private int tag; private Rhino father; private Rhino mother; private int children; The handout says clearly that the fields must be annotated with comments giving their meaning and constraints on them. Please read the handout, put in the class invariant, and resubmit. Learning to follow directions is one of the goals of this assignment.
Teach OO first. Have first assignment require mastery Allow you to establish some important groundrules with code that is short and straightforward. No one gets penalized for misunderstanding. When you think all method specs are OK, click the javdoc button and study the web pages that are produced. Can you understand how to call each method based on the spec on the web page? If not, fix the spec. When done, put a comment at top of Rhino. java saying that you looked at the javadoc specs and they are You. OK. did not put a com. You said you checked the javadoc ment at the top of file Rhino. java indicating that you looked at the javadoc specs. Please look at the javadoc specs, fix anything that needs fixing, put the comment at the top of file Rhino. java, and resubmit. comments but it is clear that you did not, because there are none. Either you misunderstood —in which case, ask for help, fix, and resubmit— or you were dishonest. The latter won’t be tolerated from now on. We are working hard to give you a good course, please do your part. Fix and resubmit.
Teach OO first. Have first assignment require mastery Allow you to establish some important groundrules with code that is short and straightforward. No one gets penalized for misunderstanding. set. Father(Rino dad) | set this rhino's father to dad. Precondition: this rhino's father is null and dad is not null. Precede each method by appropriate spec, a Javadoc comment. The best way: copy and paste. Don’t test precondition; it’s caller’s responsibility to ensure that precondition is met. E. g. it is a mistake to call method set. Father with argument null for parameter dad. Your spec on set. Father: /** Make dad be this Rhino’s father */ doesn’t mention the precondition. So a user might do a call d. set. Father(null). Handout clearly says to include all preconditions. Why didn’t you copy and paste? Follow directions! Check and fix ALL your method specs and resubmit.
Teach OO first. Have first assignment require mastery Allow you to establish some important groundrules with code that is short and straightforward. No one gets penalized for misunderstanding. /** = “r is not null and this rhino is older than r” */ public boolean is. Older(Rhino r) { return year r != null < r. year && || (year == r. year && month < r. month); (year < r. year || (year == r. year && month < r. month)); } assert. Equals(false, r 2. is. Older(r 2); Handout says: if an argument assert. Equals(true, can be null, have a test case for r 2. is. Older(r 3)); it. assert. Equals(false, r 2. is. Older(r 3)); The handout says, very clearly, that if an argument can be null there should be a test case for it. You didn’t have a test case for it. Therefore you didn’t detect an error in your program: the body of is. Older does not implement is. Older’s specification. Please add the appropriate test case, figure out the error, fix it, and resubmit.
Teach OO first. Have first assignment require mastery Allow you to establish some important groundrules with code that is short and straightforward. No one gets penalized for misunderstanding. /** Set this rhino’s father to dad. Precondition: dad is not null and this rhino’s father is null. */ public void set. Father(Rhino dad) { father= dad; dad. children= dad. children + 1; } From handout: In writing set. Father, be careful! If d becomes this Rhino’s father, d has one more child, and its number-of-children field has to be updated accordingly. Look at what the handout said about set. Father! Your class invariant mentions a field that contains the no. of children of the rhino. This meaning must be maintained by each method, for all rhinos. How should you have tested set. Father? Read your class invariant, read what the handout says, revise, resubmit.
Using an IDE that eliminates the need for method main Two candidates: Dr. Java and Blue. J My students don’t find out about method main until week 11, when I introduce applications and applets
Model for objects and classes A class is a file drawer. All the manila folders in it have the same kind of information Whoever creates the folder gets to choose the name on the tab. Must be unique. The names on folders of class Patient form a type of value. Importance cannot be overestimated. Allows us to eliminate terms like pointer and reference and provide a single consistent view of assignment. Manilla folder: an object or instance of the class
Model for objects and classes A class is a file drawer. All the manila folders in it have the same kind of information x 5 y 7 int 7 x= y; p C 2 Patient q C 1 p= q; Whoever creates the folder gets to choose the name on the tab. Must be unique. Patient C 1 The names on folders of class Patient form a type of value. Importance cannot be overestimated. Allows us to eliminate terms like pointer and reference and provide a single consistent view of assignment.
Model for objects and classes First class definition: subclass of JFrame. Reasons: 1. Inheritance comes naturally, right in the beginning. 2. Never have to show something that can’t be explained. 3. See right away how things are reused. Bottom-up rule says to search for a component from bottom up. Gets overriding method, naturally. Manilla folder: an object or instance of the class
Using an IDE that eliminates the need for method main Two candidates: Dr. Java and Blue. J Inside-out To determinerule the declaration to which a variable name refers, look in the current construct, then the surrounding one, etc. , until it is found. Similar rule for method calls sv Inside-out rule is used, with minor differences, in most languages, including predicate logic m() {. . . } SC’s file drawer
Executing method calls Force students to learn to When you first introduce the execute a method call by topic, don’t you do the hand execution. Show the • Will give them a concrete students the template for a understanding that they frame for a call, ask them to can’t get otherwise. get out a piece of paper, and • Later, it will be easy to see that recursion actually works. have them do it, step by step, in groups of two. Contains name of object or name of file drawer where method resides. method name: instr scope box cntr parameters, local variables template for frame for a call
Executing method calls template for frame for a call method name: instr scope box cntr parameters, local variables public class C { private int x; public void m(int y) { s 1: if (y != 0) { s 2: int z= y+1; s 3: x= z; } } } a 5 x 5 C m(. . . ) 1. Draw frame for call 2. Store arg values in pars 3. Execute method body. Look in frame for call for names; if not there, use scope box v a 5 5 v. m(2+3); m: s 3 s 1 s 2 y a 5 z 6 4. Erase the frame Question: When is local variable z created? 6
How to find material on what I talked about David and Paul Gries. A Multimedia Introduction to Programming Using Java. Springer Verlag, NY 2005. Comes with a CD that has 250 -odd 2 -5 minute lectures with synched animation. On the CD, we can really concentrate on program development. Webpage for current course. www. cs. cornell. edu/courses/cs 1110/2008 fa/ You can get slides of lectures and labs, assignments, etc.
- Slides: 20