thinkapjava.pdf

(1036 KB) Pobierz
153951559 UNPDF
How to Think Like a Computer Scientist
Java Version 5.0.1
How to Think Like a Computer Scientist
Java Version 5.0.1
Allen B. Downey
Olin College
Needham, Massachusetts
Copyright c 2008 Allen Downey.
Permission is granted to copy, distribute, and/or modify this document under
the terms of the GNU Free Documentation License, Version 1.1 or any later
version published by the Free Software Foundation; with no Invariant Sections,
with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license
is included in the appendix entitled “GNU Free Documentation License.”
The GNU Free Documentation License is available from www.gnu.org or by
writing to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307, USA.
The original form of this book is L A T E X source code. Compiling this L A T E X source
has the eect of generating a device-independent representation of a textbook,
which can be converted to other formats and printed.
The L A T E X source for this book is available from
http://greenteapress.com/thinkjava
This book was typeset using L A T E X and LyX. The illustrations were drawn in
xg. All of these are free, open-source programs.
Contents
1 The way of the program
1
1.1
What is a programming language? . . . . . . . . . . . . . . . . 1
1.2
What is a program? . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3
What is debugging? . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4
Formal and natural languages . . . . . . . . . . . . . . . . . . . 6
1.5
The rst program . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.6
Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2 Variables and types
11
2.1
More printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.3
Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4
Printing variables . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.5
Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.6
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.7
Order of operations . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.8
Operators for Strings . . . . . . . . . . . . . . . . . . . . . . . 17
2.9
Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.10 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3 Methods
19
3.1
Floating-point . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.2
Converting from double to int . . . . . . . . . . . . . . . . . . 20
Zgłoś jeśli naruszono regulamin