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
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
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
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
Files,
Graphics,
GUI
15. Files
12. Arrays
16. Graphics
13. ArrayList
17. GUI/Events
14. Searching/Sorting
18. Mouse/Keyboard
25. Hashing
26. Heaps/Priority Qs
Finish
Contents
Preface
xvii
xxi
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
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
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
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
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
Prologue
157
158
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
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
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
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
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
Prologue
361
362
365
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
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
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
Prologue 434
Pluggable Look and Feel
Basic Swing Components and Their Events
Layouts
Menus
Case Study and Lab: the Ramblecs Game
Summary
Exercises 436
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
Prologue 446
The Big-O Concept 448
Big-O of Sorting Algorithms
Summary 459
Exercises 461
445
455
Prologue
465
466
Sets 484
Maps 487
Case Study and Lab: Stock Exchange
Summary 499
Exercises 499
492
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
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
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
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
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
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
639
Prologue 640
Faade
Strategy
Singleton
Decorator
Composite
MVC (Model-View-Controller)
Summary
Exercises 642
Appendices
A.
B.
C.
D.
647
xv
657
647
649