Java the UML Way:
Integrating Object-Oriented Design and Programming
by Else Lervik and Vegard B. Havdal ISBN 0-470-84386-1
John Wiley & Sons, Ltd.
Table of Contents
Preface xi
1 Introduction 1
1.1 Preliminaries for Reading This Book 2
1.2 Contemplating a Computer 3
1.3 Java Applications and Java Applets 5
1.4 JavaScript and JSP 6
1.5 How This Book is Structured 7
1.6 A Small Example Program 9
1.7 Examples of Applets 15
1.8 New Concepts in This Chapter 17
1.9 Review Problems 19
1.10 Programming Problems 20
2 Variables, Data Types, and Expressions 21
2.1 Example 22
2.2 Data and Variables 22
2.3 Algorithms, Programming Errors, and Test Data 26 2.4 Statements, Blocks, and Names 28
2.5 Variables and Constants 30
2.6 Data Types 34
2.7 Assignments and Mathematical Expressions 40
2.8 Type Conversion 43
2.9 Calculations for Our Renovation Project 45 2.10 New Concepts in This Chapter 47
2.11 Review Problems 49
2.12 Programming Problems 49
3 Using Ready-Made Classes 51
3.1 Objects as Models of Reality 52
3.2 Using Ready-Made Classes 55
3.3 The Random Class 60
3.4 The String Class 63
3.5 Organizing Classes in Packages 70 3.6 Class Methods and Class Constants in the Java Library 71
3.7 Reading Data from the User 73
3.8 New Concepts in This Chapter 77
3.9 Review Problems 79
3.10 Programming Problems 79
4 Constructing Your Own Classes 81
4.1 Creating Classes 82
4.2 Programming a Class 85 4.3 Access Modifiers – Private and Public 91
4.4 Contents of a Class 92
4.5 One More Class and Some New Operators 101
4.6 Introduction to Applets 106
4.7 Introduction to Graphics 111
4.8 New Concepts in This Chapter 118
4.9 Review Problems 120
4.10 Programming Problems 121
5 Selection as a Control Structure 123
5.1 A Simple Calculator 124
5.2 A Selection is a Control Structure 126
5.3 Blocks inside Methods 131
5.4 The if Statement 133 5.5 Nested if and Multiple-Choice Statements 137
5.6 Boolean Expressions 144
5.7 The Multiple-Choice Statement switch 150
5.8 Comparing Computed Decimal Numerals 153
5.9 The Conditional Operator ?: 153
5.10 New Concepts in This Chapter 154
5.11 Review Problems 155
5.12 Programming Problems 156
6 Loops as a Control Structure 159
6.1 Counter-Controlled Loops 160
6.2 A Loop with a General Condition 163
6.3 A Graphics Example 166
6.4 The for Statement 168
6.5 Nested Control Structures 169
6.6 The do-while Statement 171 6.7 Choosing the Right Loop Statement 173
6.8 Controlling Input Data 174
6.9 New Concepts in This Chapter 177
6.10 Review Problems 178
6.11 Programming Problems 178
7 Collaboration Between Objects 181
7.1 Examples of Collaboration Between Objects 182
7.2 A Menu-Driven Program 190
7.3 Several References to the Same Object 197
7.4 Summary: Argument Transfer 202
7.5 New Concepts in This Chapter 205
7.6 Review Problems 205
7.7 Programming Problems 205
8 Java Libraries and Exception Handling 207
8.1 The Online API Documentation 208
8.2 Making Your Own Libraries 211
8.3 Localization 214
8.4 Sound and Images 218 8.5 Introduction to Exception Handling 220
8.6 Exception Handling in Detail 226
8.7 New Concepts in This Chapter 236
8.8 Review Problems 236
8.9 Programming Problems 237
9 Arrays of Primitive Data Types 239
9.1 What is an Array? 240
9.2 Copying Arrays 244
9.3 The Month Class for Precipitation Data 247
9.4 Sorting 251
9.5 Searching 255
9.6 The java.util.Arrays Class 256
9.7 Two-Dimensional Arrays 258
9.8 More Than Two Dimensions 265
9.9 Multidimensional Arrays and Object-Oriented Programming 266 9.10 New Concepts in This Chapter 267
9.11 Review Problems 268
9.12 Programming Problems 268
10 Arrays of Reference Types and Array Lists 271
10.1 An Array of Reference Type 272
10.2 Array Lists 275
10.3 Wrapper Classes – Integer, Double, etc. 278
10.4 The Methods – equals() and toString() 281
10.5 Associations 283
10.6 A Bigger Example 285
10.7 The Comparable and Comparator Interfaces 296 10.8 Sorting Arrays and Array Lists 298
10.9 New Concepts in This Chapter 304
10.10 Review Problems 305
10.11 Programming Problems 305
11 Using Data Files and Streams 307
11.1 Data Files and Streams 308
11.2 An Example of a Data File 309
11.3 Reading Text from a File 311
11.4 Writing Text to a File 315
11.5 Data Files: Summary and Class Descriptions 316 11.6 Reading Numbers from a Data File 320 11.7 Communication with the Console 323
11.8 Binary Transfer of Data 323
11.9 Random Access to the Contents of a File 325
11.10 Serialization 329
11.11 New Concepts in This Chapter 335
11.12 Review Problems 355
11.13 Programming Problems 336
12 Inheritance and Polymorphism 339
12.1 Generalization and Specialization 340
12.2 Inheritance 342 12.3 The Material Class with Subclasses 345
12.4 Handling Instances of Subclasses as a Whole 351 12.5 The Renovation Example with Many Surfaces and Many Materials 354 12.6 What if Polymorphism Didn’t Exist? 357 12.7 The protected Access Modifier 360
12.8 Two Levels of Inheritance 364
12.9 Rules and Syntax 369
12.10 Interface 372
12.11 New Concepts in This Chapter 376
12.12 Review Problems 377
12.13 Programming Problems 378
13 GUI Programming and Events 383
13.1 GUI Components 384
13.2 Pushing a Button 388
13.3 Inner Classes 395
13.4 Managing the Layout 402
13.5 New Concepts in This Chapter 412
13.6 Review Problems 413
13.7 Programming Problems 413
14 Texts, Choices, and Windows 415
14.1 Text Components and Focus Listeners 416 14.2 Giving the User a Choice Between Alternatives 423
14.3 Choices Using Check Boxes 425
14.4 Choices Using Radio Buttons 428
14.5 Choices Using Lists 431
14.6 Windows 439
14.7 Making a Window 441
14.8 Differences Between Applets and Applications 446 14.9 Other Ways to Program Listeners 448 14.10 New Concepts in This Chapter 450
14.11 Review Problems 451
14.12 Programming Problems 451
15 Creating User Interfaces 453
15.1 Menus 453
15.2 Toolbars 458
15.3 Dialog Windows 461
15.4 GridBagLayout as Layout Manager 476
15.5 The Table GUI Component (the JTable Class) 481 15.6 GUI for the Renovation Project 484
15.7 New Concepts in This Chapter 495
15.8 Review Problems 495
15.9 Programming Problems 496
16 Threads 499
16.1 Threads in Processes 500
16.2 Dividing Time Between Threads 502
16.3 Example of Threads in Use 503
16.4 Thread Conditions 507 16.5 Communication Between Threads 508
16.6 Locks and Synchronization 510
16.7 More Control: wait(), notify(), and notifyAll() 515 16.8 Peeking at the Threads with JDB 519
16.9 New Concepts in This Chapter 521
16.10 Review Problems 522
16.11 Programming Problem 522
17 Data Structures and Algorithms 523
17.1 Graphs 524
17.2 Lists 526
17.3 The Solution: Collection, List, and LinkedList 534
17.4 Queues and Stacks 538
17.5 Recursion 540
17.6 Trees 542
17.7 Trees in the API 550
17.8 Hashtables 553
17.9 New Concepts in This Chapter 558
17.10 Review Problems 559
17.11 Programming Problems 559
18 More about Applets 561
18.1 What Applets Do on the Web 561
18.2 Security 562
18.3 Programming an Applet 565
18.4 Security in Practice 570
18.5 Communication Between Applet and Browser 572 18.6 New Concepts in This Chapter 575
18.7 Review Problems 576
18.8 Programming Problems 576
19 Distributed Systems with Socket Programming and RMI 579
19.1 Sockets 580
19.2 Objects That Collaborate over a Network 586 19.3 How Does Communication Between the Objects Occur? 594
19.4 RMI and Applets 600
19.5 Deployment Diagram 600
19.6 A Distributed System with Callback 603 19.7 New Concepts in This Chapter 614
19.8 Review Problems 615
19.9 Programming Problems 615
20 Programming with Databases 619
20.1 Database Drivers 620
20.2 Establishing Contact with a Database 621
20.3 A Bigger Example 628
20.4 A Database Application 634
20.5 The Three-Layer Architecture 640 20.6 Transactions and Compiled SQL Statements 641 20.7 New Concepts in This Chapter 645
20.8 Review Problems 645
20.9 Programming Problems 646
21 Web Programming with JavaServer Pages 649
21.1 Different Ways of Programming for the Web 650
21.2 Installing Software 651
21.3 Servlets 652
21.4 JavaServer Pages (JSP) 657
21.5 What Does JSP Consist of? 658
21.6 Inputting Data from the User 662 21.7 Client-Side Validation with JavaScript 671
21.8 Databases 672
21.9 Storing State Information 680
21.10 New Concepts in This Chapter 695
21.11 Review Problems 696
21.12 Programming Problems 697
APPENDICES
A Using Java SDK and WinEdit 699
A.1 SDK 699
A.2 Running Applets 703
A.3 WinEdit 703
B Keywords 705
C Number Systems 707
D The Unicode Character Set 711
E HTML and Applets 713
F Exceptions to the Code Standard 717
G References 719
Index 721