java certification - gbv
TRANSCRIPT
Java Certification for Programmers
and Developers
Barry Boone
McGraw-Hill New York • San Francisco • Washington, D.C, • Auckland
Bogota • Caracas • Lisbon • London • Madrid
Mexico City • Milan • Montreal • New Delhi San Juan • Singapore • Sydney
Tokyo * Toronto
Contents Introduction: What this Book is All About l
What Does It Mean to Become Sun Certified in Java? 1 What are the Exams Like? 2 Why Use Java Certification for Programmers and
Developers to Prepare for the Exams? 3 Where Else Can You Go to Prepare for the Test? 4 What Should You Know Before You Start Studying? 4
Exercise 0.1 5 What is My Background? 6 How is this Book Organized? 6 What Do You Do Once You're Ready to Take the Test? 8 Onwardl 9 Answers to the exercises 9
Part I Study for Programmers n Chapter 1: Object-Oriented Programming 13
What's on the Test 14 Objectives for this Chapter 14 Object-oriented Relationships Using 'is a' and 'has a' 14
Exercise 1.1 17 Exercise 1.2 17
Review: Classes, Variables, and Methods 17 Classes 18 Member Variables 18 Member Methods 19
Packages 20 Access Control 20 Reuse Names 22 Sharing Classes Between Applications 23
Package Details 23 Creating a Package 23 Exercise 1.3 26 Exercise 1.4 26
Class Keywords 26 public 27 abstract 27 final 29 Exercise 1.5 30 Exercise 1.6 30
Ordering of a Java Source File 31 Exercise 1.7 31
V
V I Contents
Variable and Method Keywords 32 static 32 abstract 32 final 32 synchronized 33 native 33 Exercise 1,8 33
Access Control Keywords 34 public 34 private 34 protected 34
Exercises 35 Exercise 1.9 35 Exercise 1.10 36 Exercise 1.11 36 Exercise 1.12 37 Exercise 1.13 37 Exercise 1.14 38 Exercise 1.15 38 Exercise 1.16 39
Casting Classes 40 What's Not on the Test 42
Interfaces 42 How to Clone Objects 44 Exercise 1.17 45 Exercise 1.18 45 Static Initializers 46 Exercise 1.19 46
Answers to the Exercises 47 Review questions 55 Answers to the review questions 59
Chapter 2: Keywords 61
What's on the Test 62 Objectives for this Chapter 62 An Alphabetical List of All the Keywords 62 Organizing Classes 63
Defining Classes 63 Keywords for Classes and Members 64 Simple Data Types 67 Values and Variables 67 Exception Handling 68 Instance Creation and Testing 70 Control Flow 70 Not Used Yet. But Reserved 72 Exercises 73
Exercise 2.1 73 Exercise 2.2 73
VII
Exercise 2.3 73 Answers to the Exercises 74 Review Questions 75 Answers to Review Questions 76
Chapter 3: Constructors . 77
Whats on the Test 78 Objectives for this Chapter 78
The Default Constructor 78 Defining a Constructor 79 Invoking Another Constructor 82
Exercise 3.1 83 Exercise 3.2 83 Exercise 3.3 84
Answers to the Exercises 84 Review Questions 86 Answers to the Review Questions 88
Chapter 4: Memory and Garbage Collection 91
What's on the Test 92 Objectives for this Chapter 93 Garbage Collection 93 Finalization 95
Exercise 4.1 96 Exercise 4.2 97 Accessing Members , 97 Exercise 4.3 99
Passing Parameters to a Method 99 Exercise 4.4 103
What's Not on the Test 104 Forcing Garbage Collection 104
Answers to the Exercises 104 Review Questions 106 Answers to the Review Questions 109
Chapter 5: Data Types and Values ill
What's on the Test 112 Objectives for this Chapter I 12 Data Types 1 12 Integer Values 113 Character Values 1 14 Default Values 1 15
Literals 1 15 Exercises 1 16
Exercise 5.1 116 Exercise 5.2 116 Exercise 5.3 116
VIII Contents
Arrays 1 17 Initializing an Array when it is Allocated 1 18
Exercise 5.4 120 What's Not on the Test 120
Arrays of Arrays 120 Where Arrays Fit into the Class Hierarchy 122 Floating-Point Arithmetic . 123 Casting 123 Special Escape Sequences 124
Answers to the Exercises 125 Review Questions 127 Answers to the review questions 128
Chapter 6: Operators 129
What's on the Test 130 Objectives for this Chapter 130 The Bit Operators 130
The > Operator 130 The »operator 132 The < Operator 133 The & and I Operators 134 Exercise 6.1 135
Testing for an Object's Class Type 135 Exercise 6.2 137
Equals)) and == 137 Exercises 138
Exercise 6.3 138 Exercise 6.4 139
What's Not on the Test 139 Answers to the Exercises 141 Review Questions 144 Answers to the Review Questions 147
Chapter 7: Control Flow 149
What's on the Test 150 Objectives for this Chapter 150 Nested Loops 150 Labels 152
Exercise 7.1 153 Nested If and Else Statements 153 Legal Values for If Statements 155
Exercise 7.2 156 Switch and Case Statements 156 &&andll 158
Exercise 7.3 159 What's Not on the Test 160
While and Do-While Statements 160 Answers to the Exercises 160
IX
Review Questions 161 Answers to the Review Questions 163
Chapter 8: Exceptions 165
Whats on the Test 166 Objectives for this Chapter 166 Exception Basics . 166
Exercise 8.1 171 Rethrowing an Exception 172
Exercise 8.2 173 Which Exceptions a Method can Throw 173 Exceptions in an Overriding Method in a Subclass 174
Exercise 8.3 176 Creating and Throwing an Exception 176
Exercise 8.4 177 What's Not on the Test 17 7
Java's Exceptions 177 Using Methods Defined by Exception and Throwable 179
Answers to the Exercises 179 Review Questions 183 Answers to the Review Questions 189
Chapter 9: Methods 191
What's on the Test 192 Objectives for this Chapter 192 Defining a Method 192 Overloading a Method 193
Exercise 9.1 195 Overriding a Method 195
Access Control 196 Other Keywords 197 Return Types 197 Parameter Types 198 Exceptions 198 Exercise 9.2 198
Object References to Base and Derived Classes 199 Declaring Native Methods 202 Answers to the Exercises 202 Review Questions 204 Answer to the Review Questions 206
Chapter 10: The Math and String Classes 207
Whats on the Test 208 Objectives for this Chapter 208 Math Methods 208
abs() 208 ceil|) 209 floorf) 209
Contents
maxfl 210 min() 210 random)) 21 1 round () 211 sqrtf] 212 sin() 212 cos() 212 tan() 213 Exercise 10.1 213
String Methods 213 length)) 213 toUpperCasef) 214 toLowerCasef) 214 equals)) 214 equalslgnoreCasef) 214 charAtf) 215 indexOff), lastlndexOff) 215 substring)) 216 toStringO 216 trim)) 216
String Operators 217 String Objects are Read-Only 218
Exercise 10.2 218 Whats Not on the Test 218
Other Math Methods and Constants 218 Other String Methods 219 StringBuffer 219
Answers to the Exercises 220 Review Questions 221 Answers to the Review Questions 224
Input/Output 225
What's on the Test 226 Objectives for this Chapter 226 The Java.io Package 226 InputStream and OutputStream 227 FilterlnputStream and FilterOutputStream 228 Datalnput and DataOuput 230 Direct Subclasses InputStream and OutputStream 231
InputStream subclasses: 231 OutputStream subclasses: 231
Direct Subclasses of FilterlnputStream and FilterOutputStream 232 FilterlnputStream subclasses: 232 FilterOutputStream subclasses: 232
Classes that Implement Datalnput and DataOutput 232 The File Class 233
Testing to See if a File Exists 233 Finding Information about a File Node 234
XI
Deleting and Renaming Files 234 Creating Directories 234 Navigating the File System 234 Creating Files 235 Exercise 11.1 236
Writing To and Reading From Files 236 Exercise 1 1.2 238
RandomAccessFile 238 Exercise 1 1.3 240
What's Not on the Test 241 SequencelnputStream 242 StringBufferlnputStream 242 ByteArraylnputStream and ByteArrayOutputStream 242 PipedlnputStream and PipedOutputStream 242 LineNumberlnputStream 242 BufferedlnputStream 242 PushbacklnputStream 243 PrintStream 243 BufferedOutputStream 243 FileDescriptor 243
Answers to the Exercises 244 Review Questions 245 Answers to the Review Questions 247
Chapter 12: Threads 249
What's on the Test 250 Objectives for this Chapter 250 An Overview 251 Telling a Thread What to Do 251
Subclassing Thread 252 Implementing Runnable 254 Exercise 12.1 256
Thread Life Cycles 258 Scheduling 259 Monitors and Synchronization 260
Exercise 12.2 263 Programmer Control 264 Using wait(), notify)), and notifyAllf) 265
Exercise 12.3 270 Why a Thread Might Not Execute 270
Exercise 12.4 271 What's Not on the Test 271
Other Ways to Pause and Restart a Thread 271 Thread and Applet Life Cycles 271 Other Thread Methods 273
Answers to the Exercises 274 Review Questions 279 Answers to the Review Questions 281
XI I Contents
Chapter 13: Graphical User Interfaces 283
What's on the Test 284 Objectives for this Chapter 285 The Abstract Windowing Toolkit (AWT) 286 A Quick Review of Applets 287 The paintf) Method 288 Repainting 289 paintf) and the Graphics Class 291 Drawing using a Graphics Object 292
drawString)) 293 drawünel) 294 drawRect(), fillRectf) 294 drawPolygon(), fillPolygonf) 296 drawArcf), fillArc[) 296 drawlmagel) 298 Exercise 13.1 298
The Image Class 298 Java Arranges Components within Containers 299 The Component Class and Three Subclasses 300
TextArea 301 TextField 303 List 304 Exercise 13.2 305
Some Component Methods for Java 1.0.2 305 enable/disable 306 size/resize 307 show/hide 307 setForeground/setBackground 308
Some Component Methods for Java 1.1 308 Color 308 Layout Managers 309
Using Layout Managers 311 FlowLayout 31 1 BorderLayout 314 GridLayout 318
Default Layout Managers 320 Exercise 13.3 321
Whats Not on the Test 322 Smoother Graphics 322 Other Types of Layout Managers 323
Answers to the Exercises 324 Review Questions 325 Answers to the Review Questions 328
Chapter 14: Events in Java 1.0.2 329
What's on the Test 330 Objectives for this Chapter 330 Java Tells You About Events 330
Events Methods 330 The handleEvent() Method 331 The action)) Object 332 The Event Class 333
Event Propagation 334 Exercise 14.1 335 Exercise 14.2 335 Exercise 14.3 335 Event Handling Strategies 336 Miscellaneous Component Subclasses 338
Answers to the Exercises 341 Review Questions 343 Answers to the Review Questions 344
Chapter 15: Events in Java 1.1 347
Whats on the Test 348 Objectives for this Chapter 348 A Quick Reminder of Events in 1.0.2 348 Design Goals for Events in Java 1.1 349 An Overview 350 The New Event classes 350 Listening for Events 352 The Listener Interfaces 355
ActionListener 355 AdjustmentListener 356 ComponentListener 356 FocusListener 356 ItemListener 356 KeyListener 356 MouseListener 356 MouseMotionListener 357 WindowListener 357
Implementing a Listener Interface 357 Exercise 15.1 358
Semantic and Low-Level Events 359 ActionEvent 359 ItemEvent 360 AdjustmentEvent 360
AWTEvent Subclasses 360 AwTEvent 361 ActionEvent 361 AdjustmentEvent 361 ComponentEvent 362 FocusEvent 362 InputEvent 362 KeyEvent 362 MouseEvent 362 ItemEvent 362
XIV Contents
PaintEvent 363 WindowEvent 363 Exercise 15.2 363
Extending Adapter Classes 363 Exercise 15.3 364
Answers to the Exercises 365 Review Questions 368 Answers to the Review Questions 370
Chapter 16: Passing Arguments to Programs 371
What's on the Test 372 Objectives for this Chapter 372 The main() Method 372 Command Line Arguments 373
Exercise 16.1 375 When mainf) Ends 375 What's Not on the Test 376 Answers to the Exercises 376
Exercise 16.1 3/6 Review Questions 377 Answers to the Review Questions 378
Chapter 17: Embedding Applets into Web Pages 379
What's on the Test 380 Objectives 380 The Simplest HTML File 380 Passing Parameters to an Applet 381 Retrieving Parameters 382 Exercises 384
Exercise 17.1 384 Exercise 17.2 384
Customizing an Applet 385 What's Not on the Test 387
getParameterlnfo() 387 Advanced Keywords 388
Answers to the Exercises 389 Review Questions 391 Answers to the Review Questions 391
Chapter 18: Java 1.1 393
What's on the Test 394 Objectives for this Chapter 394 Inner Classes 394
Anonymous Classes 399 Some Rules for Inner Classes 399 Exercise 18.1 400
JAR Files 401
XV
Creating a JAR File 401 Specifying a JAR File in a Web Page 402
Serializing Objects 403 Exercise 18.2 406
Keywords 406 Reflection 406 Beans 407
Properties 408 Events 409 Customizer, PropertyEditor, and Beanlnfo 410 Persistence 410 Displaying a Bean 41 1 Distributing your Bean 41 1 Exercise 18.3 411
Answers to the Exercises 412 Review Questions 417 Answers to the Review Questions 420
Part II Developer Certification Study Guide #21 Chapter 19: Creating Clean APIs 423
Whats on the Programming Assignment 424 Objectives for this Chapter 424 Abstract Classes 424 Interfaces 425
Grouping Behavior 426 Enforcing Your API 426 Identifying Intent 426 Exercise 19.1 427
Packages 427 Creating Packages 427 Restricting Access 428 Defining Subsystems 429 Exercise 19.2 430
Defining Access Methods 430 Exercise 19.3 433
Answers to the Exercises 434 Review Questions 436 Answers to the Review Questions 437
Chapter 20: Advanced Graphical User Interfaces 439
What's on the Programming Assignment 440 Objectives for this Chapter 440 Mix and Match! 440
Using One Layout Manager 441 Exercise 20.1 447
XVI Contents
Changing Your Look 448 Text 448 Font and Color 448 Replacing a Component 449 Exercise 20.2 450
Working with Frames 450 Closing the Frame 451 Making an Applet a Standalone Application 453 Exercise 20.3 ' 453
Answers to the Exercises 454 Review Questions 458 Answers to the Review Questions 459
Chapter 21: Accessing Databases 461
What's on the Programming Assignment 462 Objectives for this Chapter 462 Roll-Your-Own Databases 463 Accessing Databases from Multiple Clients 466 Two-Tiered and Three-Tiered Architectures 467 Using Java 1.1 APIs 468 Remote Method Invocation 468 Defining a Remote Interface 470 Defining a Remote Class 471 Invoking Remote Objects 475 Stubs and Skeletons 478 Getting Things Going 478
Exercise 21.1 479 JDBC 479 The Need for Drivers 480 Using ODBC 481 Opening a Connection to a Database 482 Interacting with a Database 482 An Example of Using ODBC with Microsoft Access on Windows 95 483
Exercise 21.2 485 Answers to the Exercises 486 Review Questions 487 Answers to the Review Questions 489
Chapter 22: Network Programming 491
What's on the Programming Assignment 492 Objectives for this Chapter 492 A TCP/IP Primer 492 Ports and Sockets 495 Streams 497 Designing Servers and Clients 498 Handling Multiple Clients 498
XVII
Client Issues 500
The Networking Package 501
InetAddress 502
Socket and ServerSocket 502
Internet Streams 505
Client-Server Examples 506
Writing a Single-Client Server in Java 507
serverf) 5 1 5
clientfj 5 1 6
action)) • 517
run() for SingleChatReceive 517
Cleaning Up 517
Writing a Multi-Client Server in Java 518
server)) 524
run() 525
broadcast!) 525
cleanup)) 525
The Client 525
Cleaning Up 531
Exercise 22.1 531
Exercise 22.2 531
What's Not on the Developer Assignment 532
Uniform Resource Locator 532
Answer to the Exercises 532
Review Questions 544
Answers to the Review Questions 546
Chapter 23: Comments and Style 547
What's on the Programming Assignment 548
Objectives for this Chapter 548
Styles and Conventions 548
Indentation 548
Identifiers 549
Comments 549
Error Handling 550
Java's Documentation Problem 551
javadoc 552
Helping javadoc 553
javadoc Tags 557
Variables 557
Classes 557
Methods 558
Rules for Using javadoc Comments and Tags 559
Examples of javadoc Comments and Tags 560
Review Questions 562
Answers to the Review Questions 562
XVI I I Contents
Part III Practice Exams and What to Expect s&3 Chapter 24: The Scoop on Java Certification 565
A Quick Overview 566 What the Programmer Test is Like 567 What the Developer Assignment and Test are Like 567 How the Test is Administered 567 How to Sign Up For and Take the Exams 568 How to Acquire the Programming Assignment for the Developer Test 569 Strategies for Approaching the Developer Assignment 570 How and When to Sign Up for the Developer Exam 571 Whom to Contact 571 Test-Taking Tips for the Programmer Exam 572
What the Test Program Looks Like for the Programmer Exam 572 Be Careful! 573 Take Your Time and Answer the Easy Ones First 573 Beware of Tricks 574 Understand Why the Question is on the Test 575 Know Your Test Center 576 What Happens When You're Done? 577
Passing the Developer Assignment and Exam 577 Sun's Take on Certification 578 How Sun Manages the Certification Process 579 Where Certification is Heading 579
Chapter 25: Sun Certified Programmer Exam 581
Practice Exam Number 1 581 Questions 582 Answers and Explanations 617
Chapter 26: Sun Certified Programmer Exam 625
Practice Exam Number 2 625 Questions 626 Answers and Explanations 660
Chapter 27: Practice Programming Assignment 669
The Scenario 670 The Prototype Specifications 671 The Design Completed So Far 671 The New Design 673 The Code Completed So Far 674 What You will Add to the Code 681
The Database 681 The Server 681 The Client 682 The User Interface 682
The Protocol 682
xix
Making a New Reservation Delete a Passenger's Reservation Finding a Passenger's Seat Finding All Open Seats Retrieving the List of Passengers
What the User Interface Should Look Like when You're Done Updating the Passenger List
Final Words of Advice One Possible Answer for the Client Side
Client.java TopFramejava EdnaUl.java NewPassenger.java DeletePassengerjava
One Possible Answer for the Server Side Serverjava (Including ClientServerThread) The Three Missing Methods from DB Java
682
682
683
683
683
683
685
686
687
687
690
691
693
695
697
697
704
Chapter 28:
Appendix A:
Appendix B:
Appendix C:
Practice Developer Exam
Questions Answers
The Web-Based Course
The CD ROM
How the CD is Organized Source Code Suns JDK Web links
The JDK
The pieces of the JDK The Compiler The Runtime Environment for Stand-Alone Programs The Applet Viewer
707
708
710
713
717
717
718
718
718
719
719
720
721
722
Index 723