Dr. Danny Poo graduated with a BSc (Hons), MSc and PhDin Computer Science from the University of Manchester Institute ofScience and Technology (UMIST), England. He is currently atenured Associate Professor in Me Department of Information Sys-tems, National University of Singapore. He has taught courses inSystems Analysis and Design, Enterprise Systems Development,Object-Oriented Software Engineering, and Information TechnologyProject Management. He is a Steering Committee Member of the Asia-Pacific Soft-ware Engineering Conference and founder and director of CicadaCube Pte Ltd, an NUS spin-off company specializing in Enterprise-level Search and RetrievalSolutions. A well-known speaker in seminars, he has conducted numerous in-house trainingand consultancy for organizations both locally and regionally. Dr. Poo is the author of 5 books: " Object-Oriented Programming andJava", 2nd edition, Springer-Verlag, 2007; "Developing Systems Using J2EE ",PrenticeHall, 2004, "Learn To Program Java", 4th edition, Cengage Learning, 2009 ;"Learn To Program Java User Interface", Thomson Learning, 2006 ; and "Learn ToProgram Enterprise JavaBeans 3.0", 3rd edition, Cengage Learning, 2009.
目录
CHAPTER 1 The Java Programming Environment 1.1 History of Java2 1.2 Preparing to Write Java Programs2 1.3 A Simple Java Program3 1.4 How to Run a Java Program?4 1.5 Commonly Encountered Problems5 Workshops5 Workshop 1.1: Preparing the Environment for Java Programming5 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 Exercises13
CHAPTER 2 The Java Language Components 2.1 Print2Numbers Java Program17 2.2 The Java Vocabulary and Character Sets19 2.3 Primitive Data Types19 2.3.1 Boolean20 2.3.2 Characters20 2.3.3 Integers21 2.3.4 Floating Point22 2.3.5 Object References22 2.3.6 String22 2.4 Identifiers22 2.5 Reserved Words23 2.6 Comments24 2.7 Basic Program Structure25 Workshops26 Workshop 2.1: Understanding the Sequence of Program Execution26 Exercises28
CHAPTER 3 Expressions, Statements and Operators 3.1 Expression Statements31 3.1.1 Types of Expressions31 3.1.2 Assignment Expression Statements31 3.1.3 Prefix or Postfix Forms of "++" and "――" Statements32 3.1.4 Method Call Statements32 3.1.5 Object Creation Statements32 3.2 Declaration Statements33 3.3 Operators33 3.3.1 Arithmetic Operators34 3.3.2 Auto-Increment and Auto-Decrement Operators35 3.3.3 Logical Operators35 3.3.4 Relational Operators37 3.3.5 Bitwise Operators39 3.3.6 The Conditional Operator "?:" 40 3.3.7 Assignment Operators41 3.3.8 "+" Operator43 3.3.9 "." Operator44 3.3.10 Precedence and Associativity44 Workshops47 Workshop 3.1: Entering Data for Program Execution47 Exercises53
CHAPTER 4 Program Flow Controls 4.1 Sequence56 4.2 Selection57 4.2.1 Block57 4.2.2 Types of Selection Statements59 4.3 Iteration64 4.3.1 The while Statement64 4.3.2 The do-while Statement66 4.3.3 The for Statement67 4.3.4 The Enhanced‘for’ Statement70 4.4 Labels70 4.5 The break Statement71 4.6 The continue Statement71 Exercises71
CHAPTER 5 Arrays 5.1 Array75 5.1.1 Declaring and Creating an Array75 5.1.2 Initializing an Array77 5.1.3 Using Arrays77 5.2 Two-dimensional Arrays79 5.2.1 One-dimensional Array Approach79 5.2.2 Two-dimensional Array Approach80 5.2.3 Populating Two-dimensional Arrays82 5.3 Applying the Enhanced‘for’ Statement in Arrays83 5.4 An Application: Printing Numbers Divisible by 384 5.4.1 Using Label and break Statement85 5.4.2 Using continue Statement88 Workshops90 Workshop 5.1: Copying Arrays90 Exercises96
CHAPTER 6 Methods 6.1 Defining a Problem98 6.2 A Problem Solving Approach99 6.3 Improving the Problem-Solving Approach103 6.3.1 Advantage of Using Methods107 6.3.2 Walking Through readInputValues() Method107 6.3.3 Walking Through convertMarksToGrades() Method107 6.3.4 Walking Through printDetails() Method107 6.4 Block Structure and Scope108 6.4.1 Local Variables108 6.4.2 Global Variables109 6.4.3 Determining Scope of Variables across Methods110 6.4.4 Distinguishing Local Variables from Global Variables111 6.4.5 Scope of Identifier Declaration112 6.5 Parameters113 6.5.1 Actual and Formal Parameters113 6.5.2 Value Parameters117 6.6 Methods that Return Values119 6.6.1 Returning Values119 6.6.2 The return Statement121 Workshops121 Workshop 6.1: Using Methods121 Exercises125
CHAPTER 7 Class and Objects 7.1 Class and Objects128 7.2 Constructing Objects128 7.2.1 Constructors129 7.2.2 Multiple Constructor Method Definition131 7.2.3 Constructor Method Invocation133 7.3 Instance and Class Variables134 7.4 Instance and Class Methods135 7.4.1 Instance Methods135 7.4.2 Class Methods136 7.5 Constants138 7.6 The this Keyword139 7.6.1 Using this Keyword in Instance Method140 7.6.2 Using this Keyword in Constructor141 7.7 Inner Class141 7.7.1 Compiling an Inner Class142 7.7.2 Static Inner Class143 7.7.3 Creating Inner Class Objects143 7.8 Class Hierarchy143 7.8.1 Superclass and Subclass144 7.8.2 Inheritance146 Workshops148 Workshop 7.1: Implementing Class and Objects148 Exercises153
CHAPTER 8 The Java Application Programming Interface (API) 8.1 Java Package157 8.1.1 The ‘package’ Keyword158 8.1.2 The ‘import’ Keyword159 8.1.3 File Name of a Public Class161 8.2 The Java?TM Platform Standard Edition 162 8.3 The Java API 162 8.3.1 The Java API Documentation163 8.3.2 The Java API Packages164 8.3.3 Directory Structure of Java API Packages167 8.3.4 The java.lang, java.io, and java.util Packages168 8.3.5 Reading the Java API Documentation168 8.3.6 Using the Java API169 8.4 The Ubiquitous System.out.println() Method170 8.4.1 The System Class170 8.4.2 The PrintStream Class171 8.5 String Tokenizers173 8.5.1 The java.util.StringTokenizer Class 173 8.5.2 Delimiter Characters 175 Exercises176
CHAPTER 9 Inputs and Outputs 9.1 Input and Output Streams178 9.1.1 Screen Outputs178 9.1.2 Keyboard Inputs179 9.1.3 Reading and Displaying Texts 180 9.2 Exception Handling181 9.2.1 Java Exception Handling 181 9.2.2 Explicit Exception Handling 182 9.3 The Scanner Class185 9.3.1 Creating a Scanner Object186 9.3.2 Handling Numerical Data Types186 9.3.3 Handling String Values186 9.3.4 Handling Boolean Values186 9.3.5 Exceptions and Delimiters187 9.3.6 A Scanner Class Application188 Exercises190
CHAPTER 10 File Handling 10.1 Text Files193 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 Files196 Exercises198
CHAPTER 11 Inheritance 11.1 The Inheritance Mechanism199 11.1.1 Subclass and Superclass199 11.1.2 java.lang.Object Class200 11.1.3 Downward Property Propagation200 11.2 Demonstrating Inheritance200 11.3 The super Keyword202 11.3.1 Syntax202 11.3.2 Constructor Chaining203 11.3.3 Calling Superclass Methods205 11.4 Method Overriding205 Exercises207
CHAPTER 12 Encapsulation 12.1 Access Modifiers: public, protected, private216 12.1.1 Using Access Modifiers216 12.1.2 Accessibility Effects217 12.2 Data Field Encapsulation218 12.3 Class Abstraction 219 12.4 Class Encapsulation220 12.4.1 Encapsulating a Class220 12.4.2 Enhanced Maintainability222 12.4.3 Bundling and Information Hiding225 Exercises226
CHAPTER 13 Polymorphism 13.1 Illustrating Polymorphism with Geometric Shapes229 13.1.1 The Triangle Class230 13.1.2 The Rectangle Class231 13.1.3 The GeometricShape Class233 13.1.4 The User Class: GeometricShapeMain Class234 13.2 Abstract Class 235 13.3 Dynamic Binding236 Exercises237
CHAPTER 14 Interface 14.1 The Interface Construct239 14.2 Interface Definition239 14.2.1 Interface Declaration and Interface Body240 14.2.2 Compilation of Interface241 14.2.3 Implementing Interface241 14.3 Understanding the Use of Interface243 14.4 What and How in the Use of Interface244 14.5 Application of Interface245 14.5.1 Sales Person Application245 14.5.2 SalesPerson and Employee Class247 14.5.3 Sort by Age: The main() Method 1248 14.5.4 Sort by Name: The main() Method 2250 14.5.5 Sort by Wage: The main() Method 3251 14.5.6 The Output251 14.6 The Serializable Interface252 14.7 Interface and Abstract Class257 14.8 Changes in Interface257 14.9 Uses of Interface258 Exercises258
CHAPTER 15 Sorting, Searching, and Recursion 15.1 Sorting264 15.1.1 Selection Sort264 15.1.2 Bubble Sort267 15.1.3 A Sorting Application269 15.2 Searching273 15.2.1 Linear Search273 15.2.2 Binary Search274 15.2.3 A Searching Application276 15.3 Recursion280 15.3.1 Recursive Method281 15.3.2 Writing a Recursive Method284 15.3.3 The Ubiquitous Factorial284 15.3.4 Applying Recursion285
摘要
The Java Application Programming Interface (API) Java is a very small language with only 46 reserved keywords but it is extended with a library of Java Application Programming Interfaces (Java APIs). It is from the Java APIs that the power of Java is felt. This chapter introduces the Java API and its accompanying documentation and explains how the Java API can be used to solve complicated problems in Java. Chapter 9: Inputs and Outputs This chapter explains how data input and output can be carried out in Java. Chapter 10: File Handling This chapter discusses how to read and write data from text and binary files. Chapter 11: Inheritance Inheritance is an object-oriented mechanism for realizing software reuse. A subclass in single class inheritance can inherit properties from a superclass. When a subclass inherits properties from more than one superclass, we have multiple class inheritance. This chapter discusses concepts related to single class inheritance. Chapter 12: Encapsulation Encapsulation is the bringing together of data fields and methods into an object definition with the effect of hiding the internal workings of the data fields and methods from the users of the object. Any direct access and updates to the object’s constituents is not permissible and changes to the data fields can only be carried out indirectly via a set of publicly available methods. This chapter focuses on data field encapsulation and class encapsulation. Chapter 13: Polymorphism The ability of objects of different subclass definition to respond to the same message is polymorphism. Polymorphism is only possible with dynamic binding-the capability of determining which method implementation to use for a method at runtime. This chapter explains the concept of polymorphism and its peripheral object-oreinted programming concepts. Chapter 14: Interface Sometimes it is necessary to derive a subclass from several classes but the Java extends keyword does not allow for more than one parent class. With interfaces, multiple class inheritance is possible. This chapter explains and shows how to use the Java interface construct to realize multiple class inheritance. Chapter 15: Sorting, Searching and Recursion This chapter discusses three topics commonly found in Java programming. They include: Sorting, Searching, and Recursion. Enjoy!