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

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

1 CS100J. Lecture 2, 25 January 2007 Today’s topic: Objects and classes Reading for this lecture: Section 1.3. It’s most important that you study this.
CS100J Today’s topic: Objects and classes Reading for this lecture: Section 1.3 PLive: Activities 3-3.1, 3-3.2, (not 3-3.3), 3-4.1, Summary.
ITEC200 – Week03 Inheritance and Class Hierarchies.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
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.
CS100J 11 September 2003 Course Management System for CS100J is now populated with students who were pre-registered. Look at course web page to see how.
1 What’s a class People are confused with nested classes and anonymous classes. Experiments in a recitation last week revealed that the problems were probably.
CS100J CS100M is in Kimball Bll. Course Management System for CS100J is now populated with students who were pre-registered. Look at course web page to.
CSC 111 Course orientation
1 CS100J 30 January 2007 The class definition Course Management System (CMS) for CS100J is populated with students who were pre-registered. Look at course.
1 CS100J 05 February 2005 Today’s topic: Customizing a class (continued) Quiz 1 is today Quiz 2 is next Tuesday Quote for the day: There is no reason anyone.
Introduction to Methods
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
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,
Programming Languages and Paradigms Object-Oriented Programming.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Imperative Programming
1 Lecture 07 Interfaces and related issues Reading for these lectures: Weiss, Section 4.4 (The interface), p ProgramLive, Section 12.1 In User Interface.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Quote for the day: Computational thinking: a fundamental skill for everyone … [It] is … choosing an appropriate representation for a problem or modeling.
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.
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.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CS100J Spring 2006 CS100J: 11 weeks of programming using Java and 2 weeks using Matlab. David Gries is teaching CS100J. Graeme Bailey is teaching a special.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Principled approach to teaching OO first David Gries Computer Science, Cornell 1.
 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 CS100J 08 September 2005 Today’s topic: Customizing a class (continued) Quote for the day: There is no reason anyone would want a computer in their home.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 CS1110. Lecture 2, 28 Jan Objects & classes PLive: Activities 3-3.1, 3-3.2, (not 3-3.3), 3-4.1, Summary of lectures: On course page,
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
OOP Basics Classes & Methods (c) IDMS/SQL News
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.
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.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
1 CS1110 Classes, wrapper classes, Vectors. 10 Feb 2012 Miscellaneous points about classes. Discussion of wrapper classes and class Vector Use the text.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
CS Feb 2010 Customizing a class
Lecture 10 Review of classes
Java Primer 1: Types, Classes and Operators
CS1110 Classes, stepwise refinement 23 Sep 2009
CS100J. Lecture 2, 24 September Objects & classes
Programming Language Concepts (CIS 635)
CS Programming I Jim Williams, PhD.
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors
CS 1110 Please do this now. Draw three variables on a piece of paper as shown to the right: b 4 c 7 d false Below is a “program” consisting of 3 assignment.
CS100J 06 September 2005 The class definition
CS/ENGRD 2110 Fall 2018 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Review of Previous Lesson
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CS Feb 2011 Customizing a class
Presentation transcript:

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

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. Caspersen, M.E. Educating Novices in the Skills of Programming. PhD Dissertation, Computer Science, Aarhus, Denmark, June 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.

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

3 Principle 3. Present concepts at the appropriate level of abstraction. Kim Bruce. Using abstractions to make concepts concrete. SIGCSE Education Award lecture, Peter Denning et al. Computing as a discipline. CACM Three major paradigms, or cultural styles, used in CS: theory abstraction (modeling) design

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

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

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

7 Algol 60 language definition —CACM, Jan "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: 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 "

8 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!!

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

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

11 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 ; and (d) the assignment statement. (2)Does not mention the computer. (3)Is in terms of a concept with which students are already familiar.

12 A class is a file drawer. It contains manila folders, all having the same kind of information C1 Patient name B. Clinton address New York owes $ 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.

13 A class is a file drawer. Its manila folder contain the same fields and instructions (methods) Instructions in folder for people to carry out: methods Function: returns a value Procedure: 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 Patient name B. Clinton address New York owes $ getName() { … } Deposit(double) { … }

14 C1 Patient name B. Clinton address New York owes $ getName() { … } Deposit(double) { … } pat C1 variable contains the name of the folder pat.getName() function call. Its value is “B. Clinton” pat.deposit(250.0); procedure call. Subtract from field owes.

15 C2 Patient name ? address ? owes 0.0 C2 getName() { … } Deposit(double) { … } m ? variable contains the name of the folder 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

16 j a0 variable contains the name of the folder 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

17 j a0 variable contains the name of the folder a0 show() hide() setTitle(String) getTitle() getHeight() getWidth() setSize(int, int) getX() getY() setLocation(int, int) isResizable() setResizable(boolean) javax.swing.JFrame 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.

18 j 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() … SquareJFrame k a1

19 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

20 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

21 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

22 David and Paul Gries. A Multimedia Introduction to Programming Using Java. Springer Verlag, NY 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. You can get slides of lectures and labs, assignments, etc.