CONTENTS

List of examples and case studies xii

Preface to the third edition xv

The development of Java Gently xxii

Chapter 1 Introduction

1.1 Welcome to Java 1
1.2 Software development 6
1.3 The approach of this book 8
1.4 Getting started with Java 11
1.5 The Java Gently web site 14
Summary 15
Quiz: to get you started 15

Chapter 2 Simple programs

2.1 Three starter programs 16
2.2 The structure of a program 22
2.3 Basics of object orientation 28
2.4 Beginning with output 36
2.5 Simple calculations 40
Summary 48
Quiz 50
Problems 51

Chapter 3 Types and methods

3.1 Talking about types 54
3.2 Repetition with for-loops 60
3.3 Making methods 68
3.4 Passing parameters and returning results 70
3.5 Typed methods 77
3.6 The type boolean 85
3.7 Case Study 1: Pizza Delivery 2U 89
Summary 105
Quiz 107
Problems 108

Chapter 4 Input and output

4.1 Inputting interactively 113
4.2 User-friendly input-output 117
4.3 Creating and accessing packages 128
4.4 Selection with if-else 131
4.5 File input and output 139
4.6 Handling exceptions 143
Summary 154
Quiz 155
Problems 157

Chapter 5 Controlling the flow

5.1 Properties of a good program 161
5.2 Conditional loops with while- and do-statements 166
5.3 User-defined exceptions 178
5.4 The type char 181
5.5 The switch-statement 184
5.6 Case Study 2: Rock-scissors-paper game 188
Summary 196
Quiz 197 Problems 198

Chapter 6 Arrays and tables

6.1 Simple arrays 202
6.2 Tables 224
6.3 The Graph class 230
6.4 Sorting and searching 238
6.5 Class-independent tables 244
Summary 253
Quiz 254
Problems 255

Chapter 7 Formatting

7.1 Strings and string handling 259
7.2 Tokenizers and envelopes 269
7.3 Inside the Stream class 272
7.4 Formatting numbers 279
7.5 Dates, calendars and time 288
Summary 301
Quiz 302
Problems 303

Chapter 8 Objects at work

8.1 Designing an object-oriented program 306
8.2 Properties of objects 317
8.3 Lists and iterators 329
8.4 Case Study 3: Nelson's Coffee Shop 333
Summary 344
Quiz 345
Problems 346

Chapter 9 Abstraction and inheritance

9.1 Class power 348
9.2 Inheritance 351
9.3 Abstraction through interfaces 356
9.4 Abstract methods and classes 363
9.5 Case Study 4: Veterinary Tags 365
9.6 Serialization 373
Summary 379
Quiz 380
Problems 382

Chapter 10 Graphics and user interfaces

10.1 Introduction to the awt and Swing 383
10.2 Putting graphics in a window 386
10.3 Laying out a GUI 399
Summary 413
Quiz 414
Problems 415

Chapter 11 Event-driven programming

11.1 Interaction with text fields 418
11.2 Events, listeners and handlers 422
11.3 Case Study 5: The Flag Designer 424
11.4 Managing sequences of events 434
11.5 Case Study 6: Supermarket Till 444
11.6 Case Study 7: The Converter with Swing 452
Summary 461
Quiz 461
Problems 463

Chapter 12 Applets in action

12.1 From applications to applets 464
12.2 Applets in browsers 470
12.3 Sound and images 486
12.4 Case Study 8: The Nature Conservation Project 493
Summary 503
Quiz 504
Problems 506

Chapter 13 Multithreading

13.1 Introduction to multithreading 507
13.2 Setting up threads 509
13.3 Synchronization among threads 527
13.4 Inside the Display class 533
13.5 Case Study 9: Walkman Hire 540
Summary 547
Quiz 548
Problems 549

Chapter 14 Networking

14.1 Connecting via the Internet 551
14.2 Ports and sockets 558
14.3 Case Study 10: The Chatter system 565
14.4 Database connectivity 571
14.5 Accessing remote objects 582
14.6 Case Study 11: The Airport Announcer system 587
Summary 596
Quiz 597
Problems 599

Chapter 15 Data structures and algorithms

15.1 About data structures 601
15.2 Linear and binary searching 604
15.3 Quicksort and performance 611
15.4 Stacks and queues 615
15.5 Linked lists again 626
15.6 Bit sets 632
Summary 639
Quiz 639
Problems 641

Appendix A Modelling notation for Java Gently

Appendix B Collected syntax and semantic forms

Appendix C List of programs, applets and classes

Appendix D Answers to quizzes

Index