CHAPTER 1 The Java Programming Environment1.1 History of Java 2
1.2 Preparing to Write Java Programs 2
1.3 A Simple Java Program 3
1.4 How to Run a Java Program? 4
1.5 Commonly Encountered Problems 5
Workshops 5
Workshop 1.1:Preparing the Environment for Java Programming 5
Workshop 1.2:How to Run a Java Program? 10
Workshop 1.3:How to Compile and Run a Java Program in DOS Prompt? 12
Exercises 13
CHAPTER 2 The Java Language Components2.1 Print2Numbers Java Program 17
2 2 The Java Vocabulary and Character Sets 19
2.3 Primitive Data Types 19
2.3.1 Boolean 20
2.3.2 Characters 20
2.3.3 Integers 21
2.3.4 Floating Point 22
2.3.5 Object References 22
2.3.6 String 22
2.4 Identifiers 22
2.5 Reserved Words 23
2.6 Comments 24
2.7 Basic Program Structure 25
Workshops 26
Workshop 2.1:Understanding the Sequence of Program Execution 26
Exercises 28
CHAPTER 3 Expressions,Statements and Operators3.1 Expression Statements 31
3.1.1 Types of Expressions 31
3.1.2 Assignment Expression Statements 31
3.1.3 Prefix or Postfix Forms of“++”and“--”Statements 32
3.1.4 Method Call Statements 32
3.1.5 Object Creation Statements 32
3.2 Declaration Statements 33
3.3 Operators 33
3.3.1 Arithmetic Operators 34
3.3.2 Auto-Increment and Auto-Decrement Operators 35
3.3.3 Logical Operators 35
3.3.4 Relational Operators 37
3.3.5 Bitwise Operators 39
3.3.6 The Conditional Operator“?:” 40
3.3.7 Assignment Operators 41
3.3.8 “+”Operator 43
3.3.9 “.”Operator 44
3.3.10 Precedence and Associativity 44
Workshops 47
Workshop 3.1:Entering Data for Program Execution 47
Exercises 53
CHAPTER 4 Program Flow Controls 56
4.1 Sequence 56
4.2 Selection 57
4.2.1 Block 57
4.2.2 Types of Selection Statements 59
4.3 Iteration 64
4.3.1 The while Statement 64
4.3.2 The do-while Statement 66
4.3.3 The for Statement 67
4.3.4 The Enhanced'for'Statement 70
4.4 Labels 70
4 5 The break Statement 71
4.6 The continue Statement 71
Exercises 71
CHAPTER 5 Arravs 75
5.1 Array 75
5.1.1 Declaring and Creating an Array 75
5.1.2 Initializing an Array 77
5.1.3 Using Arrays 77
5.2 Two-dimensional Arrays 79
5.2.1 One-dimensional Array Approach 79
5.2.2 Two-dimensional Array Approach 80
5.2.3 Populating Two-dimensional Arrays 82
5.3 Applying the Enhanced'for'Statement in Arrays 83
5.4 An Application:Printing Numbers Divisible by 3 84
5.4.1 Using Label and break Statement 85
5.4.2 Using continue Statement 88
Workshops 90
Workshop 5.1:Copying Arrays 90
Exercises 96
CHAPTER 6 Methods 98
6.1 Defining a Problem 98
6.2 A Problem Solving Approach 99
6.3 Improving the Problem-Solving Approach 103
6.3.1 Advantage of Using Methods 107
6.3.2 Walking Through readInputValues()Method 107
6.3.3 Walking Through convertMarksToGrades()Method 107
6.3.4 Walking Through printDetails()Method 107
6.4 Block Structure and Scope 108
6.4.1 Local Variables 108
6.4.2 Global Variables 109
6.4.3 Determining Scope of Variables across Methods 110
6.4.4 Distinguishing Local Variables from Global Variables 111
6.4.5 Scope of Identifier Declaration 112
6.5 Parameters 113
6.5.1 Actual and Formal Parameters 113
6.5.2 Value Parameters 117
6.6 Methods that Return Values 119
6.6.1 Returning Values 119
6.6.2 The return Statement 121
Workshops 121
Workshop 6.1:Using Methods 121
Exercises 125
CHAPTER 7 Class and Objects 128
7.1 Class and Objects 128
7.2 Constructing Objects 128
7. 2.1 Constructors 129
7.2.2 Multiple Constructor Method Definition 131
7.2.3 Constructor Method Invocation 133
7.3 Instance and Class Variables 134
7.4 Instance and Class Methods 135
7.4.1 Instance Methods 135
7.4.2 Class Methods 136
7.5 Constants 138
7.6 The this Keyword 139
7.6.1 Using this Keyword in Instance Method 140
7.6.2 Using this Keyword in Constructor 141
7.7 Inner Class 141
7.7.1 Compiling an Inner Class 142
7.7.2 Static Inner Class 143
7.7.3 Creating Inner Class Objects 143
7.8 Class Hierarchy 143
7.8.1 Superclass and Subclass 144
7.8.2 Inheritance 146
Workshops 148
Workshop 7.1:Implementing Class and Objects 148
Exercises 153
CHAPTER 8 The Java Application Programming Interface(API)8.1 Java Package 157
8.1.1 The'package'Keyword 158
8.1.2 The'import'Keyword 159
8.1.3 File Name of a Public Class 161
8.2 The JavaTM Platform Standard Edition 162
8.3 The Java API 162
8.3.1 The Java API Documentation 163
8.3.2 The Java API Packages 164
8.3.3 Directory Structure of Java API Packages 167
8.3.4 The java.lang,java.io,and java.util Packages 168
8.3.5 Reading the Java API Documentation 168
8.3.6 Using the Java API 169
8.4 The Ubiquitous System.out.println()Method 170
8.4.1 The System Class 170
8.4.2 The PrintStream Class 171
8.5 String Tokenizers 173
8.5.1 The java.util.StringTokenizer Class 173
8.5.2 Delimiter Characters 175
Exercises 176
CHAPTER 9 Inputs and Outputs 178
9.1 Input and Output Streams 178
9.1.1 Screen Outputs 178
9.1.2 Keyboard Inputs 179
9.1.3 Reading and Displaying Texts 180
9.2 Exception Handling 181
9.2.1 Java Exception Handling 181
9.2.2 Explicit Exception Handling 182
9.3 The Scanner Class 185
9.3.1 Creating a Scanner Object 186
9.3.2 Handling Numerical Data Types 186
9.3.3 Handling String Values 186
9.3.4 Handling Boolean Values 186
9.3.5 Exceptions and Delimiters 187
9.3.6 A Scanner Class Application 188
Exercises 190
CHAPTER 10 File Handing 193
10.1 Text Files 193
10.1.1 Writing to a Text File 193
10.1.2 Appending Texts to a File 195
10.1.3 Reading from a File 195
10.2 Binary Files 196
Exercises 198
CHAPTER 11 Inheritance 199
11.1 The Inheritance Mechanism 199
11.1.1 Subclass and Superclass 199
11.1.2 java.lang.Object Class 200
11.1.3 Downward Property Propagation 200
11.2 Demonstrating Inheritance 200
11.3 The super Keyword 202
11.3.1 Syntax 202
11.3.2 Constructor Chaining 203
11.3.3 Calling Superclass Methods 205
11.4 Method Overriding 205
Exercises 207
CHAPTER 12 Encapsulation 216
12.1 Access Modifiers:public,protected,private 216
12.1.1 Using Access Modifiers 216
12.1.2 Accessibility Effects 217
12.2 Data Field Encapsulation 218
12.3 Class Abstraction 219
12.4 Class Encapsulation 220
12.4.1 Encapsulating a Class 220
12.4.2 Enhanced Maintainability 222
12.4.3 Bundling and Information Hiding 225
Exercises 226
CHAPTER 13 Polymorphism 229
13.1 Illustrating Polymorphism with Geometric Shapes 229
13.1.1 The Triangle Class 230
13.1.2 The Rectangle Class 231
13.1.3 The GeometricShape Class 233
13.1.4 The User Class:GeometricShapeMain Class 234
13.2 Abstract Class 235
13.3 Dynamic Binding 236
Exercises 237
CHAPTER 14 Interface 239
14.1 The Interface Construct 239
14.2 Interface Definition 239
14.2.1 Interface Declaration and Interface Body 240
14.2.2 Compilation of Interface 241
14.2.3 Implementing Interface 241
14.3 Understanding the Use of Interface 243
14.4 What and How in the Use of Interface 244
14.5 Application of Interface 245
14.5.1 Sales Person Application 245
14.5.2 SalesPerson and Employee Class 247
14.5.3 Sort by Age:The main()Method 1 248
14.5.4 Sort by Name:The main()Method 2 250
14.5.5 Sort by Wage:The main()Method 3 251
14.5.6 The OutPut 251
14.6 The Serializable Interface 252
14.7 Interface and Abstract Class 257
14.8 Changes in Interface 257
14 9 Uses of Interface 258
Exercises 258
CHAPTER 15 Sorting,Searching,and Recursion15.1 Sorting 264
15.1.1 Selection Sort 264
15.1.2 Bubble Sort 267
15.1.3 A Sorting Application 269
15.2 Searching 273
15.2.1 Linear Search 273
15.2.2 Binary Search 274
15.2.3 A Searching Application 276
15.3 Recursion 280
15.3.1 Recursive Method 281
15.3.2 Writing a Recursive Method 284
15.3.3 The Ubiquitous Factorial 284
15.3.4 Applying Recursion 285