This content was uploaded by our users and we assume good faith they have the permission to share this book. If you own the copyright to this book and it is wrongfully on our website, we offer a simple DMCA procedure to remove your content from our site. Start by pressing the button below!
Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark.
Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book.
INTERIOR DESIGNER Gary Adair
COVER DESIGNER Aren Howell
PAGE LAYOUT D&G Limited, LLC
Contents at a Glance Introduction
Part I: Getting Started Hour 1
Becoming a Programmer
1
3 5
2
Writing Your First Program
19
3
Vacationing in Java
33
4
Understanding How Java Programs Work
51
Part II: Learning the Basics of Programming
63
Hour 5
Storing and Changing Information in a Program
65
6
Using Strings to Communicate
83
7
Using Conditional Tests to Make Decisions
97
8
Repeating an Action with Loops
Part III: Working with Information in New Ways Hour 9
115
129
Storing Information with Arrays
131
10
Creating Your First Object
147
11
Describing What Your Object Is Like
163
12
Making the Most of Existing Objects
181
Part IV: Programming a Graphical User Interface Hour 13
197
Building a Simple User Interface
199
14
Laying Out a User Interface
217
15
Responding to User Input
231
16
Building a Complex User Interface
251
Part V: Creating Multimedia Programs Hour 17
269
Creating Interactive Web Programs
271
18
Handling Errors in a Program
289
19
Creating a Threaded Program
305
20
Reading and Writing Files
323
Part VI: Creating Multimedia Programs Hour 21
337
Using Fonts and Color
339
22
Playing Sound Files
353
23
Working with Graphics
367
24
Creating Animation
387
Appendixes
401
A
Tackling New Features of Java 2 Version 1.4
403
B
Using the Java 2 Software Development Kit
415
C
Programming with the Java 2 Software Development Kit
441
D
Using Sun ONE Studio
465
E
Where to Go from Here: Java Resources
477
F
This Book’s Web Site
485
Index
487
Contents Introduction
PART I Getting Started HOUR 1 Becoming a Programmer
1
3 5
Choosing a Language ............................................................................................6 Telling the Computer What to Do ..........................................................................8 How Programs Work ............................................................................................10 How Programs Don’t Work ..................................................................................12 Next Stop: Java ....................................................................................................12 Official Documentation ..................................................................................13 Workshop: Installing a Java Development Tool ..................................................14 Summary ..............................................................................................................14 Q&A ......................................................................................................................15 Quiz ......................................................................................................................16 Questions ........................................................................................................16 Answers ..........................................................................................................17 Activities ..............................................................................................................17 HOUR 2 Writing Your First Program
19
What You Need to Write Programs ......................................................................20 Creating the Saluton Program ..............................................................................21 Beginning the Program ........................................................................................21 The class Statement ......................................................................................22 What the main Statement Does ......................................................................22 Those Squiggly Bracket Marks ......................................................................22 Storing Information in a Variable ........................................................................23 Displaying the Contents of a Variable ............................................................24 Saving the Finished Product ................................................................................24 Compiling the Program into a Class File ............................................................25 Fixing Errors ........................................................................................................26 Workshop: Running a Java Program ....................................................................27 Summary ..............................................................................................................28 Q&A ......................................................................................................................29 Quiz ......................................................................................................................30 Questions ........................................................................................................30 Answers ..........................................................................................................31 Activities ..............................................................................................................31
vi
Sams Teach Yourself Java 2 in 24 Hours
HOUR 3 Vacationing in Java
33
First Stop: Sun Microsystems ..............................................................................34 A Brief History of Java ..................................................................................35 Going to School with Java ....................................................................................37 Lunch in JavaWorld ..............................................................................................39 Taking in a Ballgame at ESPN.com ....................................................................42 Getting Down to Business ....................................................................................43 Stopping by Gamelan to Ask Directions ..............................................................44 Workshop: Putting Java on Your Desktop ............................................................46 Summary ..............................................................................................................47 Q&A ......................................................................................................................47 Quiz ......................................................................................................................48 Questions ........................................................................................................48 Answers ..........................................................................................................49 Activities ..............................................................................................................49 HOUR 4 Understanding How Java Programs Work
51
Creating an Application ........................................................................................52 Sending Arguments to Applications ....................................................................53 Applet Basics ........................................................................................................54 Sending Parameters to Applets ............................................................................57 Workshop: Viewing the Code Used to Run Applets ........................................................................................................59 Summary ..............................................................................................................60 Q&A ......................................................................................................................60 Quiz ......................................................................................................................61 Questions ........................................................................................................61 Answers ..........................................................................................................62 Activities ..............................................................................................................62
PART II Learning the Basics HOUR 5 Storing and Changing Information in a Program
63 65
Statements and Expressions ..................................................................................66 Assigning Variable Types ....................................................................................66 Integers and Floating-Point Numbers ............................................................67 Characters and Strings ....................................................................................68 Other Numeric Variable Types ........................................................................69 The boolean Variable Type ............................................................................70 Naming Your Variables ........................................................................................71 Storing Information in Variables ..........................................................................71 Workshop: Using Expressions ..............................................................................72
Contents
vii
All About Operators ..............................................................................................75 Incrementing and Decrementing a Variable ....................................................76 Operator Precedence ........................................................................................78 Summary ..............................................................................................................79 Q&A ......................................................................................................................80 Quiz ......................................................................................................................81 Questions ........................................................................................................81 Answers ..........................................................................................................81 Activities ..............................................................................................................81 HOUR 6 Using Strings to Communicate
83
Storing Text in Strings ..........................................................................................84 Displaying Strings in Programs ............................................................................85 Using Special Characters in Strings ....................................................................85 Pasting Strings Together ......................................................................................86 Using Other Variables with Strings ......................................................................87 Advanced String Handling ..................................................................................88 Comparing Two Strings ..................................................................................89 Determining the Length of a String ................................................................89 Changing a String’s Case ................................................................................90 Looking for a String ........................................................................................90 Workshop: Presenting Credits ..............................................................................91 Summary ..............................................................................................................93 Q&A ......................................................................................................................93 Quiz ......................................................................................................................95 Questions ........................................................................................................95 Answers ..........................................................................................................96 Activities ..............................................................................................................96 HOUR 7 Using Conditional Tests to Make Decisions
97
Testing a Condition ..............................................................................................98 Statements ........................................................................................................98 Less Than and Greater Than Comparisons ....................................................99 Equal and Not Equal Comparisons ..............................................................100 Organizing a Program with Block Statements ..............................................100 if-else Statements ............................................................................................102 switch Statements ..............................................................................................103 The Conditional Operator ..................................................................................104 Workshop: Watching the Clock ..........................................................................105 Summary ............................................................................................................110 Q&A ....................................................................................................................110 Quiz ....................................................................................................................112 if
viii
Sams Teach Yourself Java 2 in 24 Hours
Questions ......................................................................................................112 Answers ........................................................................................................112 Activities ............................................................................................................112 HOUR 8 Repeating an Action with Loops
115
Loops ............................................................................................................116 Loops ........................................................................................................119 do-while Loops ..................................................................................................120 Exiting a Loop ....................................................................................................122 Naming a Loop ..................................................................................................122 Complex for Loops ........................................................................................123 Workshop: Teaching Your Computer a Lesson ..................................................124 Summary ............................................................................................................126 Q&A ....................................................................................................................127 Quiz ....................................................................................................................127 Questions ......................................................................................................128 Answers ........................................................................................................128 Activities ............................................................................................................128 for
while
PART III Working with Information in New Ways HOUR 9 Storing Information with Arrays
129 131
Creating Arrays ..................................................................................................132 Using Arrays ......................................................................................................134 Multidimensional Arrays ....................................................................................136 Sorting an Array ..................................................................................................136 Workshop: Array of Prizes, Indeed ....................................................................139 Summary ............................................................................................................143 Q&A ....................................................................................................................143 Quiz ....................................................................................................................144 Questions ......................................................................................................144 Answers ........................................................................................................145 Activities ............................................................................................................145 HOUR 10 Creating Your First Object
147
How Object-Oriented Programming Works ......................................................148 Objects in Action ................................................................................................149 What Objects Are ................................................................................................151 Understanding Inheritance ..................................................................................152 Building an Inheritance Hierarchy ....................................................................152 Converting Objects and Simple Variables ..........................................................153
Contents
ix
Casting Simple Variables ..............................................................................154 Casting Objects ..............................................................................................156 Converting Simple Variables to Objects and Back ......................................156 Workshop: Creating an Object ..........................................................................158 Summary ............................................................................................................161 Q&A ....................................................................................................................161 Quiz ....................................................................................................................161 Questions ......................................................................................................162 Answers ........................................................................................................162 Activities ............................................................................................................162 HOUR 11 Describing What Your Object Is Like
163
Creating Variables ..............................................................................................164 Creating Class Variables ....................................................................................166 Creating Behavior with Methods ........................................................................167 Declaring a Method ......................................................................................168 Similar Methods with Different Arguments ..................................................169 Constructor Methods ....................................................................................170 Class Methods ..............................................................................................171 Variable Scope Within Methods ....................................................................171 Putting One Class Inside Another ......................................................................173 Using the this Keyword ....................................................................................174 Workshop: Using Class Methods and Variables ................................................175 Summary ............................................................................................................177 Q&A ....................................................................................................................178 Quiz ....................................................................................................................179 Questions ......................................................................................................179 Answers ........................................................................................................179 Activities ............................................................................................................180 HOUR 12 Making the Most of Existing Objects
181
The Power of Inheritance ..................................................................................182 Inheriting Behavior and Attributes ................................................................183 Overriding Methods ......................................................................................184 Establishing Inheritance ......................................................................................184 Using this and super in a Subclass ............................................................185 Working with Existing Objects ..........................................................................186 Developing JavaBeans ..................................................................................187 Workshop: Creating a Subclass ..........................................................................190 Summary ............................................................................................................192 Q&A ....................................................................................................................193 Quiz ....................................................................................................................194
PART IV Programming a Graphical User Interface HOUR 13 Building a Simple User Interface
197 199
Swing and the Abstract Windowing Toolkit ......................................................200 Using Components ..............................................................................................200 Frames and Windows ....................................................................................201 Buttons ..........................................................................................................204 Labels and Text Fields ..................................................................................206 Check Boxes ..................................................................................................207 Combo Boxes ................................................................................................208 Text Areas ......................................................................................................209 Panels ............................................................................................................210 Workshop: Creating Your Own Component ......................................................210 Summary ............................................................................................................213 Q&A ....................................................................................................................214 Quiz ....................................................................................................................214 Questions ......................................................................................................214 Answers ........................................................................................................215 Activities ............................................................................................................215 HOUR 14 Laying Out a User Interface
217
Using Layout Managers ......................................................................................218 The GridLayout Manager ..............................................................................220 The BorderLayout Manager ..........................................................................221 Separating Components with Insets ..............................................................221 Workshop: Laying Out an Application ..............................................................223 Summary ............................................................................................................227 Q&A ....................................................................................................................228 Quiz ....................................................................................................................228 Questions ......................................................................................................228 Answers ........................................................................................................229 Activities ............................................................................................................229 HOUR 15 Responding to User Input
231
Getting Your Programs to Listen ........................................................................232 Setting Up Components to Be Heard ................................................................233 Handling User Events ........................................................................................233
Contents
xi
Check Box and Combo Box Events ..............................................................234 Keyboard Events ............................................................................................235 Enabling and Disabling Components ............................................................237 Workshop: A Little Lotto Madness ....................................................................238 Summary ............................................................................................................247 Q&A ....................................................................................................................247 Quiz ....................................................................................................................248 Questions ......................................................................................................248 Answers ........................................................................................................248 Activities ............................................................................................................249 HOUR 16 Building a Complex User Interface
PART V Creating Multimedia Programs HOUR 17 Creating Interactive Web Programs
269 271
Standard Applet Methods ..................................................................................272 Painting an Applet Window ..........................................................................273 Initializing an Applet ....................................................................................274 Starting and Stopping an Applet ..................................................................275 Destroying an Applet ....................................................................................275 Putting an Applet on a Web Page ......................................................................275 A Sample Applet ................................................................................................276 Drawing in an Applet Window ......................................................................277 Testing the SalutonApplet Program ..............................................................278 The Java Plug-in ................................................................................................280 The