Principled approach to teaching OO first David Gries Computer Science, Cornell 1.

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Games and Simulations O-O Programming in Java The Walker School
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
0 CS100J September 2007 CS100J: 12 weeks programming using Java, 2 using Matlab. David Gries. CS100M: 7 weeks of Matlab and 7 of Java. Daisy Fan. CS100H:
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
Review CSC 171 FALL 2004 LECTURE 21. Topics Objects and Classes Fundamental Types Graphics and Applets Decisions Iteration Designing Classes Testing and.
Chapter 10 Classes Continued
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
1 CS1110 Fall 2011: David Gries & Steve Marschner CS1112: Matlab No prior programming experience One semester of calculus Math- & engineering- type problems.
Quote for the day: Computational thinking: a fundamental skill for everyone … [It] is … choosing an appropriate representation for a problem or modeling.
Teaching OO to Beginners David Gries Computer Science, Cornell Past 3-4 years: Teach computing using Java students each semester Mostly Engineering,
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
1 CS1110 Fall 2010 Instructors: David Gries & Lillian Lee CS1112: Matlab No prior programming experience One semester of calculus Math- & engineering-
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
0 Teaching Java —with OO first David Gries Computer Science Cornell University Ithaca, NY
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
1 CS1100 Fall Instructor: David Gries CS100M: Matlab No prior programming experience One semester of calculus Math & engineering type problems CS100J:
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Arithmetic, Class Variables and Class Methods Week 11
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 CS1110 Classes, stepwise refinement 17 Feb 2009 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
1 CS September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1 CS1110 Thursday, 10 Feb 2011 Discussion of Methods: Executing method calls. If-statements. The return statement in a function. Local variables. For this.
1 CS100J Spring Instructor: David Gries CS100M: Matlab No prior programming experience One semester of calculus Math & engineering type problems.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
1 CS Sept 2010 Customizing a class Quote for the day: I have traveled the length and breadth of this country and talked with the best people, and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
1. Understanding the execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
Objects Real and Java COMP T1 3
CS Feb 2010 Customizing a class
The need for Programming Languages
CS1110 Spring Instructor: David Gries
Java Primer 1: Types, Classes and Operators
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Fall2017 Lecture 4: The class hierarchy; static components
Sit next to someone. Today, we do some work in pairs.
CS/ENGRD 2110 Fall 2018 Lecture 4: The class hierarchy; static components
CS/ENGRD 2110 Fall 2018 Lecture 2: Objects and classes in Java
Sit next to someone. Today, we do some work in pairs.
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Corresponds with Chapter 5
CS Feb 2011 Customizing a class
Presentation transcript:

Principled approach to teaching OO first David Gries Computer Science, Cornell 1

Principled 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 Hoare Klaus Wirth Edsger W Dijkstra David 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: Any subscript expressions occurring in the left part variable are evaluated in sequence from left to right The expression of the statement is evaluated The value of the expression is assigned to the left part variable, with any subscript expressions having values as evaluated in step

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 Assignment Conditional statement Loop Subroutine call Subroutine (method) Object Class Interface 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 Expression x evaluates to a1

6. Name the things you want to talk about 8 an object a1 x

Use a standard format for drawing object. Force students to use it 9 x a1 an object a1 JFrame 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 a6 JFrame w 7 get Title() setTitle(String) getWidth() show()....

Objects of a subclass 10 a1 JFrame 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()

The inside-out rule for determining which component is referenced 11 a7 JFrame get Title() setTitle(String) getWidth() show().... Demo w 7 area() { return getWidth()*getHeight(); } a1 JFrame get Title() setTitle(String) getWidth() show().... Demo w 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 Expressions, variables, assignment Lab: 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 calls Lab: write simple function/proc declarations in a subclass of JFrame 04. Fields, getter/setter methods, simple constructors 05. Static components, the class hierarchy, JUnit testing Lab: 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 Inside-out rule; super-this; stepwise refinement Lab: practice: write functions (if- and if-else; no loops!) 08. Constructors in subclasses; stepwise refinement 09. Wrapper classes; stepwise refinement Lab: Learn about class Vector 10. Recursion 11. Recursion Lab: Writing recursive functions 12. Casting among class-types; operator instanceof; function equals. Two lectures per week, with a closed, mandatory lab between them

Assignments 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 testing A. Translate between rgb, cmyk, hsv color models B. TimeZone: an instance maintains a time in one of 12 time zones C. Maintain dates in various forms and translate between them D. Bowling: an instance maintains a bowling frame. The calculation of the score in the frame requires thinking.

Assignments Turtle graphics. Draw shapes, bouncing balls. Loops: repetitive polygons, spirals, radiating lines Recursion: 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