Download presentation
Presentation is loading. Please wait.
Published byDarrell Hutchinson Modified over 9 years ago
1
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
2
Teach OO first Two aspects to programming: Structural/organizational 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 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 Based on pedagogical principals, I teach structural/organizational aspect first: OO Up to this point, the only statements they know are method call, assignment, and return.
3
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
4
Teaching OO to Beginners: Principals Reveal the programming process Teach skills, and not just knowledge Michael Caspersen discusses these in his PhD 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.
5
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 FirstClass { public static void main(String[] pars) { System.out.println("Hello World); } Almost every line of a Java program deals with a class or object!
6
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. 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.” x= x+2; Read as “x becomes x+1”, not “x equals x+2”.
7
Teach OO first. Have first assignment require mastery —grade/resubmit 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.
8
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.
9
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 OK. You did not put a com- 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. You said you checked the javadoc 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.
10
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. setFather(Rino dad) | set this rhino's father to dad. Precondition: this rhino's father is null and dad is not null. Your spec on setFather: /** Make dad be this Rhino’s father */ doesn’t mention the precondition. So a user might do a call d.setFather(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. 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 setFather with argument null for parameter dad.
11
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 isOlder(Rhino r) { return r != null && (year < r.year || (year == r.year && month < r.month)); } 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 isOlder does not implement isOlder’s specification. Please add the appropriate test case, figure out the error, fix it, and resubmit. /** = “r is not null and this rhino is older than r” */ public boolean isOlder(Rhino r) { return year < r.year || (year == r.year && month < r.month); } assertEquals(false, r2.isOlder(r2); assertEquals(true, r2.isOlder(r3)); assertEquals(false, r2.isOlder(r3)); Handout says: if an argument can be null, have a test case for it.
12
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 setFather(Rhino dad) { father= dad; } Look at what the handout said about setFather! 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 setFather? Read your class invariant, read what the handout says, revise, resubmit. From handout: In writing setFather, 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. dad.children= dad.children + 1;
13
Using an IDE that eliminates the need for method main Two candidates: DrJava and BlueJ My students don’t find out about method main until week 11, when I introduce applications and applets
14
Model for objects and classes A class is a file drawer. All the manila folders in it have the same kind of information Manilla folder: an object or instance of the class 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.
15
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. x 5 int y 7 int x= y; 7 7 p C2 Patient q C1 Patient p= q; C1
16
Model for objects and classes Manilla folder: an object or instance of the class 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.
17
Using an IDE that eliminates the need for method main Two candidates: DrJava and BlueJ sv m() {... } SC’s file drawer Inside-out rule To determine the declaration to which a variable name refers, look in the current construct, then the surrounding one, then the surrounding one, etc., until it is found. Similar rule for method calls Inside-out rule is used, with minor differences, in most languages, including predicate logic
18
Executing method calls Contains name of object or name of file drawer where method resides. method name: instr cntr scope box parameters, local variables template for frame for a call Force students to learn to execute a method call by hand Will give them a concrete understanding that they can’t get otherwise. Later, it will be easy to see that recursion actually works. When you first introduce the topic, don’t you do the execution. Show the students the template for a frame for a call, ask them to get out a piece of paper, and have them do it, step by step, in groups of two.
19
Executing method calls public class C { private int x; public void m(int y) { s1: if (y != 0) { s2: int z= y+1; s3: x= z; } method name: instr cntr scope box parameters, local variables template for frame for a call a5 x 5 m(...) C v a5 v.m(2+3); 2. Store arg values in pars 1. Draw frame for call 3. Execute method body. Look in frame for call for names; if not there, use scope box 4. Erase the frame m: s1a5 y z 5 s2 6 s3 6 Question: When is local variable z created?
20
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/cs1110/2008fa/ You can get slides of lectures and labs, assignments, etc.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.