The java programming language basics
Computer Science I
The entirety of this book is free and is released under a Creative Commons Attribution-
ShareAlike 4.0 International License (see /
Thank you in advance to anyone helping out or sending constructive criticisms.
iii
In established fields such as mathematics and physics, new textbooks are especially questionable as the material and topics don’t undergo many changes. However, in Computer Science, new languages and technologies are created and change at breakneck speeds. Faculty and students are regularly trying to give away stacks of textbooks (“Learn Java 4!,” “Introduction to Cold Fusion,” etc.) that are only a few years old and yet are completely obsolete and worthless. The problem is that such books have built-in obsolescence by focusing too much on technological specifics and not enough on concepts. There are dozens of introductory textbooks for Computer Science; add in the fact that there are multiple languages and many gimmicks (“Learn Multimedia Java,” “Gaming with JavaScript,” “Build a Robot with C!”), it is a publisher’s paradise: hundreds of variations, a growing market, and customers with few alternatives.
v
Another inspiration for the structure of this book is the Computer Science I Honors course that I developed. Usually Computer Science majors take CS1 using Java as the primary language while CE students take CS1 using C. Since the honors course consists of both majors (as well as some of the top students), I developed the Honors version to cover both languages at the same time in parallel. This has led to many interesting teaching moments: by covering two languages, it provides opportunities to highlight fundamental differences and concepts in programming languages. It also keeps concepts as the focus of the course emphasizing that syntax and idiosyncrasies of individual languages are only of secondary concern. Finally, actively using multiple languages in the first class provides a better opportunity to extend knowledge to other programming languages–once a student has a solid foundation in one language learning a new one should be relatively easy.
The exercises in this book are a variety of exercises I’ve used in my courses over the years. They have been made as generic as possible so that they could be assigned using any language. While some have emphasized the use of “real-world” exercises (whatever that means), my exercises have focused more on solving problems of a mathematical
I hope that people find this book useful. If value is a ratio of quality vs cost then this book has already succeeded in having infinite value.1If you have suggestions on how to improve it, please feel free to contact me. If you end up using it and finding it useful, please let me know that too!
1or it might be undefined, or NaN, or this book is Exceptional depending on which language sections you read
ix
Contents
v ix |
|||||
---|---|---|---|---|---|
2.1.1. | Flowcharts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | ||||
|
|||||
2.2.1. | Naming Rules & Conventions . . . . . . . . . . . . . . . . . . . . | ||||
2.2.2. | Types |
|
|||
2.2.3. | Declaring Variables: Dynamic vs. Static Typing . . . . . . . . . . | ||||
2.2.4. | |||||
2.3.1. | . . . . . . . . . . . . . . . . . . . . . . . . | ||||
2.3.2. | Numerical Operators . . . . . . . . . . . . . . . . . . . . . . . . . | ||||
2.3.3. | String Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . | ||||
2.3.4. |
|
|
|||
2.3.5. | Common Numerical Errors . . . . . . . . . . . . . . . . . . . . . . | ||||
2.3.6. | Other Operators | . . . . . . . . . . . . . . . . . . . . . . . . . . . | |||
2.4.1. | Standard Input & Output . . . . . . . . . . . . . . . . . . . . . . | ||||
2.4.2. | Graphical User Interfaces . . . . . . . . . . . . . . . . . . . . . . . | ||||
2.4.3. | Output Using printf() -style Formatting . . . . . . . . . . . . . | ||||
2.4.4. | Command Line Input . . . . . . . . . . . . . . . . . . . . . . . . . | ||||
Contents
2.5. Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.6.2. Quadratic Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
2.7. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.1.3. Logical And . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.1.4. Logical Or . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
3.4. The If-Else-If Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
3.5. Ternary If-Else Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3.6.4. Life & Taxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
3.7. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
4.2.1. Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
4.3. Do-While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
4.5.3. Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
4.5.4. Equivalency of Loops . . . . . . . . . . . . . . . . . . . . . . . . . 106
4.7.3. Paying the Piper . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
xii
6. Error Handling 151 6.1. Error
Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
153 6.2. Error Handling Strategies . . . . . . . . . . . . . . . . . . .
. . . . . . . 153 6.2.1. Defensive Programming
. . . . . . . . . . . . . . . . . . . . . . . 153 6.2.2. Exceptions . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 6.3.
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . 157
7. Arrays, Collections & Dynamic Memory 159 7.1. Basic Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 7.2. Static & Dynamic Memory . . . . . . . . . . . . . . . . . . . . . . . . . . 162 7.2.1. Dynamic Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 7.2.2. Shallow vs. Deep Copies . . . . . . . . . . . . . . . . . . . . . . . 166 7.3. Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 7.4. Other Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 7.5. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
9.1.4. | Binary vs Text Files |
|
---|
9.2. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
. . . . . . . . . . . . . . . . . . . . 239 . . . . . . . . . . . . . . . . . . . . 241
12.3.4. Importance of a Total Order . . . . . . . . . . . . . . . . . . . . . 242
14.Introduction to Databases & Database Connectivity 249
xiv
16.1.1. Order of Precedence
. . . . . . . . . . . . . . . . . . . . . . . . . 273
. . . . . . . . . . . . . . . . . . . . . . . . . 291
18.1.1. Declaration: Prototypes . . . . . . . . . . . . . . . . . . . . . . . 291 18.1.2. Void Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 18.1.3. Organizing Functions . . . . . . . . . . . . . . . . . . . . . . . . . 294 18.1.4. Calling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
. . . . . . . . . . . . . . . . . . . . . . . . 301
18.3.2. Quadratic Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
23.Structures 341 23.1. Defining Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 23.1.1. Alternative Declarations . . . . . . . . . . . . . . . . . . . . . . . 342 23.1.2. Nested Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 23.2. Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 23.2.1. Declaration & Initialization
. . . . . . . . . . . . . . . . . . . . . 344 23.2.2. Selection Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 346xvi
II. The Java Programming Language 381
26.Basics 383 26.1. Getting
Started: Hello World . . . . . . . . . . . . . . . . . . . . . . . . 384
26.2. Basic Elements . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . 385 26.2.1. Basic Syntax Rules . . . . . . . . . . . . . . .
. . . . . . . . . . . 385 26.2.2. Program Structure . . . . . . . . . .
. . . . . . . . . . . . . . . . 386 26.2.3. The main() Method . . . . .
. . . . . . . . . . . . . . . . . . . . 389 26.2.4. Comments . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . 389 26.3. Variables .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
390 26.3.1. Declaration & Assignment . . . . . . . . . . . . . . . .
. . . . . . 391 26.4. Operators . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . 393 26.5. Basic I/O . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . 395 26.6. Examples
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
396 26.6.1. Converting Units . . . . . . . . . . . . . . . . . . . . . .
. . . . . 396 26.6.2. Computing Quadratic Roots . . . . . . . . . . . .
. . . . . . . . . 400
xvii
Contents
31.Arrays 439 31.1. Basic Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439 31.2. Dynamic Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442 31.3. Using Arrays with Methods . . . . . . . . . . . . . . . . . . . . . . . . . 442 31.4. Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 31.5. Dynamic Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . 444
32.Strings 449 32.1. Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 32.2. String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450 32.3. Arrays of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452 32.4. Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 32.5. Tokenizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455