knking.com -- Programming Language Books and Training

 C C++ Java

Books by K. N. King

Home
Books by K. N. King
Short courses
Recommended books
Recommended links
FAQ

Java Programming: From the Beginning

Brief Contents

1 Getting Started 1
2 Writing Java Programs 27
3 Classes and Objects 85
4 Basic Control Structures 127
5 Arrays 181
6 Graphics 221
7 Class Variables and Methods 257
8 More Control Structures 299
9 Primitive Types 339
10 Writing Classes 387
11 Subclasses 443
12 The Abstract Window Toolkit 485
13 Data Structures 541
14 Files 599

Appendix A Setting Up Java 661
Appendix B Java Language Summary 665
Appendix C Java API Summary 681
Appendix D Applets 743
Appendix E The jpb Package 759

Bibliography 765
Index 767


Full Contents

Preface xvii

1 Getting Started 1

1.1 What Do Computers Do? 2

Hardware 3
Software 3

1.2 Ways of Interacting with Computers 5

Graphical User Interfaces 5
Text-Based Interfaces 8

1.3 What Is Programming? 10

A Real-World Algorithm 11
Computer Algorithms 12
Expressing Algorithms 12

1.4 Storing Data 13

Variables 14

1.5 Programming Languages 15

Writing and Executing a Program 17

1.6 Why Java? 18

1.7 The Programming Process 20

1.8 What You Need to Know 21

2 Writing Java Programs 27

2.1 A Simple Java Program 28

Program: Printing a Message 28
Java Programs in General 29

2.2 Executing a Java Program 30

Entering a Java Program 31
Compiling a Java Program 32
Running a Java Program 33
Using Multiple Windows 33

2.3 Program Layout 34

Comments 34
Tokens 36
Indentation and Brace Placement 37

2.4 Using Variables 39

Declaring Variables 39
Initializing Variables 40
Changing the Value of a Variable 41
Program: Printing a Lottery Number 41

2.5 Types 42

Literals 43

2.6 Identifiers 44

Keywords 45

2.7 Performing Calculations 46

Operators 46
Precedence and Associativity 49
Assignment Operators 50
Program: Converting from Fahrenheit to Celsius 51

2.8 Constants 52

2.9 Methods 54

Methods in the Math Class 55
Using the Result of a Method Call 56

2.10 Input and Output 58

Displaying Output on the Screen 58
Escape Sequences 59
Printing Multiple Items 60
Obtaining Input from the User 60
Packages 61
Program: Converting from Fahrenheit to Celsius (Revisited) 62

2.11 Case Study: Computing a Course Average 63

Improving the Program 69

2.12 Debugging 70

Types of Errors 71
Fixing Compile-Time Errors 71
Fixing Run-Time Errors 72
Fixing Behavioral Errors 73
Using a Debugger 73
Debugging Without a Debugger 73
Choosing Test Data 74

3 Classes and Objects 85

3.1 Objects as Models 86

3.2 Representing Objects Within a Program 87

Instance Variables 87
Instance Methods 88

3.3 Classes 89

Declaring a Class 89
Declaring Instance Variables 90
Declaring Instance Methods 90
Method Overloading 91
Declaring Constructors 91
Example: An Account Class 92

3.4 Creating Objects 93

3.5 Calling Instance Methods 94

How Instance Methods Work 95

3.6 Writing Programs with Multiple Classes 97

3.7 How Objects Are Stored 99

The null Keyword 100
Object Assignment 100
Garbage 102

3.8 Developing a Fraction Class 103

Getters and Setters 104
Writing the add Method 105
Adding a toString Method 107

3.9 Java's String Class 108

Creating Strings 108
Common String Methods 109
Chaining Calls of Instance Methods 112
Using + to Concatenate Strings 112
Program: Decoding a Vehicle Identification Number 114

3.10 Case Study: Checking an ISBN Number 117

4 Basic Control Structures 127

4.1 Performing Comparisons 128

Relational Operators 128
Equality Operators 129
Testing Floating-Point Numbers for Equality 130
Testing Objects for Equality 130
Comparing Strings 131

4.2 Logical Operators 132

Performing the And Operation 133
Performing the Or Operation 134
Performing the Not Operation 134
Precedence and Associativity of And, Or, and Not 135
Simplifying boolean Expressions 135

4.3 Simple if Statements 136

Indentation 137
The Empty Statement 138
Blocks 139

4.4 if Statements with else Clauses 140

Cascaded if Statements 142
Simplifying Cascaded if Statements 144
Program: Flipping a Coin 144
The "Dangling else" Problem 146

4.5 The boolean Type 148

Program: Flipping a Coin (Revisited) 150

4.6 Loops 151

Types of Loops 151
The while Statement 151
Blocks as Loop Bodies 153
Declaring Variables in Blocks 155
Example: Improving the Fraction Constructor 155

4.7 Counting Loops 156

Increment and Decrement Operators 158
Using the Increment and Decrement Operators in Loops 159
Program: Counting Coin Flips 161

4.8 Exiting from a Loop: The break Statement 163

4.9 Case Study: Decoding Social Security Numbers 165

4.10 Debugging 169

5 Arrays 181

5.1 Creating and Using Arrays 182

Creating Arrays 182
Array Subscripting 184
Processing the Elements in an Array 185
Program: Computing an Average Score 186

5.2 The for Statement 188

for Statements Versus while Statements 188
for Statement Idioms 190
Omitting Expressions in a for Statement 191
Declaring Control Variables 192
Commas in for Statements 193
Program: Computing an Average Score (Revisited) 193

5.3 Accessing Array Elements Sequentially 194

Searching for a Particular Element 195
Counting Occurrences 195
Finding the Largest and Smallest Elements 195

5.4 Accessing Array Elements Randomly 197

Program: Finding Repeated Digits in a Number 197

5.5 Using Arrays as Vectors 199

Scaling a Vector 199
Adding Vectors 200
Computing the Inner Product of Two Vectors 200

5.6 Using Arrays as Databases 200

Parallel Arrays 201
Arrays of Objects 202
Creating a Database 202
Adding a Record to a Database 203
Removing a Record from a Database 204
Searching a Database 204
Modifying a Record in a Database 204

5.7 Arrays as Objects 205

Resizing an Array 206

5.8 Case Study: A Phone Directory 208

6 Graphics 221

6.1 Creating a Drawing 222

Using Graphics Methods 223
The DrawableFrame Class 223
Program: Drawing a Line 226
Drawing Rectangles 227
Drawing Ovals 229
Drawing Arcs 229
Drawing Polygons 230
Drawing Polylines 232

6.2 Drawing in Color 233

6.3 Displaying Text 235

The Font Class 235
Program: Displaying Text in Different Colors 236

6.4 Combining Text with Graphics 237

Program: Displaying a Stop Sign 238
The FontMetrics Class 239
Program: Obtaining Font Information 241

6.5 Case Study: A Message Window 243

6.6 Debugging 246

7 Class Variables and Methods 257

7.1 Class Methods Versus Instance Methods 258

Class Methods 258
Example: The Math Class 260
Example: The System Class 260
Example: The String Class 260
Summary 261

7.2 Writing Class Methods 261

Example: A Termination Method 262
Local Variables 263

7.3 The return Statement 264

Example: A Dollar-Formatting Method 265
Conditional Expressions 266

7.4 Parameters 268

How Arguments Are Passed 268
Array Parameters 271
Program Arguments 272
Program: Printing Command-Line Arguments 273

7.5 Class Variables 274

Declaring Class Variables 274
Uses for Class Variables 275
Using Class Variables as Global Variables 275
Class Variables in the System Class 275
Using Class Variables as Constants 277
Using Class Variables to Store Data for Class Methods 277
Summary 278

7.6 Adding Class Variables and Methods to a Class 278

Example: Modifying the DisplayText Program 279

7.7 Writing Helper Methods 281

Improving Clarity 282
Reducing Redundancy 283
The Revised CourseAverage Program 283
Reusing Helper Methods 286

7.8 Designing Methods 287

Cohesion 287
Stepwise Refinement 287
Choosing Method Names 288
Parameters Versus Class Variables 289
Return Type 290

8 More Control Structures 299

8.1 Exceptions 300

Handling Exceptions 300
Variables and try Blocks 301
Accessing Information About an Exception 302
Terminating the Program After an Exception 302
Converting Strings to Integers 303
Multiple catch Blocks 304
Checked Exceptions Versus Unchecked Exceptions 304
Using Exceptions Properly 305

8.2 The switch Statement 306

Combining Case Labels 307
The default Case 308
The General Form of the switch Statement 309
Layout of the switch Statement 310
Advantages of the switch Statement 310
Limitations of the switch Statement 311
The Role of the break Statement 311
Program: Determining the Number of Days in a Month 312

8.3 The do Statement 314

8.4 The continue Statement 316

8.5 Nested Loops 318

Labeled break Statements 319
Labeled continue Statements 320
Program: Computing Interest 320

8.6 Case Study: Printing a One-Month Calendar 322

8.7 Debugging 327

9 Primitive Types 339

9.1 Types 340

Literals 340

9.2 Integer Types 341

Integer Literals 343
Converting Strings to Integers 345

9.3 Floating-Point Types 346

Floating-Point Literals 347
Special Values 348
The Math Class 349

9.4 The char Type 353

The ASCII Character Set 353
The Unicode Character Set 355
Escape Sequences 356
Operations on Characters 356
Characters Versus Strings 357
The Character Class 358

9.5 Type Conversion 361

Implicit Conversions 361
Numeric Promotion 362
Conversion During Assignment 363
Argument Conversion 364
Casting 364
Program: Encrypting a Message Using the Rot13 Technique 367

9.6 Case Study: Drawing Random Circles 370

10 Writing Classes 387

10.1 Designing Classes 388

Object-Oriented Analysis 389
Design Issues 389
Naming Classes 389
Instantiable or Not? 390
Mutable or Immutable? 390

10.2 Choosing Instance Variables 391

How Many Instance Variables? 391
Public Instance Variables 392
Information Hiding 393
The "Hidden Variable" Problem 393

10.3 Designing Instance Methods 395

Categories of Methods 396
Manager Methods 396
Implementor Methods 397
Access Methods 398
Helper Methods 398
Using Class Methods as Helpers 400
Method Overloading 401

10.4 The this Keyword 402

Using this to Access Hidden Variables 403
Using this to Call Instance Methods 403
Using this as an Argument 404
Using this as a Return Value 405

10.5 Writing Constructors 406

Using this in Constructors 406
"No-Arg" Constructors 407
Constructors Versus Instance Variable Initialization 407

10.6 Example: The Fraction Class 409

10.7 Adding Class Variables and Methods 413

Class Variables in Instantiable Classes 413
Class Methods in Instantiable Classes 415
Restrictions on Class Methods 416

10.8 Reusing Code 416

Reusing Class Methods 417
Reusing Classes 418
Writing Packages 419

10.9 Case Study: Playing Blackjack 420

10.10 Debugging 430

11 Subclasses 443

11.1 Inheritance 444

Writing a Subclass 445
Writing Subclass Constructors 446
Illustrating Inheritance 448
When Not to Use Inheritance 449

11.2 The protected Access Modifier 449

Uses of protected 451
protected Versus private 451

11.3 Overriding 452

11.4 Polymorphism 453

Casting Object References 455
Advantages of Polymorphism 456

11.5 The Object Class 457

Object Methods 458
The equals Method 459
The toString Method 460

11.6 Abstract Classes 461

Example: A Shape Class 462

11.7 Final Classes and Methods 465

11.8 Inheritance in the Abstract Window Toolkit 466

Components 467
Relationships Among Component Classes 469

11.9 Case Study: "Nervous" Shapes 471

12 The Abstract Window Toolkit 485

12.1 Overview 486

Swing 486
Creating a Graphical User Interface 487

12.2 Frames 488

Frame Methods 488
Creating a Frame 489
Setting the Location of a Frame 490
Adding Components to a Frame 490

12.3 Event Listeners 492

Events 493
Interfaces 493
Creating Event Listeners 494
Adapter Classes 497

12.4 Inner Classes 498

12.5 Attaching Listeners to Multiple Components 501

Example: A Single Listener 502
Example: Separate Listeners 504

12.6 Layout 506

Layout Manager Classes 506
The FlowLayout Class 507
The GridLayout Class 508
The BorderLayout Class 509
Preferred Sizes 510
Panels 510

12.7 Creating and Using Components 513

Checkboxes 513
Checkbox Groups 514
Choice Menus 514
Labels 515
Lists 516
Scrollbars 517
Text Areas 518
Text Fields 520

12.8 Examples 521

Using Labels and Text Fields: Temperature Conversion 521
Using Lists and Text Areas: Showing Definitions 524
Using Labels and Scrollbars: Picking Colors 526

13 Data Structures 541

13.1 Multidimensional Arrays 542

Creating and Initializing a Multidimensional Array 542
Subscripting a Multidimensional Array 544
Processing the Elements in a Multidimensional Array 544
How Multidimensional Arrays Are Stored 545
Using Two-Dimensional Arrays as Matrices 546
Using Two-Dimensional Arrays to Store Images 547
Ragged Arrays 548
Using Multidimensional Arrays as Parameters and Results 550
Program: Finding the Distance Between Two Cities 551

13.2 The Vector Class 552

Arrays Versus Vectors 559
Program: Reversing a Series of Lines 560

13.3 Wrapper Classes 562

Using Wrapper Classes to Create Objects 562
Using the Wrapper Classes with Vectors 563
Other Uses of Wrapper Classes 564

13.4 Sorting 564

Inserting into a Sorted Array 565
Insertion Sort 567
Program: Sorting a Series of Lines 568

13.5 Searching 570

Program: Determining Air Mileage 572

13.6 Sets 574

Bit Sets 575
Program: Finding Primes Using the Sieve of Eratosthenes 577

13.7 The StringBuffer Class 579

Program: Sorting the Characters in a String 582

13.8 Case Study: Finding Anagrams 583

14 Files 599

14.1 Files and Streams 600

How Files Are Stored 601
Text Files Versus Binary Files 602
Streams 603
Stream Layering 604
Working with Files 605
Obtaining File Names 606
Buffering and Flushing 606
File Pointers 607

14.2 The File Class 608

File Properties 609
Program: Determining the Properties of a File 610
Program: Listing the Files in a Directory 611
File Operations 612
Program: Renaming the Files in a Directory 612

14.3 Reading and Writing Bytes 613

Writing Bytes 613
Reading Bytes 615
Program: Copying a File 617

14.4 Advanced Exception-Handling 619

The Hierarchy of Exception Classes 619
Using Multiple catch Blocks 620
finally Blocks 622
The throws Clause 622
Program: Copying a File (Revisited) 623

14.5 Reading and Writing Data Types 625

Writing Data Types 625
Writing Strings 626
Reading Data Types 627

14.6 Reading and Writing Characters 629

Writing to a Text File 629
Reading from a Text File 630
Program: Converting Text to HTML 632

14.7 Reading and Writing Objects 636

The Serializable Interface 637
Writing Objects 638
Reading Objects 639
Reading and Writing Entire Data Structures 641

14.8 Case Study: A Phone Directory (Revisited) 642

Appendix A Setting Up Java 661

Appendix B Java Language Summary 665

Appendix C Java API Summary 681

Appendix D Applets 743

Appendix E The jpb Package 759

Bibliography 765

Index 767


Copyright © 2000 W. W. Norton & Company, Inc. All rights reserved.

Copyright © 2000-2006 K. N. King. All rights reserved.