Introduction
to Interactive Programming
by Lynn Andrea
Stein
A Rethinking
CS101 Project
Interactive Programming In Java
Chapter Outlines
Massachusetts Institute of
Technology
Department of Electrical
Engineering and Computer Science
Cambridge, MA 02139 USA
ipij@ai.mit.edu
http://www.ai.mit.edu/projects/cs101/
Front Matter
- Table of Contents
- Preface
- Why Interactive Programming?
- Ramifications for Later Curriculum
- A Short History of the Rethinking CS101 Project
- Research Roots
- Classroom Experience
- How to Use This Book
- Part By Part
- Pedagogical Elements and Supplementary Materials
- About the Author
- Acknowledgements
- List of Figures
- List of Tables
- List of Sidebars
- Java Sidebars
- Style Sidebars
Part 1: Introduction To Interactive Program Design
Chapter Overview
Objectives of this Chapter
- Computers and Programs
- Thinking Like a Programmer
- Programming Primitives, Briefly
- Ongoing Computational Activity
- Coordinating a Computational Community
- What is the Desired Behavior of the Program?
- Who are the Members of the Community?
- What Goes Inside Each One?
- How Do They Interact?
- The Development Cycle
- The Interactive Control Loop
Chapter Summary
Exercises
Chapter 2: The Programming Process
Chapter Overview
Objectives of this Chapter
- The Problem
- Problem Requirements
- Assumptions
- Promises/Guarantees
- The Community Around You
- Program Libraries
- Users
- Physical Environment
- Understand their interfaces (and assumptions)
- Requirements are a Moving Target
- Software Lifecycle
- Documenting your Design
- Designing a solution
- Who are the members?
- How do they Interact
- What Promises?
- What Assumptions?
- Who does what?
- What goes inside?
- Nouns and Verbs
- Acting it out
- The process
- Keep it simple
- Keep it working
- You'll still have to debug (debugging is normal)
- When it's not working
- make state manifest
- Explain it to someone
- Act it out
- Documentation
Chapter Summary
Exercises
Overview
Objectives of this Interlude
- Introduction: Word Games
- Designing a Community
- A Uniform Community of Transformers
- The User and the System
- What Goes Inside
- Building a Transformer
- Transformer Examples
- Strings
- String Concatenation
- String Methods
- Rules and Methods
- Classes and Instances
- Fields and Customized Parts
- Generality of the Approach
- Summary
Suggested Exercises
- Sidebar: Selected String Methods
Part 2: Entities and Interactions
Chapter Overview
Objectives of this Chapter
- Things
- Primitive Things and Literals
- Numbers
- Characters and Strings
- Booleans
- Objects
- Naming Things
- Referring to Things
- Assignment
- Types
- Declarations and The type-of-thing name-of-thing
Rule
- Definition = Declaration + Assignment
- Primitive Types
- Object Types
- Types of Names
- Shoebox Names
- Label Names
Chapter Summary
Exercises
- Sidebar: Java Naming Syntax and Conventions
- Sidebar: Java Primitive Types
Chapter Overview
Objectives of this Chapter
- Interfaces are Contracts
- Generalized Interfaces and Java Interfaces
- A Java Interface Example
- Method Signatures
- Name
- Parameters and Parameter Types
- Return Type
- Putting It All Together: Abstract Method Declaration
Syntax
- What a Signature Doesn't Say
- Interface Declaration
- Syntax
- Method Footprints and Unique Names
- Interfaces are Types: Behavior Promises
- Interfaces are Not Implementations
Chapter Summary
Exercises
- Style Sidebar: Method Documentation
- Style Sidebar: Interface Documentation
See also Java Chart on Interfaces.
Chapter Overview
Objectives of this Chapter
- Simple Expressions
- Literals
- Names
- Method Invocation
- Combining Expressions
- Assignments and Side-Effecting Expressions
- Other Expressions that Use Objects
- Field Access
- Instance Creation
- Type Membership
- Complex Expressions on Primitive Types: Operations
- Arithmetic Operator Expressions
- Explicit Cast Expressions
- Comparator Expressions
- Logical Operator Expressions
- Parenthetical Expressions and Precedence
Chapter Summary
Exercises
- Style Sidebar: Don't Embed Side-Effecting Expressions
- Sidebar: Java Operators
- Sidebar: Arithmetic Expressions
- Sidebar: Coercion and Casting
- Sidebar: Java Operator Precedence
- Sidebar: Other Assignment Operators
See also Java Chart on Expressions
Chapter Overview
Objectives of this Chapter
- Statements and Instruction-Followers
- Simple Statements
- Declarations and Definitions
- Sequence Statements
- Flow of Control
- Simple Conditionals
- Simple Loops
- Statements and Rules
- Method Invocation Execution Sequence
- Return
Chapter Summary
Exercises
- Style Sidebar: Formatting Declaration Statements
- Style Sidebar: Formatting Blocks
- Style Sidebar: Using Booleans
- Style Sidebar: Documentation
See also Java Chart on Statements
Overview
Objectives of this Interlude
- The Problem
- Representation
- Interacting with the Rules
- Paying Attention to the World
- Fancy Dot Tricks
- Remembering State
- Fields
- Fields vs. Variables
- Summary
Suggested Exercises
Chapter Overview
Objectives of this Chapter
- Classes are Object Factories
- Classes and Instances
- Recipes Don't Taste Good
- Classes are Types
- Class Declaration
- Classes and Interfaces
- implements and type inclusion
- contract vs. implementation
- Data Members, or Fields
- Fields are not Variables
- Hotel Rooms and Storage Rental
- Whose Data Member is it?
- Scoping of Fields
- Comparison of Kinds of Names
- Static Members
- Methods
- Method Declaration
- Method Body and Behavior
- A Method ALWAYS Belongs to an Object
- this.
- Static Methods
- Method Overloading
- Constructors
- Constructors are Not Methods
- Syntax
- Execution Sequence
- Multiple Constructors and the Implicit No-Arg
Constructor
- Constructor Functions
Chapter Summary
Exercises
- Style Sidebar: Class Declaration
- Sidebar: Java Types and Default Initialization
- Table: Comparison of Kinds of Names
- Style Sidebar: Field Documentation
- Style Sidebar: Method Implementation Documentation
- Sidebar: Method Invocation and Execution
- Style Sidebar: Constructor Documentation
- Style Sidebar: Capitalization Conventions
See also Java Charts on Classes, Methods, and Fields.
Part 3: Refining Designs
Chapter Overview
Objectives of this Chapter
- Object-Oriented Design
- Objects are Nouns
- Methods are Verbs
- Interfaces are Adjectives
- Classes are Object Factories
- Some Counter Code (An Example)
- Public and Private
- Kinds of Objects
- Data Repostories
- Resource Libraries
- Traditional Objects
- Types and Objects
- Declared Types and Actual Types
- Use Interface Types
- Use Contained Objects to Implement Behavior
- The Power of Interfaces
Chapter Summary
Exercises
- Style Sidebar: Class and Member Documentation
- Sidebar: Final
- Sidebar: class Math
Collections: An Extended Example
Overview
Objectives
- Exercises
Chapter Overview
Objectives of this Chapter
- Animate Objects
- Animacies are Execution Sequences
- Being Animate-able
- Implementing Animate
- AnimatorThread
- Creating the AnimatorThread in the Constructor
- A Generic AnimateObject
- More Details
- AnimatorThread Details
- Delayed Start and the init() Trick
- Threads and Runnables
- Where do Threads come from?
- Starting a Program
- Why Constructors Need to Return
- Sidebar: class AnimatorThread
- Sidebar: Thread Methods
- Sidebar: class Main
- Style Sidebar: Using main()
Chapter Summary
Exercises
Chapter 10: Reusing Implementation: Inheritance
Chapter Overview
Objectives of this Chapter
- Derived Factories
- Simple Inheritance
- java.lang.Object
- Superclass Membership
- Overriding
- super.
- The Outside-In Rule
- Problems with Private
- Constructors are Recipes
- this()
- super()
- Implicit super()
- Interface Inheritance
- Relationships Between Types
- The class Object
- Style Sidebar: Explicit Use of this. and
super()
- Sidebar: Abstract Classes
- Chapter Summary
Exercises
Interlude: A System of Animate Objects
Objectives
Overview
Suggested Exercises
Chapter 11: When Things Go Wrong: Exceptions
Chapter Overview
Objectives of this Chapter
- Exceptional Events
- When Things Go Wrong
- Expecting the Unexpected
- What's Important to Record
- Throwing an Exception
- Catching an Exception
- Throw vs. Return
- Designing Good Test Cases
- Sidebar: Throw Statements and Throws Clauses
- Sidebar: Try Statement Syntax
- Sidebar: Exceptions, Errors, and RuntimeExceptions
- Chapter Summary
Exercises
Part 4: Refining Interactions
Chapter Overview
Objectives of this Chapter
- Conditional Behavior
- If and else
- Basic Form
- Else
- Cascaded Ifs
- Many Alternatives
- Limited Options: Switch
- Constant Values
- Symbolic Constants
- Using Constants
- Syntax
- Basic Form
- The Default Case
- Variations
- Switch Statement Pros and Cons
- Arrays
- What is an Array?
- Array Declaration
- Array Construction
- Array Elements
- Manipulating Arrays
- Stepping Through an Array Using a For Statement
- Using Arrays for Dispatch
- When to Use Which Construct
- Sidebar: if Statement Syntax
- Sidebar: final
- Style Sidebar: Use Named Constants
- Sidebar: break and continue statements
- Sidebar: switch Statement Syntax
- Sidebar: Array Syntax
- Sidebar: for Statement Syntax
- Chapter Summary
Exercises
Chapter Overview
Objectives of this Chapter
- Design, Abstraction, and Encapsulation
- Procedural Abstraction
- The Description Rule of Thumb
- The Length Rule of Thumb
- The Repetition Rule of Thumb
- An Example
- The Benefits of Abstraction
- Protecting Internal Structure
- private
- Packages
- Packages and Names
- Packages and Visibility
- Inheritance
- Clever Use of Interfaces
- Inner Classes
- Static Classes
- Member Classes
- Local Classes and Anonymous Classes
-
- Style Sidebar: Procedural Abstraction
- Sidebar: Package Naming Summary
- Sidebar: Package Visibility Summary
- Sidebar: Inner Classes
- Chapter Summary
Exercises
Chapter Overview
Objectives of this Chapter
- Procedural Encapsulation and Object Encapsulation
- From Dispatch to Objects
- A Straightforward Dispatch
- Procedural Encapsulation
- Variations
- Pushing Methods Into Objects
- What Happens to the Central Loop?
- The Use of Interfaces
- Runnables as First Class Procedures
- Callbacks
- Recursion
- Structural Recursion
- A Recursive Class Definition
- Methods and Recursive Structure
- The Power of Recursive Structure
- Functional Recursion
Chapter Summary
Exercises
Chapter Overview
Objectives of this Chapter
- Control Loops and Handler Methods
- Dispatch Revisited
- Simple Event Handling
- A Handler Interface
- An Unrealistic Dispatcher
- Sharing the Interface
- Real Event-Driven Programming
- Previous Examples
- The Idea of an Event Queue
- Properties of Event Queues
- Graphical User Interfaces: An Extended Example
- java.awt
- Components
- Graphics
- The Story of paint
- Events and Polymorphism
Chapter Summary
Exercises
See also the AWT Quick Reference.
Interlude: Achieving Customized Behavior
Objectives
Overview
Suggested Exercises
Chapter Overview
Objectives of this Chapter
- Model/View: Separating GUI Behavior from Application Behavior
- The Event Queue, Revisited
- Reading What the User Types: An Example
- Setting up a User Interaction
- Listening for the Event
- Registering Listeners
- Recap
- Specialized Event Objects
- Listeners and Adapters: A Pragmatic Detail
- Inner Class Niceties
- Sidebar: cs101.awt.DefaultFrame
- Chapter Summary
Exercises
See also the AWT Quick Reference.
Interlude: An AWT Application
Objectives
Overview
Suggested Exercises
Part 5: Systems of Objects
Chapter 17: Models of Communities
Chapter Overview
Objectives of this Chapter
- State Machines
- State Spaces
- Organizational Behavior
- Network Models
- Patterns
- UML
- Metrics
- Static Complexity
- Throughput and Latency
- Chapter Summary
Exercises
Chapter 18: Interfaces and Protocols: Gluing Things Together
Chapter Overview
Objectives of this Chapter
- Pacing
- Procedure Calls
- Callbacks
- Explicit Communication Channel Objects
- Protocols
Chapter Summary
Exercises
Chapter Overview
Objectives of this Chapter
- What is a Client-Server Interaction?
- Implementing Client-Server Interactions
- Client Pull
- Server Push
- The Nature of Duals
- Pushing and Pulling Together
- Passive Repository
- Active Constraint
Chapter Summary
Exercises
Interlude: Combining Events and Interactive Control Loops
Objectives
Overview
Suggested Exercises
Chapter Overview
Objectives of this Chapter
- Reads and Writes
- An Example of Conflict
- Synchronization
- Java synchronized
- methods
- (blocks)
- What synchronization buys you
- Safety rules
- Deadlock
- Obscure Details
Chapter Summary
Exercises
Chapter Overview
Objectives of this Chapter
- A Readable Writeable Channel
- Tin Can Telephones
- Streams
- Using A Channel
- For Writing
- Flushing Out the Stream
- A Scribe Example
- For Reading
- Reading and Blocking
- A Lector Example
- Encapsulating Communications
- Real Streams
- Abstract Stream Classes
- Decorator Streams
- Stream Sources
- Decoration in Action
- Network Streams: An Example
- Starting from Streams
- Decorating Streams
- Sockets and Ports
- Using A Socket
- Opening a Client-Side Socket
- Opening a Single Server-Side Socket
- A Multi-Connection Server
- Server Bottlenecks
Chapter Summary
Exercises
Interlude: Client/Server Chat
Objectives
Overview
Suggested Exercises
Chapter 22: Conventional Architectures
Chapter Overview
Objectives of this Chapter
- Server Architechtures
- Dumb broadcast server
- Routing server
- DNS
- RPC
- Peer Architectures
- Ring
- Round Robin
- Cubes
- Arbitration
- Blackboard
- Tuple-space
Chapter Summary
Exercises
Appendices
- Applets
- AWT Quick Reference
- AWT Components
- Component
- Canvas
- Widgets and their Event Types
- Basic Widgets
- ItemSelectable Widgets
- Text Widgets
- Container
- Panel and Frame
- Dimension, Point, and Rectangle
- Graphics
- AWT Events
- ActionEvent and ActionListener
- AWT Listeners and Adapters
- IO Quick Reference
- InputStream and Reader
- OutputStream and Writer
- Sources of Streams
- InputStreamReader and OutputStreamWriter
- Files
- Pipes
- Streams that Add Features
- Buffering
- Primitive Data
- Object Streams and Serialization
- Other Useful Streams
- IOExceptions
- Java Charts
- About Java Charts
- Program File
- Class Declaration
- Field Declaration
- Method Declaration
- Expression
- Statement
- Disclaimers, Notes, Amendments, etc.
- Glossary
- Indices
- Syntax Sidebars
- Style Sidebars
- Interludes
- Case Studies
- Terms