Home page of Holt Software Associates  | Home page of the Turing Programming Language, the fastest way to teach programming concepts  | Home page of Holt Software's Java products  | Home page of Ready to Program with Java(tm) Technology, a Java development environment designed for education  | Information about Holt Software's courses for teachers  | Information about how to contact Holt Software  | Information about how students can purchase Holt Software's books and software  | Information about how schools and bookstores can purchase Holt Software's books and software

Other Holt Software Books    [Graphical Version]  |  [Printer Friendly Version]

[Cover of Book]

Java for
AP* Computer Science

*AP is a registered trademark of the College Board, which is not
involved in the production, and does not endorse this book

Book Information

Title: Java for AP Computer Science (First Edition)
Authors: Tom West and Christine Stephenson
ISBN: 0-921598-51-3
Publisher: Holt Software Associates Inc.
Binding: Hardcover
Pages: 950 pgs + Appendices
Price (US): Bookstores & Schools: US$49.95    Retail: $62.50
Price (Cdn): Bookstores & Schools: Cdn$64.95    Retail: $81.25
Ordering Information: For bookstores and schools, click here for ordering information.
For individuals click here for ordering information.
Errata: Errors found in the current printing
Program Examples: Found on CD included with book
Solutions Manual: Not yet available

Book Description

Perhaps the most complete book covering every topic of the Java AP Curriculum, Java for AP Computer Science provides students with a thorough, yet readable, coverage of the topics in both the A and AB Computer Science curriculum.

Java for AP Computer Science is designed to ensure that students obtain a more in-depth knowledge of Java. It provides detailed tracing examples for small and large programs to help students understand how Java constructs operate. The text also provides detailed analysis, design, and implementation of several large programs to illustrate programming design principles.

Java for AP Computer Science uses pre-built classes to allow students to use graphics in a pedagogically useful manner. These include classes to create patterns using "PaintBugs", create smooth animations, visualize different sorting algorithms, and create graphical representations of trees.

Chapters also include complete sets of exercise broken down by subsection to allow partial coverage of chapters. Many chapters also include self-quizzes with answers and explanations as to how the answers were achieved. Finally, most chapters also include additional technical details and sidebars emphasizing critical concepts.

Two sample chapters in PDF format (Chapter 8: Loops and Chapter 14: Inheritance illustrate the style in which the book is written. The Table of Contents and the Index provide you with a good idea of how the topics are covered in the book.

The book is also produced with the realization of tight budgets in the educational world. The book is currently priced as a 950+ page 8"x10" hardcover at US$49.95 (CDN$64.95) including shipping.

Java for AP Computer Science comes with a CD and license to use Holt Software's Ready to Program with Java Technology, a full Java development environment designed specifically for education containing everything you need to write Java programs. The CD also contains all the example classes found in the book. Note that while the book does provide information on the Ready environment (installation and how-to-use guides in the appendices), the text and all sample classes do not require the use of Ready and can be used with any Java IDE (version 1.3 or later).


Correlations with AP Topics

Holt Software will soon provide a correlation between all the topics listed for AP Computer Science A and AB, and their coverage in the textbook. This document should appear shortly.

Get Acrobat Reader

Errata in Java for AP Computer Science

All these will be corrected in the next printing.
PageErrata
267The title of Section 7.9.1 should be

7.9.1 Answer - If Constructs (pg 240)

314In question d, there is a semicolon after the for statement. Thus the output should simply be '5'. Alternatively, you could correct the question on page 292 by removing the semicolon and keep the answer, depending on whether you like trick questions.
374The table at the bottom of page 374 is in error.
  • -1 of s1 is less than s2
  • 0 of s1 is the same as s2
  • 1 of s1 is greater than s2
should read
  • Less than 0 if s1 is less than s2
  • 0 if s1 is the same as s2
  • Greater than 0 if s1 is greater than s2
Note that all the examples and description in the book use only the sign of the return value, so no other changes are necessary.
648In question 8, the two calls to exer7 should read exer8 and the call
     exer8 (150)
should read
     exer8 (150, 2)
672In the table on page 672, the second column, second row reads
     Determine if first (= 0) is greater than last (=12). The comparison is false
In this case, last is 15, not 12
731In the output of the InsertionSort program, the insertLoc and boundary columns are reversed.
788,789In the DoubleListNode class listing, all references to ListNode should refer to DoubleListNode

Preface to Java for AP Computer Science (First Edition)

This textbook, Java for AP Computer Science, is intended for use in a Grade 11 or 12 computer science course for students planning to write either the Advanced Placement A or AB Computer Science exam. It covers all the topics and language features required by both exams as well as a variety of elements of Java outside the AP mandated requisites.

The text emphasizes an understanding of the fundamental concepts of computer science. The book also endeavors to provide enough supporting detail about the actual implementation of these concepts in Java that students understand not only the behavior of the implementation, but also why the behavior occurs.

Java for AP Computer Science uses an "Objects Early" approach rather than an "Objects First" approach. The general concept of object oriented programming is introduced before the students write their first line of code. The creation of objects in student programs, however, takes place after an introduction to the basic concepts of programming. We believe this "Objects Early" approach allows students to understand exactly why programs are written in the way they are, rather than requiring them to "just do it this way for now".

Because the AP Java language requirements are designed to facilitate testing, they do not include all the elements of Java necessary to write functional programs. We have therefore included elements of Java that allow students to write meaningful programs.

Since the AP can serve as an exemption for a first college level course in computer science, this text also includes elements of the Java language that students in university are expected to be familiar with. These are covered so that students will have some basic familiarity with them, although much less emphasis is placed upon them in exercises and future chapters.

Lastly, some elements of Java beyond the AP are examined to make the course more rewarding for the students. By learning simple elements of Java to make programs appear more polished and produce graphically pleasing results, students achieve gratification while gaining further experience in the use of object oriented programming.

More Details

The sections marked More Details contain information that, while not part of the AP curriculum, has shown itself to be of interest to many students learning the language. There is no need to teach the materials in the More Details section. We hope, however, that many of the questions asked of teachers by more advanced students will be answered in these sections.

Tracing and Debugging

It is the opinion of the authors that it is not possible to consistently write correct programs unless there is a clear understanding of exactly how programs operate. To this end, the book contains a number of examples of stepping and tracing both for purposes of illustrating constructs in the Java language, and to familiarize students with the idea of tracing execution of their own programs.

Java for AP Computer Science also devotes a section to the topic of debugging techniques. It has been the authors' experience that even quite competent students are often stymied when their programs fail to operate in the manner that they expect. By providing a technique for systematically finding bugs in their programs, the text endeavors to provide students with a clear course of action when they inevitably discover a bug.

Helper Classes

This text uses a number of helper classes to provide functionality to the students that is beyond the level specified by the AP exam. Specifically, helper classes are used to allow simple input of integers into student programs, to help format output, and to give students access to graphics without requiring them to learn a large number of details about window creation, applet execution, and so on. Lastly, some helper classes are provided to allow students to visualize sorting routines, recursion in the Towers of Hanoi, and the creation of trees. These classes are all provided as part of the hsa package and can be used with any development environment supporting Java 1.3 or greater.

Graphics in Java for AP Computer Science

One of the challenges when teaching computer science (especially with a curriculum as rigorous as the AP) is to make the course interesting enough to attract students. There are several ways this can be accomplished. The choice of language, Java, is one attractive aspect of the course since students deem Java "industrially relevant" due to the media attention it has received (witness the large number of trade books about Java available in a bookstore).

While Java has facilities for creating elaborate graphical user interfaces, they are less than ideal from a pedagogical perspective (being designed for maximum flexibility with highly experienced programmers in mind rather than student programmers). Java for AP Computer Science therefore provides a number of classes that students will use to produce quite sophisticated graphical animations while not sacrificing valuable learning time to endless details about the intricacies of the Java class library.

The AP Computer Science Subset

The College Board AP Computer Science curriculum specifies a subset of the Java language. It is important to understand how this subset is used, and as importantly, how it is not used.

  1. The College Board guarantees that a student need know only the AP subset to be able to answer all questions on the AP Computer Science exam.
  2. Students answers do not have to be restricted to the AP subset of Java. Answers may use any part of the Java 2 version 1.2 language.
This text covers several language features that are outside of the AP Java subset (such as break or switch statements) that may make it easier for students to answer questions on the AP exam. Students should not be afraid to use these features where applicable on the AP Computer Science exam.

As of the writing of this preface (July 2003), the AP has no formal policy on handling answers containing features found in later versions of Java beyond version 1.2. Historically, however, there have usually been only minor deductions if the answer was otherwise correct and understood by the evaluator.

The Case Study

Part of the AP Computer Science exam is based upon a case study. The current case study at the time of the writing of this book (July 2003) is The Marine Biology case study. The case study does tend to change somewhat from year to year and is thus not included in this text (it is included with the Ready to Program environment). A number of web sites cover integrating the case study into the AP curriculum. The most popular site is run by Chris Nevison, Chief Reader for AP Computer Science, and is found at

http://cs.colgate.edu/APCS/

Ready To Program

Java for AP Computer Science can be used with any general purpose Java development environment. However, in most cases it comes bundled with Ready to Program with Java™ Technology, a Java development environment designed explicitly for the student use. Ready is designed specifically to allow students to write their first simple program within minutes of their introduction to the environment. The text also provides small sub-sections of the book labeled Ready Tips indicating where a particular aspect of the environment is particularly useful in relation to a specific concept.

The use of the Ready to Program software is governed by the license found in Appendix I, which allows the use of the software in a course in connection with this book. A general site license is available for purchase from Holt Software that includes

For more information contact Holt Software or the check the web site at

http://www.holtsoft.com/ready

Teacher's Manual

For information on the availability of a teacher's manual for this text, containing answers to exercises and additional materials that did not fit into the book, contact Holt Software, or check the web site at

http://www.holtsoft.com/books/solutions_manuals.html

Errata

Almost every textbook has errors discovered after publication. While we endeavor to correct all outstanding errors with each printing, any errors reported to us will be listed on our web page for this book found at

http://www.holtsoft.com/books/java_for_ap.html

Contacting the Authors

The authors are happy to receive comments, criticism, suggestions, error reports, and any other communication regarding the book. The author's email addresses are

Tom West west@hsa.on.ca

Christine Stephenson chris@hsa.on.ca

Overview

This book covers the material in the AP Computer Science curriculum in twenty-one chapters.

The list of chapter titles outlines the arrangement of materials

  1. Computer Hardware and Software
  2. Computer Programming
  3. Variables and Data Types
  4. Expressions
  5. Writing Java Programs
  6. Objects and Methods
  7. Selection Constructs
  8. Loops
  9. Top Down Program Design
  10. Strings
  11. Methods
  12. Objects and Classes
  13. Arrays
  14. Inheritance and Interfaces
  15. Recursion
  16. Searching
  17. Sorting
  18. Linked Lists (AB only)
  19. Trees (AB only)
  20. Data Structures in the Java Class Library (AB only)
  21. Stacks, Queues, and Priority Queues (AB only)

Chapter 1 provides an overview of types of computers, basic hardware design, computer software, system usage, and networks. It also explores social issues involved in computing such as privacy, piracy, intellectual property, and the social impact of computers. It ends with a section on proper personal use of computers. Note that material after this chapter is not dependent on this chapter. You may choose to cover the material in this chapter throughout the course when it may be more appropriate.

Chapter 2 describes the fundamentals of writing and running programs on computers. It covers such elements as the differences between compilers, interpreters, and compiler-interpreters and an overview of different programming languages. It concludes with a non-language specific introduction to object oriented programming.

Chapter 3 introduces Java programming. It details the format of a Java program, covers the concepts of data types, and provides an introduction to each of the Java primitive data types. It then introduces the assignment statement. Student understanding is verified with a pair of self-tests.

Chapter 4 presents expressions in Java. Programs perform calculations and expressions are fundamental to calculations. By dealing explicitly with expressions, the text also introduces the ideas of precision and exactness that are required to successfully write computer programs.

Chapter 5 provides information on program input and output and program design. The sections on input and output cover the use of System.in and System.out as well as the use of the hsa.Stdin class (for simple input). The chapter then presents the development of two programs, QuadraticInput and TermDeposit, using the design techniques covered in Chapter 2. The chapter concludes with a section on programming style.

Chapter 6 discusses the creation of objects and the use of methods. It does so by introducing several classes including the AddressBook class and the PaintBug class. The PaintBug class allows students to use graphics in a pedagogically useful context without overwhelming complexity. The chapter also introduces class methods. The chapter concludes with an introduction to formatted text output, as students are often eager to have the output of their programs appear as professional as possible.

Chapter 7 introduces selection constructs including if statements, if-else structures, multi-way if structures, and the switch statement. It also covers the nesting of selection constructs and different indenting styles.

Chapter 8 presents looping constructs. Both counted loops (for loops) and conditional loops (while and do-while loops) are covered. The chapter also details the break statement, as this allows for a more natural program design for some algorithms.

Chapter 9 formally introduces top-down design using two examples: a phone book and a mortgage program. The chapter also covers the mechanics of testing programs including full coverage and full path testing.

Chapter 10 covers the details of the String class. While the String class has been used throughout previous chapters, this chapter introduces a number of the methods that can be used in conjunction with String objects and provides numerous programs that allow students to practice with the constructs taught so far. The chapter also covers the StringBuffer and StringTokenizer classes.

Chapter 11 provides the details of writing methods. The book specifically uses class methods in this chapter, in order to allow students to focus on one concept at a time. The text covers function-type methods and procedure-type methods along with parameters. It also briefly introduces class variables along with consideration of their use.

Chapter 12 deals with the creation of objects and classes. It provides the full syntactical detail necessary to write a class with fields and methods. It introduces constructors and helper methods (methods that are not part of the interface).

Chapter 13 covers the declaration, creation, and use of arrays. It presents a program that uses an array to control a series of bank accounts and a class that uses arrays to implement a phone book. The chapter details the concept of related arrays and operations on arrays such as inserting and removing an element. The chapter also covers two-dimensional arrays.

Chapter 14 deals with inheritance, polymorphism, abstract classes, and class hierarchies. It does this using classes that implement animation, allowing students to learn the concepts while producing graphically interesting programs.

Chapter 15 discusses recursion, including mutual recursion. It uses the Towers of Hanoi, an expression evaluator, and the "Dragon Curve" to illustrate recursion both theoretically and graphically.

Chapter 16 covers linear search, binary search, and hashing. The concept of key-value storage is also discussed. The chapter also introduces the concept of genericity and the Object class, as well as Java's implementation of multiple inheritance (interfaces). The chapter also introduces the ArrayList class.

Chapter 17 covers sorting including selection sort, insertion sort, merge sort, and quicksort. The chapter uses a separate class to implement common elements of each sort, allowing concentration on the specific characteristics of each sort. The chapter concludes by visually displaying the different sorting process using a helper class to allow students to better understand how the sorts work.

Chapter 18 details linked lists including the different variants of linked lists and concepts like insertion, deletion, and using interators to list elements in the list. The chapter also shows the creation of a larger multiple class program consisting of five separate classes. The chapter concludes with an explanation of the use of chaining in hash tables and a larger program demonstrating the use of chaining.

Chapter 19 covers the use of trees. This chapter demonstrates several algorithms using trees including different traversal algorithms. The chapter includes an implementation of a Binary Search Tree and ends with an explanation of the heap sort. The chapter uses a helper class that visually depicts trees in a window to help students understand how trees can be created.

Chapter 20 introduces the different data structures found in the Java class library that are part of the AP computer science curriculum. This includes the List, Iterator, and ListIterator interfaces, the ArrayList and LinkedList classes, the Set and Map interfaces, and the HashSet, TreeSet, HashMap, and TreeMap classes. Each class is illustrated with a sample program. The chapter concludes with an implementation of a library automation system using several of the classes introduced in this chapter.

Chapter 21 explains the stack, queue, and priority queue data structures. Each data structure is demonstrated using one or two examples. The chapter introduces students to the concept of using the computer to simulate real-life activities by including a simulation of the operations of a bank.

Acknowledgements

The authors would like to gratefully acknowledge the assistance of our set of readers, who volunteered their time to find errors, suggest improvements, and generally help increase the quality of the book from its first iteration to what you now hold in your hand. Without their help, this book would be much diminished.

The readers, in alphabetical order are:

  1. Chris Darcy Royal St. George's College
  2. Jack De Value FreeHold High School
  3. Lawrence Demello St. Clement's School
  4. Michael Devoy Waterloo Catholic District School Board
  5. Steve Gannaway Windham High School
  6. Rose Hoffmann Catholic Memorial High School
  7. Ron Ouwerkerk St. George's School
  8. Roy Parteno St. Marguerite d'Youville
  9. Barbara Quintero Miami Senior High School
  10. Ann Shen Bishop Strachan School
The authors would also like to acknowledge Catharine Kozuch, who did the final page checks, liaised with U of T Press, and generally does what is necessary to make certain that teachers and students actually get a book in their hands.

Table of Contents of Java for AP Computer Science

         
PREFACEXVIII
ACKNOWLEDGEMENTSXXVI
1COMPUTER HARDWARE AND SOFTWARE1
1.0Introduction1
1.1Types of Computers1
1.1.1Supercomputers2
1.1.2Mainframes2
1.1.3Minicomputers (Workstations)2
1.1.4Microcomputers (Personal Computers)3
1.1.5Embedded Computers3
1.2Computer Hardware Design3
1.2.1CPU4
1.2.2Primary Memory (Main Memory)6
1.2.3Secondary Memory (Storage Devices)7
1.2.4Peripherals8
1.3Computer Software10
1.3.1Operating Systems10
1.3.2Applications11
1.4Computer Systems Usage12
1.4.1Single User Systems12
1.4.2Multi-user Systems13
1.5Networks14
1.5.1Client-Server Architecture14
1.5.2Peer-to-Peer Architecture15
1.5.3The Internet16
1.6Social Issues in Computing17
1.6.1Software Piracy17
1.6.2Privacy18
1.6.3Social Impact20
1.6.4Intellectual Property22
1.7Responsible Computer Usage23
1.7.1Use of Computer in Schools23
1.7.2Use of Computers on the Internet24
1.7.3Safety from Viruses24
1.7.4Safe Computer Use26
1.8Technical Terms26
1.9What You Should Know27
1.10Exercises28
2COMPUTER PROGRAMMING29
2.0Introduction29
2.1What is Computer Programming?29
2.2Creating Programs30
2.2.1Compilers31
2.2.2Interpreters32
2.2.3Compiler-Interpreters32
2.3Program Errors34
2.3.1Compile-time Errors34
2.3.2Run-time Errors34
2.3.3Logic Error34
2.4Programming Languages35
2.4.1Low and High Level Languages36
2.4.2Older Programming Languages37
2.4.3The C Programming Language37
2.4.4C++38
2.4.5Java39
2.4.6Using Programming Languages40
2.5Program Life Cycle41
2.6Program Design Techniques44
2.7Object Oriented Programming45
2.7.1Objects in Real Life47
2.7.2Objects in Programs48
2.7.3Objects Containing Objects50
2.7.4Classes50
2.7.5Object Oriented Analysis51
2.8Types of Java Programs53
2.8.1Applications53
2.8.2Applets55
2.9Technical Terms56
2.10What You Should Know57
2.11Exercises58
3JAVA PROGRAMS AND DATA TYPES61
3.0Introduction61
3.1General Forms of Java Programs62
3.1.1Initial Comment62
3.1.2Import Statements63
3.1.3Class Declaration63
3.1.4Main Method Declaration64
3.1.5Main Method Contents67
3.1.6Close of Main Method68
3.1.7Close of Class68
3.1.8A Template for an Application68
3.1.9Case Sensitivity69
3.1.10Exercises69
3.2Variables and Data Types70
3.2.1Variables70
3.2.2Data Types72
3.2.3int72
3.2.4double73
3.2.5boolean75
3.2.6char75
3.2.7byte, short, long, float76
3.2.8Examples of Variables and Data Types77
3.2.9Quiz Yourself - Variables and Data Types78
3.3Assignment Statements78
3.3.1Basic Assignment Statement78
3.3.2Assigning Another Value80
3.3.3Assigning From a Variable81
3.3.4Declaration and Assignment84
3.3.5Constants84
3.3.6Exponential Notation85
3.3.7Uninitialized Variables85
3.3.8Data Type Conversion (Casting)86
3.3.9Rounding87
3.3.10Quiz Yourself - Data Conversion and Rounding88
3.4Compile-Time Errors in Java89
3.4.1Example Errors89
3.4.2Finding and Fixing Compile-Time Errors91
3.5Technical Terms92
3.6What You Should Know92
3.7Quiz Answers93
3.7.1Answers - Variables and Data Types (pg. 78)93
3.7.2Answers - Data Conversion and Rounding (pg. 88)94
3.8Exercises96
4EXPRESSIONS103
4.0Introduction103
4.1Mathematical Expressions103
4.1.1Mathematical Operators104
4.1.2Operator Precedence109
4.1.3Complex Expressions110
4.1.4Compound Assignment Operators113
4.1.5Autoincrement and Autodecrement115
4.1.6Functions in Mathematical Expressions116
4.1.7Examples of Mathematical Expressions117
4.1.8Quiz Yourself - Mathematical Expressions117
4.2String Variables118
4.2.1How Strings are Stored118
4.2.2Assigning Strings119
4.2.3Special Characters120
4.2.4String Expressions121
4.2.5Concatenating a String with a Non-String122
4.2.6Examples of Using Strings124
4.2.7Quiz Yourself - String Variables125
4.3Boolean Expressions125
4.3.1Logical Operators127
4.3.2Short Circuit Evaluation129
4.3.3Examples of Boolean Expressions130
4.3.4Quiz Yourself - Boolean Expressions131
4.4Mathematical Functions131
4.4.1Rounding Up and Down131
4.4.2Trigonometry134
4.4.3Square Root135
4.4.4Absolute Value136
4.4.5Exponentiation137
4.4.6Minimum and Maximum137
4.4.7Random Numbers138
4.4.8Table of Functions139
4.5Run-Time Errors in Java140
4.5.1Finding and Fixing Run-Time Errors141
4.5.2Mathematical Operations and NaN141
4.6Technical Terms141
4.7What You Should Know142
4.8Quiz Answers142
4.8.1Answers - Mathematical Expressions (pg. 117)142
4.8.2Answers - String Variables (pg. 125)144
4.8.3Answers - Boolean Expressions (pg. 131)146
4.9Exercises148
5WRITING JAVA PROGRAMS157
5.0Introduction157
5.1Text Output157
5.1.1The System Console157
5.1.2System.out Object159
5.1.3System Console Cursor160
5.2Text Input162
5.2.1System.in (Optional)162
5.2.2hsa.Stdin165
5.3Writing Programs170
5.3.1Quadratic Equation Example Program170
5.3.2Term Deposit Example Program175
5.4Programming Style180
5.4.1Comments181
5.4.2Style Guidelines183
5.5Technical Terms184
5.6What You Should Know184
5.7Exercises185
6USING OBJECTS AND METHODS189
6.0Introduction189
6.1Creating Objects190
6.1.1Review190
6.1.2Java Classes190
6.1.3The import Statement191
6.1.4Object Variables193
6.1.5Declaring Object Variables196
6.1.6Instantiation (Creating Objects)196
6.2Using Methods198
6.2.1Review198
6.2.2Syntax of a Method Call198
6.2.3Phone Book Example199
6.2.4String Class Example200
6.2.5Random Class Example200
6.2.6Signatures and Interfaces201
6.3Class or Static Methods212
6.3.1Signature Appearance213
6.3.2Examples of Class Methods - The Math Class213
6.3.3Examples of Class Methods - The PaintBug Class215
6.4Formatted Text Output216
6.4.1Formatting Using hsa.Stdout217
6.4.2Formatting Numbers Using NumberFormat223
6.4.3Formatting Using Date and DateFormat229
6.5Technical Terms232
6.6What You Should Know232
6.7Exercises233
7SELECTION CONSTRUCTS235
7.0Introduction235
7.1If-Then Structure235
7.1.1Conditional Execution235
7.1.2If-Then in Java236
7.1.3Visualizing Flow of Execution237
7.1.4More About the If Clause239
7.1.5Another Example of the If-Then Construct239
7.1.6Quiz Yourself - If Construct240
7.2If-Then-Else Structure241
7.2.1Java242
7.2.2More About the Else Clause243
7.2.3Another Example243
7.2.4Quiz Yourself - If-Else Construct246
7.3Nesting If247
7.3.1Example of Nested If247
7.3.2Alternate Structure252
7.3.3Another Example of Nested If254
7.3.4Quiz Yourself - Nested If256
7.4Indentation Styles258
7.5Switch Construct259
7.5.1The Break Statement263
7.5.2Characters in a Switch Construct264
7.5.3Switch Construct Versus Multi-way If Construct265
7.6Common Errors265
7.7Technical Terms266
7.8What You Should Know267
7.9Quiz Answers267
7.9.1Answers - Simple Counted Loops (pg. 240)267
7.9.2Answers - If-Else Construct (pg. 246)269
7.9.3Answers - Nested If (pg. 256)271
7.10Exercises274
8LOOPS281
8.0Introduction281
8.1Introduction to Loops281
8.2Pseudo-code282
8.3Counted Loops283
8.3.1Simple Counted Loops283
8.3.2Quiz Yourself - Simple Counted Loops288
8.3.3More Complicated Counted Loops288
8.3.4Quiz Yourself - Counted Loops292
8.4Conditional Loops293
8.4.1While Loops293
8.4.2Quiz Yourself - While Loops295
8.4.3Do Loops296
8.5Choosing Which Loop299
8.5.1Counted Loops vs. Conditional Loops299
8.6Breaking Out of Loops300
8.6.1The Break Statement301
8.6.2Break Statements and Counted Loops302
8.6.3Multiple Break Statements303
8.6.4Break vs. Loop Restructuring305
8.7Nesting Loops305
8.8Technical Terms310
8.9What You Should Know311
8.10Quiz Answers311
8.10.1Answers - Simple Counted Loops (pg. 288)311
8.10.2Answers - Counted Loops (pg. 292)313
8.10.3Answers - While Loops (pg. 295)315
8.11Exercises318
9TOP DOWN PROGRAM DESIGN323
9.0Introduction323
9.1Review323
9.2Top Down Programming Technique324
9.3The PhoneBookExample Program324
9.3.1Specification of the PhoneBookExample Program324
9.3.2Design for the PhoneBookExample Program324
9.3.3Implementation Stage for PhoneBookExample333
9.4Testing336
9.4.1Different Types of Problems336
9.4.2Different Types of Testing337
9.5The Mortgage Program341
9.5.1Specification of the Mortgage Program341
9.5.2Design for the Mortgage Program341
9.5.3Implementation Stage for Mortgage352
9.5.4Output from the Mortgage Program354
9.6Testing of the Mortgage Program355
9.7Technical Terms361
9.8What You Should Know361
9.9Exercises361
10STRINGS363
10.0Introduction363
10.1Review363
10.1.1Strings363
10.1.2Objects and References364
10.2The String Class365
10.2.1String Literals365
10.2.2String Objects and Aliasing366
10.2.3Immutable Types367
10.3Testing For Equality in Strings370
10.3.1The equals method371
10.3.2CountStudents Example373
10.3.3The equalsIgnoreCase method374
10.4Comparing Strings374
10.4.1How String Comparisons are Performed375
10.4.2The SortThree Example377
10.4.3The FirstLastWord Example379
10.4.4The compareToIgnoreCase method381
10.5Substrings381
10.5.1The length Method381
10.5.2The charAt Method382
10.5.3The substring Method386
10.6Finding Characters or Strings in a String390
10.6.1Searching For a Character390
10.6.2Searching For a String395
10.7More String Methods397
10.8The FindThe Example399
10.8.1The Specification399
10.8.2The Design400
10.8.3Implementation402
10.9String Manipulation404
10.9.1String Construction404
10.9.2The StringBuffer Class404
10.9.3The MakeCountDown Example407
10.9.4The ClipIt Example408
10.10String Tokenizer409
10.11Technical Terms412
10.12What You Should Know412
10.13Exercises413
11METHODS417
11.0Introduction417
11.1Introduction to Methods417
11.1.1What is a Method?417
11.1.2Using Methods to Structure Programs418
11.1.3Reusing Methods418
11.2Review of Methods419
11.3Methods Without Parameters420
11.3.1Local Variables423
11.3.2Multiple Methods424
11.3.3The Stack425
11.3.4Function-Type Example428
11.3.5The return Statement431
11.3.6Tracing431
11.4Creating Methods With Parameters434
11.4.1Function-Type Methods with Parameters441
11.4.2Returning Objects442
11.5Class Variables445
11.5.1Default Values of Class Variables446
11.5.2Tradeoffs446
11.5.3Class Constants447
11.6Unit Testing448
11.6.1Example of a Unit Test448
11.7Errors and Debugging449
11.7.1Debugging Technique450
11.7.2Tracing Execution450
11.7.3Run-Time Errors and Stack Dumps455
11.8Technical Terms458
11.9What You Should Know458
11.10Exercises459
12OBJECTS AND CLASSES465
12.0Introduction465
12.1Review465
12.1.1Objects465
12.1.2Classes466
12.1.3Objects in Java466
12.2Fields467
12.2.1Syntax of Field Declarations468
12.2.2Objects in a Field469
12.2.3Default Values of Fields469
12.2.4Initialization of Fields469
12.3Methods in the Interface469
12.3.1Syntax for Methods in the Interface470
12.3.2Calling Methods in the Interface470
12.3.3Bank Account Example471
12.3.4Accessor Methods and Object Oriented Design473
12.3.5Using the Account Class474
12.4Constructors476
12.4.1Default Constructor477
12.4.2Overloading Constructors477
12.5Helper Methods478
12.5.1Syntax for Helper Methods478
12.5.2Calling Methods from Within the Class479
12.5.3Using Other Classes479
12.6The ImprovedAccount Class480
12.6.1Tracing Execution484
12.7Using Multiple Objects490
12.7.1The Specification490
12.7.2The Design490
12.7.3Program Execution500
12.8The MultiBug Class502
12.8.1The MultiBug Interface503
12.8.2A Program Using the MultiBug Class503
12.8.3Implementation of the MultiBug Class505
12.9Unit Testing507
12.10Technical Terms507
12.11What You Should Know508
12.12Exercises508
13ARRAYS513
13.0Introduction513
13.1What is an Array513
13.1.1Declaring an Array514
13.1.2Creating an Array514
13.1.3Accessing Elements of an Array516
13.1.4Variables as an Array Index516
13.1.5Initializing an Array520
13.1.6The length field521
13.1.7Common Errors with Arrays522
13.2Related Arrays523
13.2.1Tracing of Program using Arrays526
13.3The BankArrayTest Example530
13.3.1The Specification530
13.3.2The Design531
13.3.3The Implementation531
13.3.4Conclusion536
13.4The SimplePhoneBook Class536
13.4.1The Interface to the SimplePhoneBook Class536
13.4.2The Data537
13.4.3The Constructor537
13.4.4The add Method538
13.4.5The lookUp Method538
13.5Operations on Arrays539
13.5.1Insertion of an Element into an Array539
13.5.2Deletion of an Element from an Array545
13.5.3Using the SortedPhoneBook Class548
13.6Two-Dimensional Arrays (AB)552
13.6.1Declaring a Two-Dimensional Array552
13.6.2Creating a Two-Dimensional Array553
13.6.3Accessing Elements of an Two-Dimensional Array554
13.6.4Mapping Columns to Index Values554
13.6.5For Loops and Two-Dimensional Arrays555
13.6.6The SalesReport1 Program555
13.6.7The SalesReport2 Program560
13.7Technical Terms563
13.8What You Should Know563
13.9Exercises564
14INHERITANCE AND INTERFACES567
14.0Introduction567
14.1Inheritance567
14.1.1Inheritance in Real Life567
14.1.2Inheritance in Programming568
14.1.3Syntax for Inheritance570
14.1.4Overriding Methods577
14.2Polymorphism580
14.2.1Why Does Polymorphism Work?581
14.3Animation Using Polymorphism582
14.3.1The Animator Class583
14.3.2The Moveable Class585
14.3.3Moving The Object Yourself589
14.3.4Adding Different Classes590
14.3.5The Graphics Class591
14.3.6Loading and Displaying Graphics Files600
14.3.7Using Fonts602
14.3.8Using super to Call Methods in the Parent605
14.4Abstract Classes605
14.4.1Syntax for an Abstract Class606
14.4.2Using Abstract Classes608
14.4.3A Larger Example608
14.5Class Hierarchies615
14.5.1Executing Methods616
14.5.2Polymorphism in Class Hierarchies616
14.5.3Class Hierarchies in Real Life616
14.6Technical Terms617
14.7What You Should Know617
14.8Exercises618
15RECURSION621
15.0Introduction621
15.1What is Recursion621
15.1.1Reversing a String621
15.2Tracing Execution624
15.3Towers of Hanoi628
15.3.1The Hanoi Program630
15.3.2Animating the Towers of Hanoi631
15.4Mutual Recursion632
15.4.1Expression Evaluator633
15.4.2Implementing the Evaluator Program634
15.5Recursion and Graphics - The Dragon Curve639
15.5.1The DragonCurve Class641
15.5.2The DragonCurveTest Class644
15.6Technical Terms645
15.7What You Should Know646
15.8Exercises646
16SEARCHING649
16.0Introduction649
16.1Linear Search649
16.1.1Linear Searching of an Array of Integers650
16.1.2Linear Searching of an Array of Strings653
16.1.3Speed of Linear Search653
16.2Genericity and the Object Class657
16.2.1The Object Class658
16.2.2Genericity660
16.2.3The Object Methods662
16.2.4Wrapper Classes663
16.2.5The ArrayList Class666
16.3Binary Search670
16.3.1Binary Search of an Array of Integers671
16.3.2Binary Search of an Array of Strings677
16.3.3Speed of Binary Search678
16.4Interfaces and Multiple Inheritance679
16.4.1Interfaces680
16.5Data Structure Example682
16.5.1Generic Data Structures - Key and Data682
16.5.2The Specification of the Birthplace Program684
16.5.3The Design684
16.5.4The Implementation686
16.6Preconditions and Postconditions694
16.6.1Preconditions694
16.6.2Postconditions695
16.6.3Assertions695
16.6.4The assert Statement (Optional)696
16.6.5Invariants (AB)698
16.7Hashing (AB)700
16.7.1How Hashing Works700
16.7.2Hashing Functions702
16.7.3Handling Collisions704
16.7.4The StudentHash Program706
16.7.5Object's hashcode Method710
16.7.6Speed of Hashing715
16.8Technical Terms715
16.9What You Should Know716
16.10Exercises716
17SORTING719
17.0Introduction719
17.1Introduction to Sorting720
17.1.1Illustrating Sorting720
17.1.2Common Elements to a Sort720
17.2Selection Sort723
17.2.1Efficiency of Selection Sort727
17.3Insertion Sort728
17.3.1Efficiency of Insertion Sort731
17.4Merge Sort732
17.4.1Recursive Sorts732
17.4.2Merge Sort733
17.5Quicksort (AB)738
17.5.1Efficiency of Quicksort741
17.6Visual Sorting741
17.7Technical Terms748
17.8What You Should Know748
17.9Exercises748
18LINKED LISTS (AB)751
18.0Introduction751
18.1Linked Lists751
18.1.1Implementing a Linked List752
18.1.2Storing a Linked List755
18.1.3Searching a Linked List755
18.1.4Inserting Nodes in a Linked List762
18.1.5Removing Nodes from a Linked List769
18.2The PhoneList Example774
18.2.1The PhoneEntry Class777
18.2.2The SortedIterator Class778
18.2.3The SortedLinkedList Class778
18.2.4The PhoneList Class780
18.2.5The PhoneListExample Class782
18.2.6Output from Execution784
18.3Optimizing Linked Lists785
18.3.1Adding Elements to the End of the List785
18.3.2Doubly Linked Lists787
18.3.3Circularly Linked Lists792
18.4Hash Tables and Chaining793
18.5The Birthplace3 Example794
18.5.1The HashEntry Class795
18.5.2The HashIterator Class796
18.5.3The HashLinkedList Class796
18.5.4The HashTable Class798
18.5.5The Birthplace3 Class801
18.5.6Output from Execution803
18.6Technical Terms804
18.7What You Should Know804
18.8Exercises805
19TREES (AB)807
19.0Introduction807
19.1What is a Tree?807
19.2Trees in Computer Science809
19.2.1Tree Terminology809
19.3Implementation of Binary Trees811
19.3.1Algorithms Using Binary Trees815
19.4Tree Traversal820
19.5An Expression Evaluator822
19.6Binary Search Trees826
19.6.1Key/Value Data829
19.6.2Searching a Binary Search Tree830
19.6.3Printing a Binary Search Tree833
19.6.4Adding to a Binary Search Tree834
19.6.5Removing Nodes from a Binary Search Tree838
19.7Heapsort843
19.7.1Efficiency of Heap Sort852
19.7.2Visual Heap Sort853
19.8Technical Terms854
19.9What You Should Know855
19.10Exercises855
20DATA STRUCTURES IN THE JAVA CLASS LIBRARY (AB)861
20.0Introduction861
20.1Lists in the Java Class Library861
20.1.1The List Interface862
20.1.2The Iterator Interface864
20.1.3The ListIterator Interface869
20.2The ArrayList Class874
20.3The LinkedList Class878
20.4Sets in the Java Class Library881
20.4.1The Set Interface882
20.4.2The FindPrimes Example883
20.5Maps in the Java Class Library887
20.5.1The Map Interface888
20.5.2Listing the Contents of a Map889
20.5.3The LastNameLookup Example890
20.6The Librarian Program894
20.6.1Data Structures895
20.6.2The Book Class896
20.6.3The LibraryCard Class897
20.6.4The Library Class900
20.6.5The Librarian Class905
20.6.6Output from the Program906
20.7Technical Terms908
20.8What You Should Know908
20.9Exercises908
21STACKS, QUEUES, AND PRIORITY QUEUES (AB)911
21.0Introduction911
21.1Stack911
21.1.1The Stack Interface913
21.1.2The Reverser Example914
21.1.3The BracketMatch Example917
21.2Preconditions and Throwing Exceptions921
21.2.1Throwing Exceptions921
21.3Queue923
21.3.1The Queue Interface925
21.3.2The Echoer Example926
21.3.3The BankSim Example928
21.4Priority Queue940
21.4.1The PriorityQueue Interface942
21.4.2The Jobs Example944
21.5Technical Terms948
21.6What You Should Know948
21.7Exercises949
APPENDIX A: RESERVED WORDS951
APPENDIX B: JAVA CLASS LIBRARY (FOR AP)952
APPENDIX C: AP CLASSES970
APPENDIX D: HSA CLASSES972
APPENDIX E: OPERATORS987
APPENDIX F: INSTALLING READY TO PROGRAM989
Installation for Personal Use989
Installation for Institutional Use991
APPENDIX G: USING READY TO PROGRAM994
APPENDIX H: USING OTHER IDES AND CLASS PATH1007
Other Environments1007
CLASSPATH1008
Packages and CLASSPATH1009
APPENDIX I: READY TO PROGRAM
    LIMITED LICENSE AGREEMENT
1010
INDEX1013

License for Ready to Program included with Java for AP Computer Science


[ Holt Software Home ] * [ Books (Graphical) ] * [ Books (Text) ] * [ Top of Page ] * [ Feedback ]