Contents
Chapter 1 Introduction to Computers and Java1
1.1 Introduction2
1.2 Computers: Hardware and Software4
1.3 Data Hierarchy5
1.4 Computer Organization6
1.5 Machine Languages, Assembly Languages and High-Level Languages7
1.6 Introduction to Object Technology8
1.7 Operating Systems10
1.8 Programming Languages12
1.9 Java and a Typical Java Development Environment14
1.10 Test-Driving a Java Application17
1.11 Web 2.0: Going Social21
1.12 Software Technologies23
1.13 Keeping Up-to-Date with Information Technologies24
Contents
Chapter 1 Introduction to Computers and Java1
1.1 Introduction2
1.2 Computers: Hardware and Software4
1.3 Data Hierarchy5
1.4 Computer Organization6
1.5 Machine Languages, Assembly Languages and High-Level Languages7
1.6 Introduction to Object Technology8
1.7 Operating Systems10
1.8 Programming Languages12
1.9 Java and a Typical Java Development Environment14
1.10 Test-Driving a Java Application17
1.11 Web 2.0: Going Social21
1.12 Software Technologies23
1.13 Keeping Up-to-Date with Information Technologies24
1.14 Wrap-Up24
Chapter 2 Introduction to Java Applications29
2.1 Introduction29
2.2 Your First Program in Java: Printing a Line of Text30
2.3 Modifying Your First Java Program35
2.4 Displaying Text with printf36
2.5 Another Application: Adding Integers37
2.6 Memory Concepts41
2.7 Arithmetic42
2.8 Decision Making: Equality and Relational Operators45
2.9 Wrap-Up48
Chapter 3 Introduction to Classes, Objects, Methods and Strings58
3.1 Introduction58
3.2 Declaring a Class with a Method and Instantiating an Object of a Class59
3.3 Declaring a Method with a Parameter62
3.4 Instance Variables, set Methods and get Methods64
3.5 Primitive Types vs. Reference Types68
3.6 Initializing Objects with Constructors69
3.7 Floating-Point Numbers and Type double71
3.8 (Optional) GUI and Graphics Case Study: Using Dialog Boxes75
3.9 Wrap-Up77
Chapter 4 Control Statements: Part 184
4.1 Introduction85
4.2 Algorithms85
4.3 Pseudocode85
4.4 Control Structures86
4.5 if Single-Selection Statement88
4.6 if…else Double-Selection Statement89
4.7 while Repetition Statement92
4.8 Formulating Algorithms: Counter-Controlled Repetition93
4.9 Formulating Algorithms: Sentinel-Controlled Repetition97
4.10 Formulating Algorithms: Nested Control Statements103
4.11 Compound Assignment Operators107
4.12 Increment and Decrement Operators107
4.13 Primitive Types110
4.14 (Optional) GUI and Graphics Case Study: Creating Simple Drawings110
4.15 Wrap-Up113
Chapter 5 Control Statements: Part 2126
5.1 Introduction126
5.2 Essentials of Counter-Controlled Repetition127
5.3 for Repetition Statement128
5.4 Examples Using the for Statement131
5.5 do…while Repetition Statement135
5.6 switch Multiple-Selection Statement136
5.7 break and continue Statements142
5.8 Logical Operators144
5.9 Structured Programming Summary148
5.10 (Optional) GUI and Graphics Case Study: Drawing Rectangles and Ovals152
5.11 Wrap-Up154
Chapter 6 Methods: A Deeper Look164
6.1 Introduction165
6.2 Program Modules in Java165
6.3 static Methods, static Fields and Class Math166
6.4 Declaring Methods with Multiple Parameters168
6.5 Notes on Declaring and Using Methods171
6.6 Method-Call Stack and Activation Records172
6.7 Argument Promotion and Casting172
6.8 Java API Packages173
6.9 Case Study: Random-Number Generation175
6.10 Case Study: A Game of Chance; Introducing Enumerations179
6.11 Scope of Declarations182
6.12 Method Overloading184
6.13 (Optional) GUI and Graphics Case Study: Colors and Filled Shapes186
6.14 Wrap-Up188
Chapter 7 Arrays and ArrayLists201
7.1 Introduction202
7.2 Arrays202
7.3 Declaring and Creating Arrays203
7.4 Examples Using Arrays204
7.5 Case Study: Card Shuffling and Dealing Simulation212
7.6 Enhanced for Statement215
7.7 Passing Arrays to Methods216
7.8 Case Study: Class GradeBook Using an Array to Store Grades219
7.9 Multidimensional Arrays223
7.10 Case Study: Class GradeBook Using a Two-Dimensional Array226
7.11 Variable-Length Argument Lists230
7.12 Using Command-Line Arguments232
7.13 Class Arrays233
7.14 Introduction to Collections and Class ArrayList235
7.15 (Optional) GUI and Graphics Case Study: Drawing Arcs237
7.16 Wrap-Up240
Chapter 8 Classes and Objects: A Deeper Look259
8.1 Introduction260
8.2 Time Class Case Study260
8.3 Controlling Access to Members263
8.4 Referring to the Current Object’s Members with the this Reference264
8.5 Time Class Case Study: Overloaded Constructors266
8.6 Default and No-Argument Constructors270
8.7 Notes on Set and Get Methods271
8.8 Composition272
8.9 Enumerations274
8.10 Garbage Collection and Method finalize276
8.11 static Class Members277
8.12 static Import280
8.13 final Instance Variables281
8.14 Time Class Case Study: Creating Packages282
8.15 Package Access286
8.16 (Optional) GUI and Graphics Case Study: Using Objects with Graphics287
8.17 Wrap-Up290
Chapter 9 Object-Oriented Programming: Inheritance298
9.1 Introduction298
9.2 Superclasses and Subclasses299
9.3 protected Members301
9.4 Relationship between Superclasses and Subclasses302
9.5 Constructors in Subclasses318
9.6 Software Engineering with Inheritance319
9.7 Class Object320
9.8 (Optional) GUI and Graphics Case Study: Displaying Text and Images Using Labels320
9.9 Wrap-Up322
Chapter 10 Object-Oriented Programming: Polymorphism326
10.1 Introduction327
10.2 Polymorphism Examples328
10.3 Demonstrating Polymorphic Behavior329
10.4 Abstract Classes and Methods331
10.5 Case Study: Payroll System Using Polymorphism333
10.6 final Methods and Classes346
10.7 Case Study: Creating and Using Interfaces346
10.8 (Optional) GUI and Graphics Case Study: Drawing with Polymorphism356
10.9 Wrap-Up357
Chapter 11 Exception Handling: A Deeper Look362
11.1 Introduction363
11.2 Example: Divide by Zero without Exception Handling363
11.3 Example: Handling ArithmeticExceptions and InputMismatchExceptions365
11.4 When to Use Exception Handling369
11.5 Java Exception Hierarchy369
11.6 finally Block372
11.7 Stack Unwinding and Obtaining Information from an Exception Object375
11.8 Chained Exceptions378
11.9 Declaring New Exception Types379
11.10 Preconditions and Postconditions380
11.11 Assertions381
11.12 (New in Java SE 7) Multi-catch: Handling Multiple Exceptions in One catch382
11.13 (New in Java SE 7) try-with-Resources: Automatic Resource Deallocation382
11.14 Wrap-Up383
Chapter 12 ATM Case Study, Part 1: Object-Oriented Design with the UML388
12.1 Case Study Introduction388
12.2 Examining the Requirements Document389
12.3 Identifying the Classes in a Requirements Document395
12.4 Identifying Class Attributes400
12.5 Identifying Objects’ States and Activities404
12.6 Identifying Class Operations407
12.7 Indicating Collaboration Among Objects412
12.8 Wrap-Up418
Chapter 13 ATM Case Study Part 2: Implementing an Object-Oriented Design421
13.1 Introduction422
13.2 Starting to Program the Classes of the ATM System422
13.3 Incorporating Inheritance and Polymorphism into the ATM System425
13.4 ATM Case Study Implementation431
13.5 Wrap-Up449
Chapter 14 GUI Components: Part 1451
14.1 Introduction452
14.2 Java’s New Nimbus Look-and-Feel453
14.3 Simple GUI-Based Input/Output with JOptionPane454
14.4 Overview of Swing Components456
14.5 Displaying Text and Images in a Window457
14.6 Text Fields and an Introduction to Event Handling with Nested Classes461
14.7 Common GUI Event Types and Listener Interfaces466
14.8 How Event Handling Works467
14.9 JButton468
14.10 Buttons That Maintain State471
14.11 JComboBox; Using an Anonymous Inner Class for Event Handling476
14.12 JList479
14.13 Multiple-Selection Lists480
14.14 Mouse Event Handling482
1