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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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
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
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
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
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
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
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
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