Anda di halaman 1dari 16

Second AP* Edition

with GridWorld

Java
Methods
Object-Oriented Programming
and
Data Structures
Maria Litvin
Phillips Academy, Andover, Massachusetts

Gary Litvin
Skylight Software, Inc.

Skylight Publishing
Andover, Massachusetts

Skylight Publishing
9 Bartlet Street, Suite 70
Andover, MA 01810
web:
e-mail:

http://www.skylit.com
sales@skylit.com
support@skylit.com

Copyright 2011 by Maria Litvin, Gary Litvin, and


Skylight Publishing
All rights reserved. No part of this publication may be reproduced,
stored in a retrieval system, or transmitted, in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without
the prior written permission of the authors and Skylight Publishing.

Library of Congress Control Number: 2010915303


ISBN 978-0-9824775-7-1
*
AP and Advanced Placement are registered trademarks of The College Board, which was
not involved in the production of and does not endorse this book.

The names of commercially available software and products mentioned in this book are
used for identification purposes only and may be trademarks or registered trademarks
owned by corporations and other commercial entities. Skylight Publishing and the authors
have no affiliation with and disclaim any sponsorship or endorsement by any of these
product manufacturers or trademark owners.
Oracle, Java, and Java logos are trademarks or registered trademarks of Oracle Corporation
and/or its affiliates in the U.S. and other countries.
SCRABBLE is the registered trademark of HASBRO in the United States and Canada and
of J.W. Spear and Sons, PLC, a subsidiary of Mattel, Inc., outside the United States and
Canada.

1 2 3 4 5 6 7 8 9 10

16 15 14 13 12 11

Printed in the United States of America

To Marg and Aaron

Brief Contents
Preface xvii
How to Use This Book xxi
Chapter 1 An Introduction to Hardware, Software, and the Internet
Chapter 2 An Introduction to Software Engineering 11
Chapter 3 Objects and Classes 43
Chapter 4 Algorithms 79
Chapter 5 Java Syntax and Style 107
Chapter 6 Data Types, Variables, and Arithmetic 125
Chapter 7 Boolean Expressions and if-else Statements 157
Chapter 8 Iterative Statements: while, for, dowhile 199
Chapter 9 Implementing Classes and Using Objects 217
Chapter 10 Strings 265
Chapter 11 Class Hierarchies and Interfaces 293
Chapter 12 Arrays 333
Chapter 13 java.util.ArrayList 361
Chapter 14 Searching and Sorting 381
Chapter 15 Streams and Files 411
Chapter 16 Graphics 427
Chapter 17 GUI Components and Events 433
Chapter 18 Mouse, Keyboard, Sounds, and Images 441
Chapter 19 Big-O Analysis of Algorithms 445
Chapter 20 The Java Collections Framework 465
Chapter 21 Lists and Iterators 507
Chapter 22 Stacks and Queues 527
Chapter 23 Recursion Revisited 549
Chapter 24 Binary Trees 577
Chapter 25 Lookup Tables and Hashing 605
Chapter 26 Heaps and Priority Queues 625
Chapter 27 Design Patterns 639
Appendices 647
Solutions to Selected Exercises 647
Index of Tables and Figures 649
Index 657

Start

AP CS
Exam

1. Hardware/Software

Data
Structures

2. Software Engineering

19. Big-O

3. Objects/Classes
4. Algorithms

20. Java Collections

5. Syntax/Style

6. Arithmetic

21. Lists/Iterators

7. Boolean/if-else

22. Stacks/Queues

8. while/for loops

23. Recursion++

9. Classes: details

10. Strings

24. Binary Trees

Files,
Graphics,
GUI

11. Class Hierarchies

15. Files

12. Arrays

16. Graphics

13. ArrayList

17. GUI/Events

14. Searching/Sorting

18. Mouse/Keyboard

25. Hashing

26. Heaps/Priority Qs

27. Design Patterns

Finish

Contents
Preface

xvii

How to Use This Book

xxi

Chapter 1. An Introduction to Hardware, Software, and the Internet


1.1
1.2

1.3
1.4
1.5

1.6
1.7

Prologue 2
Hardware Overview
 www.skylit.com/javamethods
1.2.1 The CPU
1.2.2 Memory
1.2.3 Secondary Storage Devices
1.2.4 Input and Output Devices
Software Overview
What Do Software Engineers Do?
Representation of Information in Computer Memory
1.5.1 Numbers
1.5.2 Characters
The Internet
Summary
Exercises 5

Chapter 2. An Introduction to Software Engineering


2.1
2.2
2.3
2.4
2.5
2.6
2.7

Prologue 12
Compilers and Interpreters 14
Software Components and Packages
Lab: Three Ways to Say Hello 21
Object-Oriented Programming 27
Lab: More Ways to Say Hello 30
Summary 37
Exercises 38

11

20

vii

viii

CONTENTS

Chapter 3. Objects and Classes


3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8

Prologue 44
Case Study: GridWorld 45
Classes 49
Lab: Interacting with Actors 56
Fields, Constructors, and Methods
Inheritance 63
Lab: Random Bugs 68
Summary 70
Exercises 72

43

56

Chapter 4. Algorithms
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8

79

Prologue 80
Properties of Algorithms 81
Iterations 86
Recursion 87
Case Study: Euclids GCF Algorithm 90
Working with Lists 92
Case Study: File Manager 95
Summary 98
Exercises 99

Chapter 5. Java Syntax and Style


5.1
5.2
5.3
5.4
5.5
5.6
5.7

Prologue 108
Using Comments 109
Reserved Words and Programmer-Defined Names
Syntax vs. Style 114
Statements, Blocks, Indentation 118
Lab: Correcting Syntax Errors 119
Summary 121
Exercises 122

107

111

CONTENTS

Chapter 6. Data Types, Variables, and Arithmetic


6.1
6.2
6.3
6.4
6.5
6.6
6.7
6.8
6.9
6.10
6.11

125

Prologue 126
Declaring Fields and Local Variables 128
Primitive Data Types 132
Strings 133
Constants 134
Scope of Variables 137
Arithmetic Expressions 138
Compound Assignment and Increment Operators 142
Converting Numbers and Objects into Strings 144
Lab: Pie Chart 147
Summary 149
Exercises 150

Chapter 7. Boolean Expressions and if-else Statements


7.1
7.2
7.3
7.4
7.5
7.6
7.7
7.8
7.9
7.10
7.11
7.12
7.13

Prologue

157

158

ifelse Statements 160


boolean Data Type 161

Relational Operators 162


Logical Operators 164
Order of Operators 166
Short-Circuit Evaluation 167
ifelseif and Nested ifelse 168
Case Study and Lab: Rolling Dice 173
The switch statement 182
Enumerated Data Types 184
Case Study and Lab: Rolling Dice Concluded
Summary 190
Exercises 192

Chapter 8. Iterative Statements: while, for, do-while


8.1
8.2
8.3
8.4
8.5
8.6
8.7

ix

Prologue 200
The while and for Loops 200
The dowhile Loop 204
return and break in Loops 205
Nested Loops 207
Lab: Perfect Numbers 210
Summary 211
Exercises 212

187

199

CONTENTS

Chapter 9. Implementing Classes and Using Objects


9.1
9.2
9.3
9.4
9.5
9.6
9.7
9.8
9.9
9.10
9.11
9.12
9.13

Prologue 218
Public and Private Features of a Class 222
Constructors 224
References to Objects 228
Defining Methods 229
Calling Methods and Accessing Fields 232
Passing Parameters to Constructors and Methods 235
return Statement 238
Case Study and Lab: Snack Bar 241
Overloaded Methods 246
Static Fields and Methods 249
Case Study: Snack Bar Concluded 254
Summary 256
Exercises 258

Chapter 10. Strings


10.1
10.2
10.3
10.4
10.5
10.6
10.7
10.8
10.9
10.10

265

Prologue 266
Literal Strings 266
String Constructors and Immutability 267
String Methods 270
Formatting Numbers into Strings 277
Extracting Numbers from Strings 280
Character Methods 281
Lab: Lipograms 282
The StringBuffer Class 284
Summary 286
Exercises 287

Chapter 11. Class Hierarchies and Interfaces


11.1
11.2
11.3
11.4
11.5
11.6
11.7
11.8
11.9

217

Prologue 294
Class Hierarchies 296
Abstract Classes 299
Invoking Superclasss Constructors 301
Calling Superclasss Methods 305
Case Study: A GridWorld Dance 307
Polymorphism 315
Interfaces 319
Summary 325
Exercises 328

293

CONTENTS

Chapter 12. Arrays


12.1
12.2
12.3
12.4
12.5
12.6
12.7
12.8

333

Prologue 334
One-Dimensional Arrays 335
Lab: Fortune Teller 339
Two-Dimensional Arrays 340
Case Study and Lab: Chomp 342
Iterations and the For Each Loop 348
Inserting and Removing Elements 350
Summary 352
Exercises 353

Chapter 13. java.util.ArrayList


13.1
13.2
13.3
13.4
13.5
13.6
13.7

Prologue

361

362

ArrayLists Structure 362


ArrayLists Constructors and Methods
ArrayLists Pitfalls 367

365

Lab: Creating an Index for a Document 370


Case Study and Lab: GridWorld Critters 374
Summary 377
Exercises 378

Chapter 14. Searching and Sorting


14.1
14.2
14.3
14.4
14.5
14.6
14.7
14.8
14.9
14.10
14.11

xi

Prologue 382
equals, compareTo, and compare 383
Sequential and Binary Search 389
Lab: Keeping Things in Order 393
Selection Sort 394
Insertion Sort 395
Mergesort 397
Quicksort 400
Lab: Benchmarks 402
java.util.Arrays and java.util.Collections
Summary 406
Exercises 408

381

404

xii

CONTENTS

Chapter 15. Streams and Files


15.1
15.2
15.3
15.4
15.5
15.6

Prologue 412
Pathnames and the java.io.File Class
Reading from a Text File 416
Writing to a Text File 419
Lab: Choosing Words 421
Summary 422
Exercises 423

411
414

Chapter 16. Graphics


16.1
16.2
16.3
16.4
16.5
16.6
16.7
16.8

Prologue 428
paint, paintComponent, and repaint
Coordinates
Colors
Drawing Shapes
Fonts and Text
Case Study and Lab: Pieces of the Puzzle
Summary
Exercises 430

427


Chapter 17. GUI Components and Events


17.1
17.2
17.3
17.4
17.5
17.6
17.7

Prologue 434
Pluggable Look and Feel
Basic Swing Components and Their Events
Layouts
Menus
Case Study and Lab: the Ramblecs Game
Summary
Exercises 436

Chapter 18. Mouse, Keyboard, Sounds, and Images


18.1
18.2
18.3
18.4
18.5
18.6
18.7

Prologue 442
Mouse Events Handling

Keyboard Events Handling
Case Study and Lab: Drawing Editor
Sounds and Images
Case Study and Lab: Ramblecs Concluded
Summary
Exercises 443

433


441

CONTENTS

Chapter 19. Big-O Analysis of Algorithms


19.1
19.2
19.3
19.4

Prologue 446
The Big-O Concept 448
Big-O of Sorting Algorithms
Summary 459
Exercises 461

445

455

Chapter 20. The Java Collections Framework


20.1
20.2
20.3
20.4
20.5
20.6
20.7
20.8
20.9
20.10

Prologue

465

466

Collection and Iterator 468


Lists and ListIterator 472
The Stack Class 478
The Queue Interface 480
The PriorityQueue Class 482

Sets 484
Maps 487
Case Study and Lab: Stock Exchange
Summary 499
Exercises 499

492

Chapter 21. Lists and Iterators


21.1
21.2
21.3
21.4
21.5
21.6
21.7
21.8

xiii

Prologue 508
Singly-Linked List 508
Traversals 514
Lab: Implementing a Singly-Linked List 516
Linked List with a Tail 517
Doubly-Linked List and Circular List 518
Lab: Teletext 521
Summary 522
Exercises 523

507

xiv

CONTENTS

Chapter 22. Stacks and Queues


22.1
22.2
22.3
22.4
22.5
22.6
22.7

Prologue 528
Implementations of Stacks 528
Lab: Browsing 531
The Hardware Stack 533
Implementations of Queues 535
Case Study and Lab: Event-Driven Computations
Summary 542
Exercises 543

527

536

Chapter 23. Recursion Revisited


23.1
23.2
23.3
23.4
23.5
23.6
23.7

Prologue 550
Three Examples 550
When Not to Use Recursion 558
Understanding and Debugging Recursive Methods
Lab: the Tower of Hanoi 564
Case Study and Lab: the Game of Hex 565
Summary 570
Exercises 570

Chapter 24. Binary Trees


24.1
24.2
24.3
24.4
24.5
24.6
24.7
24.8

561

577

Prologue 578
Implementations of Binary Trees 583
Traversals 585
Binary Search Trees 586
A Do-It-Yourself BST 588
Lab: Morse Code 593
Case Study and Lab: Java Messenger 594
Summary 596
Exercises 597

Chapter 25. Lookup Tables and Hashing


25.1
25.2
25.3
25.4
25.5
25.6
25.7

549

Prologue 606
Lookup Tables 606
Lab: Cryptogram Solver 609
Hash Tables 612
java.utils HashSet and HashMap 615
Lab: Search Engine 617
Summary 619
Exercises 620

605

CONTENTS

Chapter 26. Heaps and Priority Queues


26.1
26.2
26.3
26.4
26.5
26.6

Prologue 626
Implementations of Priority Queues 626
Binary Trees: Non-Linked Representation
A Do-It-Yourself Priority Queue 630
Lab: Heapsort 634
Summary 635
Exercises 636

625

628

Chapter 27. Design Patterns


27.1
27.2
27.3
27.4
27.5
27.6
27.7
27.8

639

Prologue 640
Faade

Strategy
Singleton
Decorator
Composite
MVC (Model-View-Controller)
Summary
Exercises 642

Appendices
A.
B.
C.
D.

647

The 17 Bits of Style


Common Syntax Error Messages
GUI Examples Index
The EasyReader, EasyWriter, EasySound,
and EasyDate Classes

Solutions to Selected Exercises


Index of Tables and Figures
Index

xv

657

647

649

Anda mungkin juga menyukai