prelude to programming : concepts and design ·  · 2012-04-05anintroduction to object-oriented...

9
Addison-Wesley Boston Columbus Indianapolis New York San Srancisco Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi jMcxico City Sao Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo

Upload: hatu

Post on 02-May-2018

218 views

Category:

Documents


2 download

TRANSCRIPT

Addison-Wesley

Boston Columbus Indianapolis New York San Srancisco Upper Saddle River

Amsterdam CapeTown Dubai London Madrid Milan Munich Paris Montreal Toronto

Delhi jMcxico City Sao Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo

Contents

Preface xv

0 Introduction 1

In the Everyday World: Computers Everywhere 2

0.1 A Brief History of Computers 2

What Is a Computer? 2

Personal Computers 4

The Internet 7

0.2 Computer Basics 8

The Central Processing Unit 9

Internal Memory 9

Mass Storage Devices 10

Input Devices 12

Output Devices 12

0.3 Software and Programming Languages 14

Types of Software 14

Types of Programming Languages IS

Chapter Review and Exercises 18

J An Introduction to Programming 23

In the Everyday World: You Are Already a Programmer! 24

1.1 What Is Programming? 24

A General Problem-Solving Strategy 25

Creating Computer Programs: The Program Development Cycle 26

1.2 Basic Programming Concepts 27

A Simple Program 28

Data Input 30

Program Variables and Constants 32

1.3 Data Processing and Output 35

Processing Data 35

Data Output 39

vii

viii Contents

1.4 Character and String Data 43

The Declare Statement 43

1.5 Integer Data 46

Operations on Integers 47

The Binary Number System 48

Integer Representation 50

1.6 Floating Point Data 55

The Declare Statement Revisited 56

Types of Floating Point Numbers 57

Floating Point Representation 59

Chapter Review and Exercises 62

2 Developing a Program 69

In the Everyday World: Planning to Program? You Need a Plan 70

2.1 The Program Development Cycle 71

The Process of Developing a Program 71

Additional Steps in the Cycle 74

2.2 Program Design 78

Modular Programming 78

2.3 Coding, Documenting, and Testing a Program 86

Coding and Documenting a Program 87

Testing a Program 89

Types of Errors 89

2.4 Commercial Programs: Testing and Documenting 91

The Testing Phase Revisited 91

External Documentation 92

2.5 Structured Programming 94

Flowcharts 94

Control Structures 99

Programming Style 102

Chapter Review and Exercises 104

3 Selection Structures: Making Decisions 111

In the Everyday World: Decisions, Decisions, Decisions... 112

3.1 An Introduction to Selection Structures 113

Types of Selection Structures 113

Single- and Dual-Alternative Structures 114

3.2 Relational and Logical Operators 120

Relational Operators 120

Logical Operators 123

Hierarchy of Operations 128

3.3 ASCII Code and Comparing Strings 130

Representing Characters by Numbers 130

3.4 Selecting from Several Alternatives 133

Using If Structures 134

Using Case-Like Statements 137

3.5 Applications of Selection Structures 141

Defensive Programming 141

Menu-Driven Programs 144

3.6 Focus on Problem Solving: A New Car Price Calculator 147

Problem Statement 147

Problem Analysis 147

Program Design 148

Program Code 151

Program Test 151

Chapter Review and Exercises 152

4 Repetition Structures: Looping 163

In the Everyday World: Loops 164

4.1 An Introduction to Repetition Structures:

Computers Never Get Bored! 165

Loop Basics 165

Relational and Logical Operators 168

Constructing Flowcharts with a Loop Structure 169

4.2 Types of Loops 172

Pre-Test and Post-Test Loops 173

Counter-Controlled Loops 177

4.3 The For Loop 183

The For Statement 184

The For Loop in Action 187

The Careful Bean Counter 190

4.4 Applications of Repetition Structures 195

Using Sentinel-Controlled Loops to Input Data 195

Data Validation 199

The Floor () and Ceiling () Functions 203

Computing Sums and Averages 206

4.5 Focus on Problem Solving: A Cost, Revenue, and Profit Problem

Problem Statement 211

Problem Analysis 211

Program Design 213

Program Code 216

Program Test 216

Chapter Review and Exercises 218

5 More about Loops and Decisions 225

In the Everyday World: Advanced Loops 226

5.1 Combining Loops with If-Then Statements 226

Exiting a Loop Early 227

X Contents

5.2 Combining Loops with If-Then Structures in Longer Programs 236

The Length_of() Function 242

The Print Statement and the New Line Indicator 242

5.3 Random Numbers 246

The Random Function 247

Not Really Random: The Pseudorandom Number 251

5.4 Nested Loops 252

Nested For Loops 253

Nesting Other Kinds of Loops 257

A Mental Workout: Mind Games 262

5.5 Focus on Problem Solving: A Guessing Game 269

Problem Statement 270

Problem Analysis 270

Program Design 271

Program Code 276

Program Test 276

Chapter Review and Exercises 278

6 Arrays: Lists and Tables 285

In the Everyday World: Arrays 286

6.1 One-Dimensional Arrays 287

Array Basics 287

Parallel Arrays 293

Some Advantages of Using Arrays 296

6.2 Searching and Sorting Arrays 300

The Serial Search Technique 300

The Bubble Sort Technique 304

6.3 More about Searching and Sorting 312

Binary Search 313

Selection Sort 317

6.4 Strings as Arrays of Characters 322

Concatenation Revisited 323

String Length versus Array Size 324

6.5 Two-Dimensional Arrays 326

An Introduction to Two-Dimensional Arrays 326

Using Two-Dimensional Arrays 328

6.6 Focus on Problem Solving: A Grade Management Program 332

Problem Statement 333

Problem Analysis 333

Program Design 335

Program Code 341

Program Test 341

Chapter Review and Exercises 342

7 Program Modules, Subprograms, and Functions 351

In the Everyday World: Living and Programming in ManageablePieces: Subprograms 352

7.1 Data Flow Diagrams and Parameters 353

The Big Sale: The Sale Price Computation Program 353

Data Flow Diagrams 354

An Introduction to Arguments and Parameters 355

7.2 More about Subprograms 362

Value and Reference Parameters 362

How to Tell the Difference between Value and Reference Parameters 363

Two Helpful Functions: Tol)pper() and ToLower() 367

The Scope of a Variable 370

7.3 Functions 374

Built-in Functions 374

User-Defined Functions 378

7.4 Recursion 382

The Recursive Process 383

7.5 Focus on Problem Solving: A Grade Management Program 388

Problem Statement 388

Problem Analysis 388

Program Design 388

Program Code 395

Program Test 396

Chapter Review and Exercises 398

3 Sequential Data Files 405

In the Everyday World: Data Files 406

8.1 An Introduction to Data Files 407

File Basics 407

Creating and Reading Sequential Files 409

8.2 Modifying a Sequential File 415

Deleting Records 415

Modifying Records 418

Inserting Records 419

Using Arrays in File Maintenance 421

8.3 Merging Sequential Files 423

8.4 Focus on Problem Solving: Control Break Processing 426

Problem Statement 427

Problem Analysis 427

Program Design 429

Coding and Testing the Program 431

xii Contents

8.5 Focus on Problem Solving: The Invoice Preparation Program 432Problem Statement 432

Problem Analysis 432

Program Design 434

Program Code 438

Program Test 438

Chapter Review and Exercises 439

9 An Introduction to Object-Oriented Programming 447

In the Everyday World: Objects 448

9.1 Classes and Objects 448

Classes 448

Defining Classes and Creating Objects 450

Creating Objects 453

The Constructor 454

9.2 More Features of Object-Oriented Programming 456Benefits of Object-Oriented Languages 456

Inheritance and Polymorphism 456

9.3 Object-Oriented Program Design and UML 468

Modeling Languages 470

Unified Modeling Language 471

9.4 Focus on Problem Solving: Using OOP to Create the Parking Lot

Program 474

Problem Statement 474

Problem Analysis 474

Program Design 476

Creating the Necessary Submodules 482

Program Code 485

Program Test 485

Chapter Review and Exercises 486

Appendix A:

Decimal, Binary, and Hexadecimal Representation 493

A.l Bases and Exponents 493

Expanded Notation 494

A.2 The Binary System 495

Converting Binary to Decimal 497

A.3 The Hexadecimal System 497

Hexadecimal Digits 498

Converting Decimal to Hexadecimal 498

Converting Hexadecimal to Decimal 500

Using Hexadecimal Notation 500

Converting Binary to Hexadecimal 501

Appendix B:

Integer Representation 503

B.I Unsigned Integer Format 503

Overflow 504

B.2 Sign and Magnitude Format 504

Representation of Sign-and-Magnttude Integers 505

The Zero 506

B.3 One's Complement Format 506The Zero Again 507

B.4 Two's Complement Format 508The Two's Complement Zero 511

Why the Two's Complement Works 512

Appendix C:

Floating Point Number Representation 513

C. I Binary Conversions to Floating Point 513

Floating Point Numbers—the Integer Part 513

Floating Point Numbers—the Fractional Part 513

Converting a Decimal Fraction to Binary 515

C.2 Putting the Two Parts Together 517

C.3 Scientific and Exponential Notation 518

Scientific Notation 518

Exponential Notation 519

C.4 Base 10 Normalization 520

C.5 Normalizing Binary Floating Point Numbers 520The Excess_127 System 521

Base 2 Normalization 521

Single Precision Floating Point Numbers 522

C.6 Hexadecimal Representation 525

Appendix D:

Introduction to RAPTOR 527

D.I What Is RAPTOR? 527

D.2 Getting Started 527

D.3 Introduction to RAPTOR Symbols and Variables 530Variables 530

D.4 RAPTOR Symbols 533

Input Symbol 533

Assignment Symbol 535

Output Symbol 538

D.5 Comments 539

xiv Contents

D.6 Built-in Operators and Functions 541

Built-in Constants 542

Relational Operators 542

Boolean (Logical) Operators 543

D.7 Making Decisions: The Selection Symbol 543

D.8 Repetition: The Loop Symbol 544

D.9 The Call Symbol and Subcharts 547

D.10 Arrays 549

D.11 Creating and Displaying Data Files 550

The Redirect_Output Procedure 550

The Redirect_Input Procedure 553

The End_0f_Input Function 553

D.12 Object-Oriented Mode 554

Creating a Class 556

Example: Using the Cube Class to Find the Volume of a Cube 557

Code the Methods 560

The Main Program 563

Inheritance and Polymorphism 564

Glossary 565

Index 585