0% found this document useful (0 votes)
206 views

Computing Concepts With Java Essentials: Advanced Placement Computer Science Study Guide

OK

Uploaded by

DoIT Churu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
206 views

Computing Concepts With Java Essentials: Advanced Placement Computer Science Study Guide

OK

Uploaded by

DoIT Churu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Computing Concepts

with Java Essentials


Third Edition

Advanced Placement*
Computer Science Study Guide

FRANCES P. TREES
Drew University

CAY HORSTMANN
San Jose State University

JOHN WILEY & SONS, INC


New York / Chichester / Weinheim / Brisbane / Singapore / Toronto

* 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.

Features of the Study Guide


This guide is organized to help students recognize and master those topics that will be tested on
both the A and AB examinations. Where applicable, each chapter includes:

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.

Expanded Coverage of Topics not Found in the Text


Any topics in the AP CS Topic Outline or the AP Java subsets that are not covered in the text are
presented in this section. In addition, Appendix A includes two additional textbook chapters that
expand the coverage of data structures and algorithms needed for the AP exam.

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.

Things to Remember When Taking the Exam


Each chapter restates important concepts and provides specific suggestions for avoiding common
errors.

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.

Connecting the Detailed Topic Outline with the Text


This listing links the topics presented in the guide to the pages in the text where the same topics
are covered.

Practice Questions (Multiple Choice and Free Response)


A large number of practice questions, modeled after those on the AP exam, are included in each
chapter. The questions use the multiple-choice and free-response formats that students will
encounter on the exam. Answers to the practice questions are provided in Appendix D.

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!

Finally, thanks to Eli for his patience, encouragement and support.

This work is dedicated to my mother.


F.T.

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

1. Introduction 1 3. Introduction to Objects and Classes 17


1.1 What is Advanced Placement? 1 3.1 Using and Constructing Objects 17
1.1.1 Computer Science as an AP 3.2 Object Variables 18
Subject 3.2.1 Importing Classes
1.2 Who Takes AP Courses? 2 3.3 Defining a Class 20
1.2.1 Who Takes AP CS? 3.4 Testing a Class 21
1.3 What is the Difference between AP 3.5 Instance Fields 21
Computer Science A and AB? 3 3.6 Constructors 22
1.3.1 AP CS Course Organization 3.7 Designing the Public Interface of a
1.4 Which Elements of Computer Science Class 22
Are on the AP Exam? 4 3.7.1 Overloading
1.4.1 Format of the AP CS Exam 3.8 Commenting the Public Interface 23
1.5 Strategies for Taking the AP CS 3.9 Specifying the Implementation of a
Exam 4 Class 23
1.5.1 Strategies for Part 1 3.10 Variable Types 25
1.5.2 Strategies for Part 2 3.11 Explicit and Implicit Method
1.5.3 Materials Parameters 26
1.5.4 Last Minute Reminders
1.6 How are the Exams Graded? 6
1.6.1 Special Recognition for Doing 4. Fundamental Data Types 35
Well 4.1 Number Types 35
1.7 How Should this Guide be Used to 4.2 Assignment 36
Prepare for the AP CS Exam? 7 4.3 Constants 37
1.8 Where Can I Get More Information? 7 4.4 Arithmetic and Mathematical
1.8.1 Where Can I Get the Text Functions 38
Ancillaries? 4.4.1 Calling Static Methods
1.9 Are There Other Suggestions for 4.4.2 Type Conversion
Success in AP CS? 8 4.5 Strings 42
4.6 Comparing Primitive Types and
Objects 44
2. Hardware and Software 9
2.1 Hardware Components 9
2.1.1 The Anatomy of a Computer 5. Decisions 55
2.2 System Software 10 5.1 The if Statement 55
2.3 Types of Systems 10 5.2 Comparing Values 56
2.4 A Simple Program 10 5.2.1 The null Reference
2.4.1 Escape Sequences 5.2.2 Dangling else
2.4.2 Java Comments 5.3 Using Boolean Expressions 61
2.5 Errors 11 5.3.1 Short Circuit Evaluation
5.3.2 Using Boolean Variables

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

9. Interfaces and Polymorphism 121 14. Recursion 221


9.1 Developing Reusable Solutions 121 14.1 Thinking Recursively 221
9.2 Converting Between Types 127 14.2 Permutations 223
9.2.1 Constants in Interfaces 14.3 Tracing Through Recursive
9.3 Polymorphism 128 Methods 225
14.4 Recursive Helper Methods and Mutual
Recursion 226
10. Inheritance 137 14.5 A Word About Efficiency 226
10.1 Introduction to Inheritance 137
10.2 Inheritance Hierarchies 141
10.3 Inheriting Instance Fields and 15. Introduction to Sorting and Searching 235
Methods 143 15.1 Selection Sort 235
10.4 Subclass Construction 143 15.1.1 Analyzing the Performance of
10.5 Converting from Subclasses to the Selection Sort Algorithm
Superclasses 144 15.2 Insertion Sort 238
10.6 Abstract Classes 147 15.2.1 Analyzing the Performance of
10.7 Access Control 148 the Insertion Sort Algorithm

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

You might also like