Table of Contents

Preface xi

1 Introduction to Object-Oriented Programming 1

1.1 Programming Paradigms 1

1.2 Why Object-Oriented Programming? 3

1.3 Characteristics and Mechanisms of Object-Oriented Programming 4

1.4 Encapsulation 5

1.5 Class and Object, Messages and Methods 6

1.6 Inheritance 9

1.7 Polymorphism 11

1.8 Abstract and Concrete Classes 13

1.9 Static and Dynamic Binding 15

Bindings in Imperative Languages 15

Bindings in Smalltalk 16

1.10 Summary 17

2 Basics Of Smalltalk 19

2.1 Introduction: Why Smalltalk? 19

2.2 Syntax: From Pseudocode to Smalltalk 20

Arithmetic Expressions 21

Assignment Statements 21

Sequencing 21

From Calling Functions to Sending Messages 21

Returning from Functions and Methods 22

Subscript Access 22

Boolean Expressions 23

Selection 23

Fixed Repetition 23

Variable Repetition 23

Local (Temporary) Variables and Scope 24

Putting It All Together 25

2.3 Numbers and Their Methods 26

The Numeric Classes 26

Numeric Literals 27

Unary Messages 27

Binary Messages 27

Keyword Messages 27

Precedence Rules 28

2.4 Characters 29

2.5 Variables and Assignment Statements 30

2.6 How to Run Smalltalk 31

Starting Smalltalk and the Transcript Window 31

Workspace Windows 32

Entering and Running a Program 32

Printing and Saving a Program 33

Closing and Restarting Smalltalk 33

Saving the Image 34

2.7 Debugging: A First Look 34

Compile-Time Errors 34

Run-Time Errors 35

2.8 Blocks 38

2.9 Control Structures: Logic, Repetition, and Selection 40

Messages to Boolean Objects 40

Logical Operations 40

Repetition 41

Selection 42

2.10 Debugging: Flow of Control Errors 42

Logic Errors and Breakpoints 42

Stopping an Infinite Loop 44

Sample Errors in Control Structures 44

2.11 Summary 46

Key Concepts 46

Programming Problems and Projects 47

3 Introduction to Collections 49

3.1 Strings 50

The Concept of a String 50

String Creators 50

Accessor Messages 51

Mutator Messages 52

Iterator Messages 52

Transformer Messages 53

Copying Strings 53

Comparing Strings 54

Identity (==) and Equality (=) 54

Symbols 55

3.2 Arrays 56

The Concept of an Array 56

Sample Errors 58

3.3 Prompters, the Transcript, and Message Boxes 58

Prompters 59

Input Phase of the Grading Program 60

The Transcript 60

Message Boxes 61

Finishing the Grading Program 61

Sample Errors 62

3.4 Ordered Collections 63

The Concept of an Ordered Collection 63

Redoing the Grading Program with Ordered Collections 63

3.5 Dictionaries 65

The Concept of a Dictionary 65

Redoing the Grading Program with a Dictionary 66

Sets and Bags 67

Inspectors 67

3.6 Iterators 69

The collect: Message 69

The select: Message 70

The reject: Message 70

The inject:into: Message 70

3.7 Browsers 71

The Class Hierarchy Browser 71

The Class Browser 74

The Senders and Implementors Browsers 75

Modifying System Classes 77

3.8 Defining a New Class 80

Interface for the Student Class 81

Rewrite of the Grading Program 81

Implementation of the Student Class 82

Entering the Student Class into Smalltalk 83

Testing the Student Class 85

3.9 Shallow Copying and Deep Copying 87

Ordinary Assignment 87

Assignment with Shallow Copy 87

Assignment with Deep Copy 88

Deep Copy Not Always Enough 89

3.10 Going All the Way with Classes 91

First Rewrite of the Grading Program 91

Second Rewrite of the Grading Program 92

The User Interface 92

Persistent Data, Global Variables, and Saving the Image 93

Implementation 94

The popUpMenu Method 94

Defining the GradingApp2 Class and the openOn: Method 95

The Remaining Methods 96

Creating a Test Bed 97

3.11 Smalltalk Housekeeping 98

Removing Instances of a Class 98

Filing out and Filing in Classes 99

The Change Log 101

3.12 Summary 102

Key Concepts 102

Programming Problems and Projects 103

4 Graphical User Interfaces 107

4.1 A Button Demo 107

The Concept of a Button as Part of an Interface 107

A Sample Application 107

Overview of Creating and Running the Application 108

The open Method 108

Preliminary Test of the Application 110

The add: Method 110

The palindrome: Method 111

Summary of Messages Sent 112

Debugging the Application 112

How Events Are Handled 114

4.2 A Text Pane Demo 115

The Concept of a Text Pane as Part of an Interface 115

A Sample Application 115

The Class Declaration 117

Opening the Application 117

Getting Text for a Text Pane 117

Saving Text from a Text Pane 118

4.3 A List Pane Demo 119

The Concept of a List Pane as Part of an Interface 119

A Sample Application 119

Overview of the Methods 120

The Class Declaration 121

Opening the Application 121

Getting a List for a List Pane 122

Selecting an Item in a List Pane 122

Adding an Item to the Beginning of a List 123

Adding an Item After the Selected Item 124

Removing the Selected Item 125

4.4 A Simple Phone Book 126

Overview of the Methods 127

The Class Declaration 128

Opening the Application 129

The List Pane Methods 130

The Text Pane Methods 130

Adding a Name to the Phone Book 131

Removing a Name from the Phone Book 131

Debugging the Phone Book 131

The Message self changed: 132

4.5 WindowBuilder Pro 133

Opening WindowBuilder Pro 134

Setting Window Attributes 135

Painting Widgets 136

Previewing the Interface 137

Specifying Event Handlers 137

Saving the Interface for an Application 138

Adding a Menu to the Interface 139

Programming the Application 141

open and openOn: Revisited 142

Returning to WindowBuilder Pro 142

Naming Subpanes 142

4.6 Patterns and Independence of Components 143

A Better Implementation of the Model/Interface Pattern 145

The Model/View/Controller Pattern (MVC) 146

4.7 Other GUI Components 147

A Sample Application 147

The Applicant Class 149

Redefining the new Method 151

The ApplicantDialog Class 151

The ApplicantMainWindow Class 155

Exploring Other Screen Controls 156

4.8 Summary 157

Key Concepts 157

Programming Problems and Projects 157

5 Developing a More Complex System 161

5.1 Developing a Complex Application 161

Composition, Subclassing, and Abstraction 161

The Software Development Life Cycle 161

User Requirements 162

5.2 Analysis of the Banking System 163

User Interface 163

List of Classes 165

The Object Model 166

Class Hierarchy 167

5.3 Design of the Banking System 168

OIG for Updating the Customer List 169

OIG for Adding a Customer 170

OIG for Selecting a Customer 171

OIG for Creating a New Savings Account for the Selected Customer

172

OIG for Making a Desposit in the Selected Account 173

Visibility Graph 174

Refine the Class Hierarchy 175

Create Class Summaries 175

5.4 Implementation and Testing of the Banking System Model 175

Implementing the Customer Class 176

Testing the Customer Class 177

Implementing the BankAccount Class 177

Implementing the SavingsAccount Class 179

Testing the SavingsAccount Class 181

Implementing the CheckingAccount Class 182

Implementing the ChargeAccount Class 184

Testing the ChargeAccount Class 186

Implementing the Bank Class 186

Testing the Bank Class 188

5.5 Connecting an Interface the Banking System Model 189

Building the Bank Interface with WindowBuilder Pro 190

Laying out the Subpanes 190

Specifying the Event Handlers for the Subpanes 191

Setting up the Menus 191

Installing the Interface and Declaring the Variables 192

Updating the Menus 193

Responding to Events in the Subpanes 194

Responding to Menu Events 195

Opening a Window on a Bank 196

Testing the Bank Interface 196

5.6 Summary 196

Key Concepts 196

Programming Problems and Projects 197

6 File Handling 199

6.1 Streams and Files 199

The Concept of a Stream 199

The Stream Class Hierarchy 199

The ReadStream Class 200

The WriteStream Class 201

The FileStream Class 203

File Dialogs and Directories 204

6.2 A Text Analyzer 207

Building the Interface 207

Completing the Application 208

Responding to Events in the Text Panes 209

Saving the Statistics 209

Obtaining the Statistics 210

Opening the Window 211

6.3 The Banking System Revisited 211

Testing the Object Filer wth a Bank 212

Adding File Operations to the Bank Interface 213

Editing the Bank Interface 213

Implementing the File Menu Event Handlers 213

6.4 Summary 215

Key Concepts 215

Programming Problems and Projects 215

7 Graphics 217

7.1 The Conceptual Framework for Graphics 217

Coordinate Systems and Points 217

Rectangles 218

Class Point and Class Rectangle 219

Graphics Media and Pens 221

Class Bitmap 222

Class Screen 223

Class GraphicsTool 223

Class Pen 223

An Example of a Pen 225

An Example of Drawing with a Pen 225

Turtle Graphics 226

Drawing Shapes 227

Transient and Refreshable Images 228

7.2 Graph Pane Demos 229

Sketchpad1 230

Sketchpad2 231

Drawing Shapes 234

The Algorithms 235

Running the Application 236

Code for the ShapeDemo Class 236

7.3 An Animation Pane Demo 240

Animation Panes 240

Animated Objects 242

An Example 243

Implementation 244

Opening and Closing the Window 244

Responding to Button Events 245

Popping Up the Menu 245

Drawing an Object 246

Manipulating a Selected Object 247

Responding to Events in the All Rectangles Menu 247

7.4 A Pie Chart for the Grade Management System 248

7.5 Summary 250

Key Concepts 250

Programming Problems and Projects 251

Bibliography 253

Index 255