Charles Dierbach
Wiley
Contents
Preface
XXI
Acknowledgments
xxv
About the Author
xxvii
Introduction
MOTIVATION
FUNDAMENTALS
1.1 What Is Computer Science?
Computational Problem Solving
5
of Computational Problem Solving
1.1.1 The Essence of
1.1.2 Limits
Self-Test Questions
1.2
1.2.2
Algorithm? 6
Algorithms and Computers:
an
Self-Test Questions
1.3
Computer Algorithms
1.2.1 What, Is
A Perfect Match
Computer Hardware 9
1.3.1 Digital Computing: It's All about
1.3.3 Fundamental Hardware
1.3.4 Operating
1.3.5 Limits of
8
Switches
10
1.3.2 The Binary Number System
11
Components
SystemsBridging Software and Hardware
Integrated
Self-Test
1.4
Questions
Computer Software
Circuits
Technology: Moore's
13
14
Computer Software? 14
14
1.4.2 Syntax, Semantics, and Program Translation
17
1.4.3 Procedural vs. Object-Oriented Programming
1.4.1 What Is
Self-Test Questions
17
COMPUTATIONAL PROBLEM SOLVING
Computational
Problem Analysis
18
1.5 The Process of
1.5.1
1.5.2 Program Design
Problem
19
Program Implementation
21
1.5.4 Program Testing
1.5.3
21
17
Solving
17
Law
11
12
Contents
1.6 The
1.6.3 The
22
Python
1.6.2 The IDLE
Python Development Environment
Standard
Python
Python
1.6.4 A Bit of
1.6.5
Learning
1.7 A First
Library
ProgramCalculating
26
the Drake Equation
29
30
1.7.2 Problem Analysis
30
1.7.3
Program Design
1.7.4
30
Program Implementation
1.7.5
30
32
Program Testing
Chapter Summary 33
Chapter Exercises 34
Python Programming Exercises
36
Modification Problems
Program Development
22
23
24
How to Use IDLE
1.7.1 The Problem
Program
22
Python Programming Language
1.6.1 About
37
Problems
37
Data and Expressions
MOTIVATION
38
39
FUNDAMENTAL CONCEPTS
2.1 Literals
2.1.1 What Is
Literal?
2.1.2 Numeric Literals
2.1.3
40
40
String Literals
40
40
44
2.1.4 Control Characters
46
2.1.5
47
String Formatting
2.1.6
Implicit and Explicit Line Joining
48
2.1.7 Let's Apply It"Hello World Unicode Encoding"
Self-Test
Questions
2.2 Variables and Identifiers
2.2.1 What Is
50
Variable?
50
2.2.2 Variable Assignment and
2.2.3 What Is
2.2.4
Identifier?
Keywords and
2.2.5 Let's
Self-Test
2.3
an
Operators
Apply
Keyboard Input
52
53
Other Predefined Identifiers in
It"Restaurant Tab Calculation"
Questions
an
2.3.2 Arithmetic
57
Operator?
Operators
57
Self-Test
Questions 60
Expressions and Data Types
2.4.1 What Is
an
61
61
Expression?
2.4.2 Operator Precedence
2.4.3
55
56
2.3.3 Let's Apply It"Your Place in the Universe"
2.4
Python
57
2.3.1 What Is
48
49
Operator Associativity
61
63
59
54
Contents
2.4.4 What Is
2.4.5
Data
64
Type?
64
Mixed-Type Expressions
2.4.6 Let's Apply
Self-Test
It"Temperature
Questions 66
Conversion
COMPUTATIONAL PROBLEM SOLVING
2.5
Age
in Seconds
2.5.2 Problem
2.5.3
67
67
Program
2.5.1 The Problem
65
Program"
67
67
Analysis
67
Program Design
Implementation
2.5.4 Program
Chapter Summary
74
Chapter Exercises
74
Python Programming Exercises
Program Modification Problems
and
Testing
69
76
76
Program Development Problems
77
Control Structures
MOTIVATION
79
80
FUNDAMENTAL CONCEPTS
3.1 What Is
80
Control Structure?
80
3.2 Boolean
Expressions (Conditions)
Operators 81
3.2.2 Membership Operators
82
81
3.2.1 Relational
3.2.3 Boolean
3.2.4
Precedence and Boolean
3.2.5 Short-Circuit
3.2.6
83
Operators
Operator
(Lazy)
Evaluation
Expressions
Logically Equivalent Boolean Expressions
Self-Test
85
86
87
88
Questions
3.3 Selection Control
89
3.3.1 If Statement
89
3.3.2 Indentation in
Python
90
3.3.3
Multi-Way Selection 91
3.3.4 Let's Apply ItNumber of Days
Self-Test Questions
96
3.4 Iterative Control
Program
96
3.4.1 While Statement
97
3.4.2 Input Error Checking
3.4.3 Infinite
in Month
loops
98
99
3.4.4 Definite
vs.
3.4.5 Boolean
Indefinite
Flags and Indefinite Loops
Loops
100
100
3.4.6 Let's Apply ItCoin Change Exercise Program
Self-Test
Questions
104
COMPUTATIONAL PROBLEM SOLVING
3.5 Calendar Month
3.5.1
Program
The Problem
104
104
104
101
94
xi
3.5.2 Problem Analysis
3.5.3
104
105
Program Design
3.5.4
Program Implementation and Testing
Chapter Summary 117
Chapter
Exercises
107
118
Python Programming Exercises
120
Program Modification Problems
Program Development
121
Problems
123
Lists
125
MOTIVATION
126
FUNDAMENTAL CONCEPTS
4.1
List Structures
4.1.1 What Is
127
127
a
List?
4.1.2 Common List
127
4.1.3 List Traversal
128
Self-Test Questions
129
4.2 Lists
(Sequences)
4.2.1
List
4.2.2
Python
Tuples
4.2.3
Sequences
127
Operations
in
130
Python
130
Type
131
132
4.2.4 Nested Lists
134
4.2.5 Let's Apply ItA Chinese Zodiac Program
Self-Test Questions
4.3
Iterating Over Lists (Sequences) in Python
4.3.1
For
135
137
137
137
Loops
4.3.2 The Built-in range Function
138
4.3.3
List Index Values
Iterating Over List Elements
4.3.4 While
4.3.5 Let's
Self-Test
4.4 More
4.4.1
on
Loops
Apply
and Lists
(Sequences)
144
144
Assigning and Copying Lists
144
146
Comprehensions
COMPUTATIONAL PROBLEM SOLVING
4.5 Calendar Year
Program
4.5.1 The Problem
4.5.2 Problem
147
147
147
Analysis
147
4.5.3 Program
Design 148
4.5.4 Program Implementation and Testing
Chapter Summary
161
Chapter Exercises
162
Python Programming Exercises
Program Modification Problems
Program Development Problems
164
164
165
139
140
ItPassword Encryption/Decryption
Questions
Python Lists
4.4.2 List
vs.
149
Program
141
Contents
Functions
168
MOTIVATION
169
169
FUNDAMENTAL CONCEPTS
5.1
Program
Routines
5.1.1 What Is
5.1.2
169
Function Routine?
Functions
Defining
169
170
5.1.3 Let's Apply
ItTemperature
Self-Test Questions
175
5.2 More
on
Functions
Conversion
Program (Function Version)
173
176
5.2.1
Calling Value-Returning Functions 176
177
5.2.2 Calling Non-Value-Returning Functions
5.2.3 Parameter Passing
178
181
5.2.4 Keyword Arguments in Python
5.2.5 Default Arguments in Python
183
5.2.6 Variable Scope
183
186
5.2.7 Let's Apply ItGPA Calculation Program
Self-Test Questions
189
COMPUTATIONAL PROBLEM SOLVING
5.3 Credit Card Calculation
5.3.1 The Problem
5.3.2 Problem
5.3.3
Program
189
190
Analysis
190
Program Design
5.3.4
Program Implementation
Chapter Summary 202
Chapter Exercises 202
Python Programming
Exercises
and
Testing
191
203
Program Modification Problems
Program Development
189
189
204
Problems
204
Objects and Their Use
MOTIVATION
206
207
FUNDAMENTAL CONCEPTS
6.1 Software Objects
6.1.1 What Is
6.1.2
an
Object?
References
Object
Questions
Self-Test
6.2 Turtle
6.2.1
208
209
216
216
Graphics
Creating
207
207
Turtle
Graphics
6.2.2 The "Default" Turtle
Window
216
218
6.2.3 Fundamental Turtle Attributes and Behavior
6.2.4 Additional Turtle Attributes
6.2.5
Creating Multiple
Turtles
222
225
219
xiii
xiv
Contents
6.2.6 Let's
Apply ItBouncing Balls Program
Self-Test Questions
COMPUTATIONAL PROBLEM SOLVING
6.3 Horse Race Simulation
6.3.1 The Problem
229
229
Program
230
6.3.2 Problem Analysis
6.3.3
230
231
Program Design
6.3.4 Program Implementation and Testing
Chapter Summary
243
Chapter Exercises
243
Python Programming
Program
Exercises
Modular
245
246
Design
MOTIVATION
247
248
FUNDAMENTAL CONCEPTS
7.1
Modules
248
248
7.1.1 What Is
Module?
248
7.1.2 Module Specification
Self-Test Questions
7.2
Developing
7.2.2
251
a
Specification
Modular Design of the Calendar Year Program
of the Calendar Year Program Modules
Self-Test Questions
Python
Modules
7.3.1 What Is
249
251
Top-Down Design
7.2.1
7.3
231
244
Modification Problems
Program Development Problems
j^J
226
229
252
255
255
Python Module? 255
Namespaces 256
7.3.2 Modules and
7.3.3
Importing Modules
257
7.3.4 Module Loading and Execution
7.3.5 Local, Global, and Built-in
7.3.6 A
260
Namespaces in Python
Programmer-Defined Stack Module
7.3.7 Let's Apply ItA Palindrome Checker Program
Self-Test Questions
268
COMPUTATIONAL PROBLEM SOLVING
7.4 Calendar Year
7.4.1 The Problem
7.4.2 Problem
7.4.3
7.4.4
269
269
Program Design
Program Implementation
Chapter Summary
Exercises
284
Python Programming Exercises
Program
269
269
Analysis
284
Chapter
269
Program (function version)
Modification Problems
Program Development Problems
and
286
287
287
Testing
262
264
269
267
251
Text Files
289
MOTIVATION
290
FUNDAMENTAL CONCEPTS
8.1 What Is
8.2
Using
8.2.1
Text File?
Text Files
291
Text Files
8.2.2
Opening
Reading Text Files
8.2.3
Writing
294
295
296
String Processing
8.3.1
Traversal
String
8.3.2
String-Applicable Sequence Operations
8.3.3
String Methods
8.3.4 Let's Apply
296
297
Text
Program
300
303
Questions
Exception Handling
8.4.1 What Is
296
ItSparse
Self-Test
8.4
291
293
Text Files
Self-Test Questions
8.3
290
290
303
Exception? 303
Propagation of Raised Exceptions 304
8.4.3 Catching and Handling Exceptions
305
8.4.4 Exception Handling and User Input 307
8.4.5 Exception Handling and File Processing
309
8.4.6 Let's Apply ItWord Frequency Count Program
an
8.4.2 The
Self-Test
COMPUTATIONAL PROBLEM SOLVING
8.5
Cancer Correlation
Cigarette Use/Lung
8.5.1 The Problem
8.5.4
8.5.5
314
Program
314
315
8.5.2 Problem
8.5.3
Analysis
Program Design
315
316
Program Implementation and Testing 318
Determining the Correlation Between Smoking
Chapter Summary
331
Chapter Exercises
332
Exercises
Python Programming
Program Modification Problems
Program Development
310
314
Questions
Problems
and
Lung Cancer
333
333
334
Dictionaries and Sets
MOTIVATION
337
338
FUNDAMENTAL CONCEPTS
9.1
Dictionary Type
9.1.1 What Is
in
Python
Dictionary?
338
338
339
9.1.2 Let's Apply ItPhone Number
Self-Test
Questions
331
346
Spelling Program
342
xvi
Contents
9.2 Set Data Type
9.2.1
346
The Set Data
in
Type
346
Python
9.2.2 Let's Apply ItKitchen Tile Visualization
348
356
Self-Test Questions
COMPUTATIONAL PROBLEM SOLVING
9.3 A Food
Program
Co-op's Worker Scheduling
9.3.1 The Problem
356
Simulation
356
357
9.3.2 Problem
Analysis 357
9.3.3 Program Design 358
9.3.4 Program Implementation and Testing
9.3.5 Analyzing
Scheduled
vs.
360
Unscheduled
Co-op Worker Approach
375
379
Chapter Summary
Chapter Exercises 379
Python Programming Exercises
380
Program Modification Problems
Program Development
380
Problems
381
383
Object-Oriented Programming
MOTIVATION
384
FUNDAMENTAL CONCEPTS
10.1 What Is
384
Class?
385
10.1.2 Three Fundamental Features of
10.2
Encapsulation
Classes in
10.2.3 Let's Apply ItA
387
Python
Recipe
Conversion
Program
399
Questions
10.3 Inheritance
386
Encapsulation?
Defining
Self-Test
Object-Oriented Programming
386
10.2.1 What Is
10.2.2
384
Object-Oriented Programming?
10.1.1 What Is
400
10.3.1 What Is Inheritance?
10.3.2
Subtypes
10.3.3
Defining Subclasses
400
401
in
Python
402
10.3.4 Let's Apply ItA Mixed Fraction Class
Self-Test Questions
10.4
Polymorphism
10.4.1 What Is
411
411
411
Polymorphism?
10.4.2 The Use of Polymorphism
Self-Test Questions
10.5 Object-Oriented Design
10.5.1 What Is UML?
10.5.2 UML Class
414
417
Using UML
Diagrams
Self-Test Questions
417
417
418
422
COMPUTATIONAL PROBLEM SOLVING
10.6 Vehicle Rental
Agency Program
10.6.1 The Problem
423
423
423
407
394
385
Contents
10.6.2 Problem
Analysis 423
Program Design 423
10.6.4 Program Implementation
Chapter Summary 453
10.6.3
Chapter
Exercises
Testing
429
454
Python Programming Exercises
Program
and
455
Modification Problems
456
Program Development Problems
457
Recursion
460
MOTIVATION
461
FUNDAMENTAL CONCEPTS
11.1 Recursive Functions
11.1.1 What Is
461
461
Recursive Function?
461
11.1.2 The Factorial Function
464
11.1.3 Let's Apply ItFractals
(Sierpinski Triangle)
Self-Test
11.2 Recursive Problem
11.2.1
11.2.2
472
Solving
Thinking Recursively 472
MergeSort Recursive Algorithm
11.2.3 Let's Apply
Self-Test
vs.
472
ItMergeSort Implementation
474
476
Questions
11.3 Iteration
467
471
Questions
Recursion
476
COMPUTATIONAL PROBLEM SOLVING
11.4 Towers of Hanoi
477
477
11.4.1 The Problem
477
11.4.2 Problem
Analysis 477
11.4.3 Program Design and Implementation
Chapter Summary
487
Chapter Exercises
487
Python Programming
Program
Exercises
488
Modification Problems
Program Development
481
489
Problems
490
Computing and Its Developments
491
CONTRIBUTIONS TO THE MODERN COMPUTER
12.1 The
Concept of
Programmable Computer
492
492
12.1.1 "Father of the Modern
12.1.2 "The First Computer
12.2
Electronic
Computing 493
Development of Boolean Algebra (mid-1800s)
Developments Leading to
12.2.1 The
Computer"Charles Babbage (1800s) 492
Programmer"Ada Lovelace (1800s) 493
12.2.2 The Development of the Vacuum Tube (1883)
12.2.3 The
Development
of
493
494
Digital Electronic Logic Gates (1903)
494
xvii
xviii
Contents
12.2.4 The Development of Memory Electronic Circuits (1919) 495
12.2.5 The Development of Electronic Digital
Logic Circuits (1937) 495
12.2.6 "The Father of Information
Theory"Claude Shannon (1948) 496
FIRST-GENERATION COMPUTERS (1940s-mid- 1950s)
12.3 The Early Groundbreakers 496
12.3.1 The Z3The First
496
Programmable Computer (1941)
496
12.3.2 The Mark IFirst Computer
Project in the United States
(1937-1943)
12.3.3 The ABCThe First
497
Fully Electronic Computing Device (1942) 498
12.3.4 ColossusA Special-Purpose Electronic Computer
(1943) 499
12.3.5 ENIACThe First Fully Electronic Programmable
Computer 500
12.3.6 EDVAC/ACEThe First Stored Program Computers
(1950) 501
12.3.7 WhirlwindThe First Real-Time Computer
(1951) 502
12.4 The First Commercially Available
Computers 503
12.4.1 The Struggles of the Eckert-Mauchly Computer
Corporation (1950) 503
12.4.2 The LEO Computer of the J.
and
Lyons
Company (1951) 504
SECOND-GENERATION COMPUTERS (mid-1950s
12.5 Transistorized Computers
505
to
mid-1960s)
505
12.5.1 The Development of the Transistor (1947)
505
12.5.2 The First Transistor
506
(1953)
Computer
12.6 The
Development of High-Level Programming Languages 506
The Development of Assembly
Language (early 1950s) 506
12.6.2 The First High-Level
Programming Languages (mid-1950s) 507
12.6.3 The First "Program Bug" (1947)
508
12.6.1
THIRD-GENERATION COMPUTERS (mid-1960s to early 1970s)
12.7 The Development of the Integrated Circuit (1958)
508
12.7.1 The
508
Catalyst for Integrated Circuit Advancements (1960s)
Microprocessor (1971) 511
12.8 Mainframes, Minicomputers, and Supercomputers 512
509
12.7.2 The Development of the
12.8.1 The Establishment of the Mainframe Computer (1962)
12.8.2 The Development of the Minicomputer (1963)
513
12.8.3 The
Development
512
of the UNDC
Operating System (1969) 513
(early 1960s) 514
Supercomputer (1972) 515
12.8.4 The Development of Graphical User Interfaces
12.8.5 The
Development of the
FOURTH-GENERATION COMPUTERS
(early 1970s to the Present) 515
Microprocessor 515
12.9.1 The First Commercially Available Microprocessor (1971)
515
12.9.2 The First Commercially Available Microcomputer Kit
(1975) 516
12.10 The Dawn of Personal
516
Computing
12.10.1 The Beginnings of Microsoft (1975)
516
12.10.2 The Apple II (1977)
517
12.10.3 IBM's Entry into the Microcomputer Market (1981)
517
12.10.4 Society Embraces the Personal Computer
(1983) 518
12.10.5 The Development of Graphical User Interfaces (GUIs) 518
12.10.6 The Development of the C+ + Programming
Language 519
12.9 The Rise of the
Contents
THE DEVELOPMENT OF COMPUTER NETWORKS
12.11 The
Development of Wide Area Networks
520
520
12.11.1 Theldeaof Packet-Switched Networks (early 1960s)
12.11.2 The First Packet-Switched Network: ARPANET
12.12 The Development of Local Area Networks (LANs)
12.12.1 The Need for Local Area Networks
12.12.2 The
12.13 The
Development
Development
of Ethernet
(1980)
12.13.2 The Development of the TCP/IP
520
521
521
521
of the Internet and World Wide Web
12.13.1 The Realization of the Need for
520
(1969)
"Internetworking"
522
522
Internetworking Protocol (1973)
12.13.3 The
Development
of the World Wide Web
12.13.4 The
Development
of the Java
(1990)
522
522
Programming Language (1995)
523
Appendix
525
Index
569
xlx