Thông tin tài liệu
Java Programming
Fundamentals
JVPG-SSMN-0101A
Java Programming Fundamentals
JVPG-SSMN-0101A
©1988-2000 Wave Technologies International, Inc.,
a Thomson Learning company.
Thomson Learning™ is a trademark used herein under license.
All rights reserved.
Printed in the United States of America. No part of this book may be used or reproduced in any
form or by any means, or stored in a database or retrieval system, without prior written
permission of the publisher. Making copies of any part of this book for any purpose other than
your own personal use is a violation of United States copyright laws. For information, contact
Wave Technologies International, Inc., 10845 Olive Blvd., Suite 250, St. Louis, Missouri
63141.
This book is sold as is, without warranty of any kind, either express or implied, respecting the
contents of this book, including, but not limited to, implied warranties for the book’s quality,
performance, merchantability, or fitness for any particular purpose. Neither Wave Technologies
International, Inc., nor its dealers or distributors shall be liable to the purchaser or any other
person or entity with respect to any liability, loss, or damage caused or alleged to be caused
directly or indirectly by this book.
Trademarks
Trademarks and registered trademarks of products mentioned in this book are held by the
companies producing them. Use of a term in this book should not be regarded as affecting the
validity of any trademark or service mark.
The Wave Technologies International, Inc. logo is a registered trademark of Wave Technologies
International, Inc., St. Louis, Missouri.
Copyrights of any screen captures in this book are the property of the software’s manufacturer.
Mention of any products in this book in no way constitutes an endorsement by Wave
Technologies International, Inc.
10 9 8 7 6 5 4 3 2 1
iii
SELF-STUDY
Contents
Introduction 1
Course Purpose 1
Course Goals 1
Exercises 2
Scenario-Based Learning 3
Multimedia Overview 3
Videos 3
Assessment 4
Hardware and Software Requirements 4
Chapter 1—Java Runtime Environment 5
Objectives 6
Pre-Test Questions 6
Introduction 7
The Java Virtual Machine 7
The Java 2 Software Development Kit 8
Installation 8
Windows 95/98/Me 9
Windows NT/2000 9
Linux 10
Creating a Simple, Stand-Alone Application 11
Creating a Simple Applet 13
Java Comments 14
Sun Certification 15
Summary 16
Post-Test Questions 16
Chapter 2—Data Types, Variables and Operators 17
Objectives 18
Pre-Test Questions 18
Introduction 19
Data Types 19
Variables 20
Default variable values 21
Variable declaration and initialization 21
Casting 22
iv Contents
Operators 24
Arithmetic operators 24
Relational operators 25
Logical operators 26
Precedence 27
Sun Certification 28
Summary 29
Post-Test Questions 30
Chapter 3—Control Statements 33
Objectives 34
Pre-Test Questions 34
Introduction 34
Code Blocks 35
Expressions 36
Conditional Statements 38
if statement 38
switch/case statement 40
Iteration (Loop) Statements 42
while loop (entry condition loop) 42
do while loop (exit condition loop) 43
for loop 43
Nested loops (break and continue) 44
Exercise 3-1: Using while and for loops 46
Sun Certification 47
Summary 48
Post-Test questions 48
Chapter 4—Methods 49
Objectives 50
Pre-Test Questions 50
Introduction 51
Methods 51
Return Statement 53
Calling a Method 55
Parameters 57
Pass by Value 58
Overloading 60
Exercise 4-1: Writing methods 62
Sun Certification 63
Summary 64
Post-Test Questions 64
Contents v
Chapter 5—Arrays 65
Objectives 66
Pre-Test Questions 66
Introduction 67
What Is an Array? 67
Initializing an Array 68
Using an Array 70
Passing an Array to a Method 74
Methods with an Array Return Type 75
Garbage Collection 76
Command Line Parameters 78
Exercise 5-1: Using arrays 79
Graduating Task #1: Creating a binary search 79
Sun Certification 81
Summary 83
Post-Test Questions 83
Chapter 6—Classes and Objects 85
Objectives 86
Pre-Test Questions 86
Introduction 87
Object-Oriented Programming 87
What Is a Class? 88
What Is an Object? 89
Instance and Class Members 89
Instance members 90
Class members 93
Abstraction 95
Object References 96
Exercise 6-1: Creating your own classes 98
Sun Certification 99
Summary 100
Post-Test Questions 101
Chapter 7—Inheritance 103
Objectives 104
Pre-Test Questions 104
Introduction 105
What Is Inheritance? 105
Using inheritance 106
Extending Classes 107
Using this and super() 110
The instanceof Operator 111
vi Contents
Overriding Methods 112
Exercise 7-1: Implementing inheritance 113
Sun Certification 114
Summary 116
Post-Test Questions 117
Chapter 8—Constructors 119
Objectives 120
Pre-Test Questions 120
Introduction 121
What Is a Constructor? 121
What can constructors do? 122
Using Constructors 123
This 126
this() as a constructor 126
Avoiding namespace conflicts 127
Super 128
Constructor Process 129
Constructors and Callbacks 130
Strings and StringBuffer 132
String constructors 132
String characteristics 133
Methods of String 135
StringBuffer 136
Exercise 8-1: Building constructors 137
Exercise 8-2: Implementing callbacks 138
Sun Certification 139
Summary 142
Post-Test Questions 143
Chapter 9—Interfaces and Abstract Classes 145
Objectives 146
Pre-Test Questions 146
Introduction 147
What Is an Interface? 147
Contents of an interface 148
Interface functions 149
Polymorphism 151
What Is an Abstract Class? 158
Graduating Task #2: Interfaces and Polymorphism 161
Sun Certification 161
Summary 162
Post-Test Questions 162
Contents vii
Chapter 10—Packages and Access Modifiers 163
Objectives 164
Pre-Test Questions 164
Introduction 165
Packages and Access Modifiers 165
Packages 165
Access modifiers 167
Comparison Between Java 1.1 And Java 2 168
Information Hiding 169
Encapsulation 171
Exercise 10-1: Using encapsulation, accessors and mutators 173
Sun Certification 173
Summary 174
Post-Test Questions 175
Chapter 11—Swing Components 177
Objectives 178
Pre-Test Questions 178
Introduction 179
What Is the AWT? 179
Heavyweight components (peer pattern) 180
AWT 1.1 180
What Is Swing? 181
Model View Controller (MVC) programming paradigm 182
Basic Swing Components 183
Graphical widgets 185
Containers 203
JavaBeans 207
Sun Certification 208
Summary 208
Post-Test Questions 209
Chapter 12—Layout Managers 211
Objectives 212
Pre-Test Questions 212
Introduction 213
What Is a Layout Manager? 213
FlowLayout 214
GridLayout 216
BorderLayout 218
BoxLayout 221
viii Contents
Swing 223
Strut 223
Glue 225
Combining layouts 227
Graduating Task #3: Creating sophisticated layouts 230
Sun Certification 231
Summary 232
Post-Test Questions 232
Chapter 13—Graphics 233
Objectives 234
Pre-Test Questions 234
Introduction 235
What Are Graphics in Java? 235
Graphics Class 236
drawString() 244
drawLine() 244
drawRect() 245
drawImage() 247
Color class 248
Font class 250
Exercise 13-1: Drawing to your Scribble JFrame 252
Sun Certification 253
Summary 253
Post-Test Questions 254
Chapter 14—The Delegation Model 257
Objectives 258
Pre-Test Questions 258
Introduction 259
What Is an Event? 259
SDK 1.3 Event Handling 260
Generating the event object 261
Sending the event object to the listener 261
Preparing the listener to receive the event 262
Example: Creating a closeable JFrame 263
JFrame convenience methods for event handling 268
Example: Event handling and callbacks 269
Sun Certification 274
Summary 274
Post-Test Questions 275
Contents ix
Chapter 15—Inner Classes 277
Objectives 278
Pre-Test Questions 278
Introduction 279
What Is an Inner Class? 279
Inner Classes for Event Handling 282
Graduating Task #4: Event-enabling the Scribble Application 286
Sun Certification 287
Summary 289
Post-Test Questions 290
Chapter 16—Applets 291
Objectives 292
Pre-Test Questions 292
Introduction 293
Applets and Web Browsers 294
JApplets 294
Applet life cycle 295
The <APPLET></APPLET> tags 296
Passing parameters to applets 298
Applets should not be trusted 300
Converting an Application into an Applet 301
Converting an Applet into an Application 306
Exercise 16-1: Converting the Scribble application into an applet 308
Sun Certification 308
Summary 309
Post-Test Questions 309
Chapter 17—Exceptions 311
Objectives 312
Pre-Test Questions 312
Introduction 313
What Is an Exception? 313
Errors 314
Exceptions 314
When Bad Things Happen to Good Programs 316
Ignoring the Exception 316
Handling the Exception 317
Throwing the Exception to the calling method 319
Handling and rethrowing the Exception 320
Creating and Throwing Your Own Exceptions 321
xContents
Exception Handling Tips 324
Sun Certification 325
Summary 326
Post-Test Questions 326
Chapter 18—Creating Threads and Thread Methods 327
Objectives 328
Pre-Test Questions 328
Introduction 329
What Are Threads? 329
How Operating Systems Handle Multitasking 330
Types of Threads in Java 330
Creating Threads 332
Subclassing the Thread class 332
Implementing the Runnable interface 335
Which technique? 337
Thread states 339
The currentThread(), getName() and sleep() methods 341
The setName() and setPriority() methods 343
The yield() method 345
Graduating Task #5: Creating a threaded digital clock 346
Sun Certification 347
Summary 347
Post-Test Questions 348
Chapter 19—Synchronization 351
Objectives 352
Pre-Test Questions 352
Introduction 353
What Is Thread Synchronization? 353
Thread Racing 353
Synchronized and the Object Monitor 355
Thread Race Condition 356
Competing for resources 357
Synchronizing the methods 359
Atomic processes 360
Sophisticated Thread Synchronization 361
Consumer/producer scenario 362
Deadlocks 368
Graduating Task #6: Enhancing the Digital Clock with Advanced
Thread Techniques 369
Sun Certification 370
Summary 370
Post-Test Questions 371
[...]... by going to the command prompt and invoking the Java interpreter java: java HelloWorld Note that you type java HelloWorld, not java HelloWorld.class When you enter the proper command, the JVM will display "Hello World!" in a command line window The Java application development cycle is shown in Figure 1-1 Text Editor javac * .java java *.class Figure 1-1: Java development cycle Output Window Creating... class You must save the program with your text editor as HelloWorld .java (giving it the same name as your class), and compile it using a Java compiler You can use the Javac program if you have the Java 2 Software Development Kit (SDK) supplied by Sun javac HelloWorld .java The Java compiler converts your source code (saved as HelloWorld .java, a text file) to the HelloWorld.class file This *.class file... a file compiled into bytecode 11 12 Chapter 1 Java Runtime Environment Bytecode is another element that makes Java different from other programming languages Other compiled programming languages generate machine dependent binaries, which are files that contain native machine language statements To make Java portable, the Javac compiler generates bytecode Java bytecode is composed of an instruction set... operating system In this chapter we will walk through a basic installation of the Java Development Kit (JDK) and the creation, compilation and execution of a small Java program THE JAVA VIRTUAL MACHINE The Java Virtual Machine (JVM) separates Java from most other programming languages One of the primary design goals of the Java language is to enable the same code to run on any platform The JVM is a software... polymorphism Describe method overloading and overriding Use Java static and instance members 1 Introduction 2 Create Java constructors Identify the differences between instance and class members Use Java abstract classes and interfaces Use Java Strings and StringBuffers Describe Java packages and accessibility Use the Java Abstract Windowing Toolkit (AWT) and Swing components... Microsoft Internet Explorer 5.5 or higher Java Development Kit version 1.2.2 or higher ( http:/ /java. sun.com ) 1 Java Runtime Environment MM AJJO R T O P IICCSS A OR P Objectives 6 Pre-Test Questions 6 Introduction 7 The Java Virtual Machine 7 The Java 2 Software Development Kit 8 Installation 8 Creating a Simple Applet 13 Java Comments 14 Sun Certification... comment /* … */ Javadoc comment /** … */ Sun Certification The following code shows examples of all three comment types: class HelloWorld { //** This is a javadoc comment It is a multiline commment unique to Java If you use the javadoc utility (which comes with Java) , it will automatically create HTML-based documentation for you The online help that comes with the Sun SDK was created using javadoc comments... interpretation, Java is considered to be a hybrid language Note that many compilers exist that will compile Java code into a native format If this is done, the compiled program will not be portable; however, the program will execute without the virtual machine 7 Chapter 1 Java Runtime Environment THE JAVA 2 SOFTWARE DEVELOPMENT KIT The development platform used in this course is the Java 2 Software... Edition, v 1.2.2 or higher The Java 2 SDK was previously known as the Java Development Kit (JDK) 1.2, and it is backward compatible with JDK 1.1 It contains several components, including: 8 The Java Runtime Environment (JRE), which provides the JVM The development tools and compilation libraries necessary to create Java applications and applets This grouping includes the Java compiler, debugging programs... open a Java- enabled browser (the latest Netscape or Internet Explorer should work) and point it to the file that you just saved It should open with the text “Hello World” in the window JAVA COMMENTS When programming, it will be useful to add comments to your code Java supports three types of comments; two will be recognizable to those familiar with C or C++, and the third type is unique to Java The . Java Programming
Fundamentals
JVPG-SSMN-0101A
Java Programming Fundamentals
JVPG-SSMN-0101A
©1988-2000 Wave. installation of the Java
Development Kit (JDK) and the creation, compilation and execution of a small Java
program.
THE JAVA VIRTUAL MACHINE
The Java Virtual
Ngày đăng: 14/03/2014, 16:20
Xem thêm: Java Programming Fundamentals pptx