The Art and Science of Java
Preliminary Draft
Eric S. Roberts
Stanford University
Stanford, California
January 2006
Preface
This text is an early draft for a general introductory textbook in computer science—a
Java-based version of my 1995 textbook The Art and Science of C. My hope is that I can
use much of the existing material in writing the new book, although quite a bit of the
material and overall organization have to change. At this point, the material is still in a
preliminary form, and the feedback I get from those of you who are taking this course
will almost certainly lead to some changes before the book is published.
One of the central features of the text is that it incorporates the work of the Association of
Computing Machinery’s Java Task Force, which was convened in 2004 with the
following charter:
To review the Java language, APIs, and tools from the perspective of introductory
computing education and to develop a stable collection of pedagogical resources that
will make it easier to teach Java to first-year computing students without having
those students overwhelmed by its complexity.
I am grateful to my colleagues on the Task Force—Kim Bruce, Robb Cutler, James H.
Cross II, Scott Grissom, Karl Klee, Susan Rodger, Fran Trees, Ian Utting, and Frank
Yellin—for all their hard work over the past year, as well as to the National Science
Foundation, the ACM Education Board, the SIGCSE Special Projects Fund for their
financial support.
I also want to thank the participants in last year’s CS 298 seminar—Andrew Adams,
Andy Aymeloglu, Kurt Berglund, Seyed Dorminani-Tabatabaei, Erik Forslin, Alex
Himel, Tom Hurlbutt, Dave Myszewski, Ann Pan, Vishakha Parvate, Cynthia Wang, Paul
Wilkins, and Julie Zhuo for helping me work through these ideas. In addition, I would
like to thank my CS 106A TA Brandon Burr and all the hardworking section-leaders for
taking on the challenge of helping to teach a course with a just-in-time approach to the
materials.
Particularly because my wife Lauren Rusk (who has edited all of my books) has not yet
had her chance to work her wonderful magic on the language, you may still find some
rough edges, awkward constructions, and places where real improvement is needed.
Writing is, after all, at least as difficult as programming and requires just as much testing
to get everything right. If you let me know when things are wrong, I think we’ll end up
with a textbook and a course that are exciting, thorough, and practical.
Thanks in advance for all your help.
Eric Roberts
Professor of Computer Science
Stanford University
September 2005
Table of Contents
1. Introduction 1
1.1 A brief history of computing 2
1.2 What is computer science? 4
1.3 An overview of computer hardware 5
1.4 Algorithms 7
1.5 Stages in the programming process 8
1.6 Java and the object-oriented paradigm 13
1.7 Java and the World Wide Web 17
2. Programming by Example 21
2.1 The “hello world” program 22
2.2 Perspectives on the programming process 26
2.3 A program to add two numbers 26
2.4 Classes and objects 31
3. Expressions 39
3.1 Primitive data types 41
3.2 Constants and variables 42
3.3 Operators and operands 46
3.4 Assignment statements 53
3.5 Programming idioms and patterns 56
4. Statement Forms 63
4.1 Simple statements 64
4.2 Control statements 66
4.3 Boolean data 67
4.4 The
if statement 73
4.5 The
switch statement 78
4.6 The concept of iteration 79
4.7 The
while statement 85
4.8 The
for statement 90
5. Methods 99
5.1 A quick overview of methods 100
5.2 Methods and the object-oriented paradigm 103
5.3 Writing your own methods 108
5.4 Mechanics of the method-calling process 114
5.5 Algorithmic methods 125
6. Objects and Classes 135
6.1 Using the
RandomGenerator class 136
6.2 Defining your own classes 143
6.3 Defining a class to represent rational numbers 150
7. The Object Memory Model 165
7.1 The structure of memory 166
7.2 Allocation of memory to variables 170
7.3 Primitive types vs. objects 176
7.4 Linking objects together 180
8. Object-Oriented Graphics 189
8.1 The
acm.graphics model 190
8.2 The graphics class hierarchy 191
8.3 Facilities available in the
GraphicsProgram class 198
8.4 Animation and interactivity 199
8.5 Creating compound objects 208
8.6 Principles of good object-oriented design 210
9. Strings and Characters 225
9.1 The principle of enumeration 226
9.2 Characters 228
9.3 Strings as an abstract idea 237
9.4 Using the methods in the
String class 238
10. Arrays and ArrayLists 253
10.1 Introduction to arrays 254
10.2 Internal representation of arrays 258
10.3 Passing arrays as parameters 259
10.4 The
ArrayList class 263
10.5 Using arrays for tabulation 267
10.6 Initialization of arrays 268
10.7 Multidimensional arrays 270
11. Searching and Sorting 283
11.1 Searching 284
11.2 Sorting 292
Index 307
A note on the cover image: The cover of The Art and Science of C showed a picture of Patience, one of the
two stone lions that guard the entrance to the New York Public Library. Addison-Wesley and I chose that
image both to emphasize the library-based approach adopted by the text and because patience is an
essential skill in programming. In 2003, the United States Postal Service decided to put Patience on a
stamp, which gave those of us who have a special attachment to that lion a great deal of inner pleasure.
Chapter 1
Introduction
[The Analytical Engine offers] a new, a vast, and a powerful language . . .
for the purposes of mankind.
— Augusta Ada Byron, Lady Lovelace,
The Sketch of the Analytical Engine
Invented by Charles Babbage, 1843
Augusta Ada Byron, Lady Lovelace (1815–1852)
Augusta Ada Byron, the daughter of English poet Lord Byron, was encouraged b ests in
science and mathematics at a time when few women were allowed to study those
subjects. At the age of 17, Ada met Charles Babbage, a prominent English scientist who
devoted his life to designing machines for carrying out mathematical computations—
machines that he was never able to complete. Ada was firmly convinced of the potential
of Babbage’s Analytical Engine and wrote extensive notes on its design, along with
several complex mathematical programs that have led many people to characterize her as
the first programmer. In 1980, the U.S. Department of Defense named the programming
language Ada in her honor.
评论3
最新资源