Computing Concepts With Java Essentials: Advanced Placement Computer Science Study Guide
Computing Concepts With Java Essentials: Advanced Placement Computer Science Study Guide
Advanced Placement*
Computer Science Study Guide
FRANCES P. TREES
Drew University
CAY HORSTMANN
San Jose State University
* AP and Advanced Placement are registered trademarks of the College Entrance Examination Board, which was not involved in
the production of and does not endorse this product.
ACQUISITIONS EDITOR Paul Crockett
MARKETING MANAGER Katherine Hepburn
PROJECT MANAGER Cindy Johnson
EDITORIAL ASSISTANT Simon Durkin
COVER DESIGN Susan Cyr / Harry Nolan
This book was set in Times New Roman and printed and bound by J&M Reproductions. The
cover was printed by J&M Reproductions.
This book is printed on acid-free paper.
The paper in this book was manufactured by a mill whose forest management programs include
sustained-yield harvesting of its timberlands. Sustained-yield harvesting principles ensure that
the numbers of trees cut each year does not exceed the amount of new growth.
* AP and Advanced Placement are registered trademarks of the College Entrance Examination
Board, which was not involved in the production of and does not endorse this product.
Copyright 2004 John Wiley & Sons, Inc. 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, scanning or otherwise,
except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without
either the prior written permission of the Publisher, or authorization through payment of the
appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA
01923, (978) 750-8400, fax (978) 750-4470. Requests to the Publisher for permission should be
addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken,
NJ 07030, (201) 748-6011, fax (201) 748-6008, E-Mail: [email protected]. To order
books or for customer service, please call 1800CALLWILEY (2255945)
ISBN 0-471-44939-3
Printed in the United States of America
10 9 8 7 6 5 4 3 2 1
ii
PREFACE
This Study Guide is designed to assist high school students preparing for the AP Computer
Science Examination. It is organized and designed to accompany Computing Concepts with Java
Essentials, 3rd. ed., and Big Java, both by Cay Horstmann and published by John Wiley & Sons,
Inc.
To the Student
Advanced Placement Computer Science is a college level course. As a student in an AP course,
you are expected to understand college level material and to read college level textbooks.
Because these texts may be used in multiple college courses, they contain material that is not
tested on the AP Computer Science (CS) Examinations. This guide identifies and stresses the
content that is tested on the AP CS Exams. This does not imply that you should skip the
additional material in the text. Your teacher may include the additional material in your course
and hold you responsible for that material. Some of the material that is not tested on the exam is
extremely useful and may be included in your course. By following this guide, you will be able
to focus on the topics included in the AP CS 2003-2004 Topic Outline and the elements of the
AP Java subset that will be tested on the exam. Because the guide is designed for students taking
either the A or the AB Computer Science exam, topics covered on the AB Exam are marked AB
only. If you are participating in an AP CS A course, this should not discourage you from
reading these sections; it merely indicates that those topics will be tested on the AB exam and not
the A Exam.
Topic Summary
This section parallels the material in the textbook, adding additional examples and explanation of
topics presented in the text. Students will find special emphasis here on topics that are likely to
be tested.
iii
Topics That are Useful but not Tested
This section alerts students to sections of the textbook that present useful features of the Java
language. Although these sections are not required for the AP exam, they address features of
Java programs that students are likely to encounter, or that they will need to progress in their
programming ability.
Key Words
Vocabulary used in the chapter is listed alphabetically here for student review and reference.
Each key word is accompanied by the page number(s) where it is defined and/or discussed in the
text.
AB Examination Topics
Any sections (and practice questions) that cover topics that will not be tested on the Computer
Science A exam are clearly marked AB only. Students who are preparing for the AB exam
should be sure to complete these sections.
Web Resources
The AP Computer Science Topic Outline and Quick Reference Guides that students may use
during the AP exam are available from the College Boards Web site at
http://www.collegeboard.com/ap/students/compsci/. The Quick Reference Guides list the classes,
interfaces, and methods contained in the Java subset covered by the AP exam. The AP Computer
Science Topic Outline and the AP testable Java subsets may be revised from time to time, so you
are encouraged to check for the most current version of these documents in the AP Computer
Science Course Description on the College Board site.
Web sites associated with this guide and with Computing Concepts with Java Essentials, 3rd.
ed., and Big Java are another important resource for users of this guide. Cay Horstmanns site,
http://www.horstmann.com, contains an expansion of Chapter 19, Help with Common
Compilers, that provides installation tips and getting started instructions for the most commonly
used Java compilers and environments. You will also find source code for programs in the text(s)
and Appendix A, solutions to selected exercises in the text(s), frequently asked questions, a
programming style guide, and more.
iv
Many of these same items are also available from the Wiley Web site at
http://www.wiley.com/college/horstmann, plus instructor resources not available to students.
Acknowledgments
It has been a sincere pleasure working with Cay Horstmann, master computer scientist and
teacher, in the creation of this guide. I will always value this experience.
Many thanks to Paul Crockett for providing the opportunity for me to be involved in this project
and to Cindy Johnson and her staff at Publishing Services for their hard work, support, and
patience throughout. I am extremely grateful to Joe Kmoch, Washington High School, whose
review offered many valuable comments and suggestions. I also am grateful to Judy Hromcik,
Arlington High School, and David Levine, St. Bonaventure University, for their suggestions and
encouragement.
My energy and enthusiasm for AP Computer Science is constantly renewed by the many AP CS
teachers that I work with at AP CS workshops and, of course, by my AP CS students, some of
whom are now AP CS teachers!
v
vi
CONTENTS IN BRIEF
1. Introduction 1
2. Hardware and Software (1)* 9
3. Introduction to Objects and Classes (2) 17
4. Fundamental Data Types (3) 35
5. Decisions (5) 55
6. Iteration (6) 73
7. Designing Classes (7) 93
8. Testing and Debugging (8) 113
9. Interfaces and Polymorphism (9) 121
10. Inheritance (11) 137
11. Array Lists and Arrays (13) 163
12. Exception Handling (14) 195
13. System Design (16) 207
14. Recursion (17) 221
15. Introduction to Sorting and Searching (18) 235
16. Introduction to Data Structures (AB) (19) 263
17. Advanced Data Structures (AB) (20, A-20, and A-20A) 303
18. Information about the Case Study 355
19. Help with Common Compilers 359
A. Advanced Data Structures and Algorithms (Expanded Coverage) 368
B. Exercise Solutions 437
*
Parenthesis indicates the corresponding chapter in Computing Concepts with Java Essentials, 3rd ed., and Big Java.
vii
CONTENTS
viii
6. Iteration 73 10.8 Object: The Cosmic Superclass 148
6.1 while Loops 73 10.8.1 Overriding the toString
6.2 for Loops 74 Method
6.3 Nested Loops 76 10.8.2 Overriding the equals
6.4 Off-by-1 Errors 77 Method
6.5 Random Numbers and Simulations 78
6.6 Loop Invariants (AB only) 79
11. Array Lists and Arrays 163
11.1 Array Lists 163
7. Designing Classes 93 11.1.1 A Word about Efficiency
7.1 Choosing Classes 93 11.1.2 Wrapper Classes
7.2 Cohesion and Coupling 94 11.2 Arrays 169
7.3 Accessor and Mutator Methods 95 11.2.1 One-Dimensional Arrays
7.4 Parameter Passing 95 11.2.2 Another Word about
7.5 Side Effects 98 Efficiency
7.6 Preconditions and Postconditions 98 11.2.3 Two-Dimensional Arrays (AB)
7.6.1 Class Invariants (AB)
7.7 Static Methods 100
7.8 Static Fields 100 12. Exception Handling 195
7.9 Scope 101 12.1 Introduction to Exceptions 195
7.9.1 Scope of Local Variables 12.2 Understanding Unchecked
7.9.2 Scope of Class Members Exceptions 196
7.10 Initializing Variables 101 12.3 Throwing Exceptions (AB only) 199
7.11 Packages 102
13. System Design 207
8. Testing and Debugging 113 13.1 Designing Software Systems 207
8.1 Unit Tests 113 13.2 The Software Life Cycle 208
8.2 Test Case Evaluation 114 13.3 Discovering Classes 208
8.3 Program Traces and Assertions 114 13.4 Relationships between Classes 208
8.4 The Debugger 115 13.5 Responsibilities of a Class 209
ix
15.3 Merge Sort 241 18. Information about the Case Study 355
15.3.1 Analyzing the Performance of 18.1 What Is a Case Study? 355
the Merge Sort Algorithm 18.2 Why Are Case Studies Included in AP
15.4 Quicksort (AB only) 243 CS? 356
15.4.1 Analyzing the Performance of 18.3 How Do I Use the Case Study During
the Quicksort Algorithm the School Year? 356
15.5 Comparing O(n 2) and O(n log(n)) (AB 18.4 How Do I Prepare for the Case Study
only) 245 Questions on the AP Exam? 356
15.6 Sequential Search 246 18.5 Where Do I Get the Case Study? 357
15.6.1 Analyzing the Performance of
the Sequential Search
Algorithm (AB only) 19. Help with Common Compilers 359
15.7 Binary Search 247 19.1 Java 2 Platform 359
15.7.1 Analyzing the Performance of 19.2 Command Line Programming 360
the Binary Search Algorithm 19.3 Common Integrated Development
15.8 Comparing O(n) and O(log(n)) (AB Environments 361
only) 248 19.3.1 BlueJ
19.3.2 Eclipse
19.3.3 JCreator
16. Introduction to Data Structures (AB) 263 19.3.4 JJ
16.1 Using Linked Lists 263 19.3.5 Metrowerks CodeWarrior
16.2 Implementing Linked Lists 268 19.3.6 NetBeans
16.2.1 Doubly Linked Lists
16.2.2 Circularly Linked Lists
16.2.3 Header and Trailer Nodes
16.3 Abstract and Concrete Data Types280 Appendices
16.4 Stacks 281
16.5 Queues 284 A. Advanced Data Structures and
Algorithms (Expanded Coverage) 368
17. Advanced Data Structures (AB) 303
17.1 Sets 303 B. Exercise Solutions 437
17.2 Maps 304
17.3 Hash Tables 306
17.3.1 Using HashSet
17.3.2 Implementing HashSet
17.3.3 Using HashMap
17.3.4 Implementing HashMap
17.4 Computing Hash Codes 314
17.5 Binary Search Trees 314
17.5.1 Binary Tree Traversals
17.5.2 Searching a Binary Search
Tree
17.5.3 Removing a Node from a
Binary Search Tree
17.6 Using Tree Sets and Tree Maps 322
17.7 Priority Queues 330
17.8 Heaps 333
17.8.1 Implementing a Heap
17.8.2 The Heapsort Algorithm
17.9 Choosing a Container 335