0% found this document useful (0 votes)
33 views11 pages

Pages 1

The document is the contents page of a book on computer programming. It lists the chapter titles and section headings in the book. The acknowledgements section thanks the Department of Computer Science & Engineering at the University of Nebraska–Lincoln for supporting the author in writing and maintaining the book. It also dedicates the book to the author's family.

Uploaded by

hamadi.lakhal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views11 pages

Pages 1

The document is the contents page of a book on computer programming. It lists the chapter titles and section headings in the book. The acknowledgements section thanks the Department of Computer Science & Engineering at the University of Nebraska–Lincoln for supporting the author in writing and maintaining the book. It also dedicates the book to the author's family.

Uploaded by

hamadi.lakhal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Acknowledgements

I’d like to thank the Department of Computer Science & Engineering at the University
of Nebraska–Lincoln for their support during my writing and maintaining this book.

This book is dedicated to my family.

ix
Contents

Copyleft (Copyright) i

Draft Notice iii

Preface v

Acknowledgements ix

1. Introduction 1
1.1. Problem Solving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2. Computing Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3. Basic Program Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4. Syntax Rules & Pseudocode . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.5. Documentation, Comments, and Coding Style . . . . . . . . . . . . . . . 14

2. Basics 17
2.1. Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.1.1. Flowcharts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.2. Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.2.1. Naming Rules & Conventions . . . . . . . . . . . . . . . . . . . . 19
2.2.2. Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.2.3. Declaring Variables: Dynamic vs. Static Typing . . . . . . . . . . 31
2.2.4. Scoping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.3. Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.3.1. Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . 33
2.3.2. Numerical Operators . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.3.3. String Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.3.4. Order of Precedence . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.3.5. Common Numerical Errors . . . . . . . . . . . . . . . . . . . . . . 38
2.3.6. Other Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.4. Basic Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.4.1. Standard Input & Output . . . . . . . . . . . . . . . . . . . . . . 42
2.4.2. Graphical User Interfaces . . . . . . . . . . . . . . . . . . . . . . . 42
2.4.3. Output Using printf() -style Formatting . . . . . . . . . . . . . 43
2.4.4. Command Line Input . . . . . . . . . . . . . . . . . . . . . . . . . 44

xi
Contents

2.5. Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.5.1. Types of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.5.2. Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.6. Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.6.1. Temperature Conversion . . . . . . . . . . . . . . . . . . . . . . . 50
2.6.2. Quadratic Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
2.7. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

3. Conditionals 65
3.1. Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.1.1. Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . 66
3.1.2. Negation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
3.1.3. Logical And . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.1.4. Logical Or . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
3.1.5. Compound Statements . . . . . . . . . . . . . . . . . . . . . . . . 71
3.1.6. Short Circuiting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
3.2. The If Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.3. The If-Else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
3.4. The If-Else-If Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
3.5. Ternary If-Else Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3.6. Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3.6.1. Meal Discount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3.6.2. Look Before You Leap . . . . . . . . . . . . . . . . . . . . . . . . 83
3.6.3. Comparing Elements . . . . . . . . . . . . . . . . . . . . . . . . . 84
3.6.4. Life & Taxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
3.7. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

4. Loops 95
4.1. While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
4.1.1. Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
4.2. For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
4.2.1. Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
4.3. Do-While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
4.4. Foreach Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
4.5. Other Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
4.5.1. Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
4.5.2. Infinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
4.5.3. Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
4.5.4. Equivalency of Loops . . . . . . . . . . . . . . . . . . . . . . . . . 106
4.6. Problem Solving With Loops . . . . . . . . . . . . . . . . . . . . . . . . . 106
4.7. Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
4.7.1. For vs While Loop . . . . . . . . . . . . . . . . . . . . . . . . . . 107
4.7.2. Primality Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
4.7.3. Paying the Piper . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

xii
Contents

4.8. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

5. Functions 133
5.1. Defining & Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . 134
5.1.1. Function Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . 134
5.1.2. Calling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
5.1.3. Organizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
5.2. How Functions Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
5.2.1. Call By Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
5.2.2. Call By Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
5.3. Other Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
5.3.1. Functions as Entities . . . . . . . . . . . . . . . . . . . . . . . . . 142
5.3.2. Function Overloading . . . . . . . . . . . . . . . . . . . . . . . . . 144
5.3.3. Variable Argument Functions . . . . . . . . . . . . . . . . . . . . 145
5.3.4. Optional Parameters & Default Values . . . . . . . . . . . . . . . 145
5.4. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

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

8. Strings 177
8.1. Basic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
8.2. Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
8.3. Tokenizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
8.4. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179

9. File Input/Output 183


9.1. Processing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
9.1.1. Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
9.1.2. Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
9.1.3. Buffered and Unbuffered . . . . . . . . . . . . . . . . . . . . . . . 187

xiii
Contents

9.1.4. Binary vs Text Files . . . . . . . . . . . . . . . . . . . . . . . . . 187


9.2. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188

10.Encapsulation & Objects 197


10.1. Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
10.1.1. Defining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
10.1.2. Creating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
10.1.3. Using Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
10.2. Design Principles & Best Practices . . . . . . . . . . . . . . . . . . . . . 200
10.3. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

11.Recursion 203
11.1. Writing Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . . . 204
11.1.1. Tail Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
11.2. Avoiding Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
11.2.1. Memoization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
11.3. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

12.Searching & Sorting 211


12.1. Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
12.1.1. Linear Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
12.1.2. Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
12.1.3. Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
12.2. Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
12.2.1. Selection Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
12.2.2. Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
12.2.3. Quick Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
12.2.4. Merge Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
12.2.5. Other Sorts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
12.2.6. Comparison & Summary . . . . . . . . . . . . . . . . . . . . . . . 237
12.3. Searching & Sorting In Practice . . . . . . . . . . . . . . . . . . . . . . . 238
12.3.1. Using Libraries and Comparators . . . . . . . . . . . . . . . . . . 238
12.3.2. Preventing Arithmetic Errors . . . . . . . . . . . . . . . . . . . . 239
12.3.3. Avoiding the Difference Trick . . . . . . . . . . . . . . . . . . . . 241
12.3.4. Importance of a Total Order . . . . . . . . . . . . . . . . . . . . . 242
12.3.5. Artificial Ordering . . . . . . . . . . . . . . . . . . . . . . . . . . 242
12.3.6. Sorting Stability . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
12.4. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

13.Graphical User Interfaces & Event Driven Programming 247

14.Introduction to Databases & Database Connectivity 249

xiv
Contents

I. The C Programming Language 251


15.Basics 253
15.1. Getting Started: Hello World . . . . . . . . . . . . . . . . . . . . . . . . 253
15.2. Basic Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
15.2.1. Basic Syntax Rules . . . . . . . . . . . . . . . . . . . . . . . . . . 255
15.2.2. Preprocessor Directives . . . . . . . . . . . . . . . . . . . . . . . . 255
15.2.3. Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
15.2.4. The main() Function . . . . . . . . . . . . . . . . . . . . . . . . 259
15.3. Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
15.3.1. Declaration & Assignment . . . . . . . . . . . . . . . . . . . . . . 260
15.4. Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
15.5. Basic I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
15.6. Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
15.6.1. Converting Units . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
15.6.2. Computing Quadratic Roots . . . . . . . . . . . . . . . . . . . . . 267

16.Conditionals 271
16.1. Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
16.1.1. Order of Precedence . . . . . . . . . . . . . . . . . . . . . . . . . 273
16.1.2. Comparing Strings and Characters . . . . . . . . . . . . . . . . . 273
16.2. If, If-Else, If-Else-If Statements . . . . . . . . . . . . . . . . . . . . . . . 274
16.3. Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
16.3.1. Computing a Logarithm . . . . . . . . . . . . . . . . . . . . . . . 276
16.3.2. Life & Taxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
16.3.3. Quadratic Roots Revisited . . . . . . . . . . . . . . . . . . . . . . 279

17.Loops 283
17.1. While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
17.2. For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
17.3. Do-While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
17.4. Other Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
17.5. Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
17.5.1. Normalizing a Number . . . . . . . . . . . . . . . . . . . . . . . . 287
17.5.2. Summation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
17.5.3. Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
17.5.4. Paying the Piper . . . . . . . . . . . . . . . . . . . . . . . . . . . 288

18.Functions 291
18.1. Defining & Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . 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

xv
Contents

18.2. Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295


18.2.1. Passing By Reference . . . . . . . . . . . . . . . . . . . . . . . . . 297
18.2.2. Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
18.3. Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
18.3.1. Generalized Rounding . . . . . . . . . . . . . . . . . . . . . . . . 301
18.3.2. Quadratic Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . 303

19.Error Handling 305


19.1. Language Supported Error Codes . . . . . . . . . . . . . . . . . . . . . . 305
19.1.1. POSIX Error Codes . . . . . . . . . . . . . . . . . . . . . . . . . 306
19.2. Error Handling By Design . . . . . . . . . . . . . . . . . . . . . . . . . . 308
19.3. Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
19.4. Using Enumerated Types for Error Codes . . . . . . . . . . . . . . . . . 310

20.Arrays 313
20.1. Basic Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
20.2. Dynamic Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
20.3. Using Arrays with Functions . . . . . . . . . . . . . . . . . . . . . . . . . 318
20.4. Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
20.4.1. Contiguous 2-D Arrays . . . . . . . . . . . . . . . . . . . . . . . . 322
20.5. Dynamic Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . 324

21.Strings 325
21.1. Character Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
21.2. String Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
21.3. Arrays of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
21.4. Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
21.5. Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
21.6. Tokenizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333

22.File I/O 335


22.1. Opening Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
22.2. Reading & Writing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
22.2.1. Plaintext Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
22.2.2. Binary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
22.3. Closing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339

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 . . . . . . . . . . . . . . . . . . . . . . . . . . 346

xvi
Contents

23.3. Arrays of Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348


23.4. Using Structures With Functions . . . . . . . . . . . . . . . . . . . . . . 351
23.4.1. Factory Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
23.4.2. To String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 354
23.4.3. Passing Arrays of Structures . . . . . . . . . . . . . . . . . . . . . 355

24.Recursion 357

25.Searching & Sorting 361


25.1. Comparator Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
25.2. Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
25.3. Searching & Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
25.3.1. Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
25.3.2. Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
25.3.3. Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
25.4. Other Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
25.4.1. Sorting Pointers to Elements . . . . . . . . . . . . . . . . . . . . . 377

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

27.Conditionals 403
27.1. Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
27.1.1. Order of Precedence . . . . . . . . . . . . . . . . . . . . . . . . . 405
27.1.2. Comparing Strings and Characters . . . . . . . . . . . . . . . . . 406
27.2. If, If-Else, If-Else-If Statements . . . . . . . . . . . . . . . . . . . . . . . 407
27.3. Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
27.3.1. Computing a Logarithm . . . . . . . . . . . . . . . . . . . . . . . 408
27.3.2. Life & Taxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410

xvii
Contents

27.3.3. Quadratic Roots Revisited . . . . . . . . . . . . . . . . . . . . . . 411

28.Loops 415
28.1. While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
28.2. For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
28.3. Do-While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
28.4. Enhanced For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
28.5. Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
28.5.1. Normalizing a Number . . . . . . . . . . . . . . . . . . . . . . . . 419
28.5.2. Summation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
28.5.3. Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
28.5.4. Paying the Piper . . . . . . . . . . . . . . . . . . . . . . . . . . . 421

29.Methods 423
29.1. Defining Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
29.1.1. Void Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
29.1.2. Using Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
29.1.3. Passing By Reference . . . . . . . . . . . . . . . . . . . . . . . . . 427
29.2. Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
29.2.1. Generalized Rounding . . . . . . . . . . . . . . . . . . . . . . . . 428

30.Error Handling & Exceptions 431


30.1. Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
30.1.1. Catching Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . 431
30.1.2. Throwing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . 433
30.1.3. Creating Custom Exceptions . . . . . . . . . . . . . . . . . . . . . 433
30.1.4. Checked Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 434
30.2. Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
30.2.1. More Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437

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

xviii

You might also like