
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. 2 The Art and Science of Java Given our vantage point at the beginning of the 21st century, it is hard to believe that computers did not even exist in 1940. Computers are everywhere today, and it is the popular wisdom, at least among headline writers, to say that we live in the computer age. 1.1 A brief history of computing In a certain sense, computing has been around since ancient times. Much of early mathematics was devoted to solving computational problems of practical importance, such as monitoring the number of animals in a herd, calculating the area of a plot of land, or recording a commercial transaction. These activities required people to develop new computational techniques and, in some cases, to invent calculating machines to help in the process. For example, the abacus, a simple counting device consisting of beads that slide along rods, has been used in Asia for thousands of years, possibly since 2000 BCE. Throughout most of its history, computing has progressed relatively slowly. In 1623, a German scientist named Wilhelm Schickard invented the first known mechanical calculator, capable of performing simple arithmetical computations automatically. Although Schickard’s device was lost to history through the ravages of the Thirty Years’ War (1618–1648), the French philosopher Blaise Pascal used similar techniques to construct a mechanical adding machine in the 1640s, a copy of which remains on display in the Conservatoire des Arts et Métiers in Paris. In 1673, the German mathematician Gottfried Leibniz developed a considerably more sophisticated device, capable of multiplication and division as well as addition and subtraction. All these devices were purely mechanical and contained no engines or other source of power. The operator would enter numbers by setting metal wheels to a particular position; the act of turning those wheels set other parts of the machine in motion and changed the output display. During the Industrial Revolution, the rapid growth in technology made it possible to consider new approaches to mechanical computation. The steam engine already provided the power needed to run factories and railroads. In that context, it was reasonable to ask whether one could use steam engines to drive more sophisticated computing machines, machines that would be capable of carrying out significant calculations under their own power. Before progress could be made, however, someone had to ask that question and set out to find an answer. The necessary spark of insight came from a British mathematician named Charles Babbage, who is one of the most interesting figures in the history of computing. During his lifetime, Babbage designed two different computing machines, which he called the Difference Engine and the Analytical Engine; each represented a considerable advance over the calculating machines available at the time. The tragedy of his life is that he was unable to complete either of these projects.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages316 Page
-
File Size-