Introduction 1
Part I Studying for the Programmer誷 Exam 7 Chapter 1 Programmer Exam Roadmap 8 What Is the Programmer Exam Like? 8 What誷 on the Exam 8 Declarations and Access Control 9 Flow Control and Exception Handling 9 Garbage Collection 9 Language Fundamentals 9 Operators and Assignments 9 Overloading, Overriding Runtime Type, and Object Orientation 10 Threads 10 The java.awt Package ?Layout 10 The java.lang Package 10 The java.util Package 10 The java.io Package 11 How Does the Text Map to the Programmer Exam Objectives? 11 How the Test Is Administered 11 How to Sign up for and Take the Exams 11 Test-Taking Tips for the Programmer Exam 12 What the Test Program Looks Like for the Programmer Exam 12 Be Careful! 13 Take Your Time and Answer the Easy Ones First 13 Beware of Tricks 14 Understand Why the Question Is on the Test 14 Know Your Test Center 15 What Happens When You誶e Done? 16 Sun誷 Take on Certification 16 How Sun Manages the Certification Process 16 Where Certification Is Heading 17 Chapter 2 Java Programming Fundamentals 18 Java Programming Concepts 18 Objects 18 Classes 19 Packages 19 Inheritance 19 Encapsulation 20 Access Controls 20 Java誷 Characteristics 21 Design for Distributed Networks 22 Design for Multithreading 22 Design for Ease of Use and Reuse 22 Design for Security 23 Working with Java Applets and Applications 23 The Java Specification and the API 23 Java 1.1 24 Java 2: JDK 1.2 24 Java 2: JDK 1.3 25 Review Questions 25 Answers to Review Questions 26 Chapter 3 Java Keywords 27 An Alphabetical List of All the Keywords 27 Organizing Classes 27 Defining Classes 28 Keywords for Classes and Members 28 Simple Data Types 30 Values and Variables 30 Exception Handling 31 Instance Creation and Testing 32 Control Flow 32 Exercises 34 Exercise 3-1 34 Exercise 3-2 34 Exercise 3-3 34 Answers to the Exercises 34 Review Questions 35 Answers to Review Questions 36 Chapter 4 Java Building Blocks 37 Identifiers 37 Literals 38 Boolean Literals 38 Character Literals 38 Exercise 4-1 39 String Literals 39 Exercise 4-2 40 Integer Literals 40 Floating-Point Literals 41 Separators 41 Operators 41 Exercise 4-3 42 Exercise 4-4 43 Comments 43 Answers to the Exercises 44 Review Questions 44 Answers to Review Questions 46 Chapter 5 Memory and Garbage Collection 47 Garbage Collection 47 When Does an Object Become Eligible for Garbage Collection? 48 Invoking the Garbage Collector 48 Finalization 49 Exercise 5-1 49 Exercise 5-2 49 Accessing Members 50 Exercise 5-3 51 Passing Parameters to a Method 51 Exercise 5-4 54 Forcing Garbage Collection 55 Answers to the Exercises 55 Review Questions 57 Answers to Review Questions 59 Chapter 6 Data Types and Values 60 Data Types 60 Variables and Data Types 61 Primitive Data Types 61 Integer Data Types 62 Character Values 63 Floating-Point Arithmetic 64 Booleans 65 Casting 65 Exercises 66 Exercise 6.1 66 Exercise 6.2 66 Exercise 6.3 66 Arrays 66 Declaring and Initializing Arrays 67 One-Dimensional Arrays 67 Initializing an Array When it is Allocated 68 Exercise 6.4 69 Arrays of Arrays 69 Where Arrays Fit into the Class Hierarchy 70 Answers to the Exercises 71 Review Questions 72 Answers to Review Questions 73 Chapter 7 Operators 74 Operators and Expressions 74 Arithmetic Operators 75 Assignment Operators 77 Comparison Operators 78 Logical Operators 79 Bitwise Operators 80 The>>Operator 81 The>>>Operator 82 The< Exercises 85 Exercise 7.1 85 Testing for an Object誷 Class Type 85 Exercise 7.2 86 equals() and == 86 Exercise 7.3 87 Exercise 7.4 87 Operator Precedence and Evaluation Order 87 Using Arrays in Expressions 89 Using chars in Expressions 89 Answers to the Exercises 89 Review Questions 92 Answers to Review Questions 94 Chapter 8 Control Flow 96 Using if and if . . . else Statements 96 if Statements 96 if . . . else Statements 97 Nested if Statements 97 Legal Values for if Statements 98 Exercise 8.1 98 Looping Expressions 99 while Statements 99 do-while Statements 100 Legal Values for while and do-while Statements 101 Iteration with for loops 101 for Statements 101 Controlling for Loops with Expressions 102 Nested for Loops 102 Switch and Case Statements 103 switch case 103 Nested switch case 104 Using Jump Statements 105 break 105 continue 105 Labeled Loops 106 Exercise 8.2 108 Using && and || 109 Exercise 8.3 109 Answers to the Exercises 109 Review Questions 110 Answers to Review Questions 111 Chapter 9 Methods 112 Defining Methods 112 Methods Definitions 112 Return Types 113 Method Modifiers 114 Exercise 9.1 116 Overloading a Method 117 Exercise 9.2 118 Overriding a Method 118 Access Control to Subclasses 119 Inheriting Synchronized and Abstract 119 Return Types 119 Parameter Types 120 Exceptions 120 Exercise 9.3 120 Object References to Base and Derived Classes 121 Answers to the Exercises 122 Review Questions 124 Answers to Review Questions 125 Chapter 10 Constructors 126 Constructor Essentials 126 The Default Constructor 127 Defining, Overloading, and Overriding a Constructor 128 Invoking Another Constructor 130 Instance Variables in Constructors 130 Exercise 10.1 132 Exercise 10.2 132 Exercise 10.3 133 Answers to the Exercises 133 Review Questions 134 Answers to Review Questions 136 Chapter 11 Objects and Classes 137 Objects 137 Creating and Destroying Objects 138 Classes 139 Declaring a Class 139 Application Class Structures 140 Applet Class Structures 140 Class Variables 143 Instance Variables 144 Static Variables 145 Predefined Instances 145 null 145 this 145 super 146 Object-Oriented Relationships Using 詉s a?and 詇as a?146 Exercises 148 Exercise 11.1 148 Exercise 11.2 148 Class Keywords 148 public 149 abst 149 strictfp 150 final 150 Exercise 11.3 151 Exercise 11.4 151 Variable Keywords 151 static 151 final 151 transient 152 volatile 152 Exercise 11.5 152 Access Control Keywords 152 public 152 private 153 protected 153 Exercise 11.6 153 Exercise 11.7 154 Exercise 11.8 154 Exercise 11.9 154 Exercise 11.10 154 Casting Classes 155 Inner Classes 156 Creating Inner Classes 156 Anonymous Classes 160 Static Inner Classes 161 Exercise 11.11 161 Answers to the Exercises 162 Review Questions 168 Answers to Review Questions 171 Chapter 12 Packages, Inheritance, and Interfaces 172 Packages 172 Declaring a Package 172 The CLASSPATH Environmental Variable 174 Import Statements 174 Package Naming Conventions 175 Using Packages 176 Exercise 12.1 177 Exercise 12.2 177 Exercise 12.3 177 Exercise 12.4 178 Creating a Package 178 Ordering of a Java Source File 180 Exercise 12.5 180 Exercise 12.6 181 Inheritance 182 Declaring Inheritance 183 Using Inheritance 184 Interfaces 185 Using Interfaces 185 Defining Interfaces 186 Using the Cloneable Interface 187 Exercise 12.7 188 Answers to the Exercises 188 Review Questions 190 Answers to Review Questions 191 Chapter 13 Passing Arguments to Programs 192 The main() Method 192 Command Line Arguments 193 Exercise 13.1 194 When main() Ends 195 Embedding an Applet in an HTML Page 195 Passing Parameters to an Applet 196 Retrieving Parameters 197 Exercise 13.2 198 Customizing an Applet 198 Answers to the Exercises 201 Review Questions 203 Answers to Review Questions 204 Chapter 14 Exceptions 205 Exception Basics 205 Working with Exceptions 206 Uncaught Exceptions 207 Checked and Unchecked Exceptions 207 Using try, catch, and finally 208 Exercise 14.1 209 Common Java Exceptions 210 Using Methods Defined by Exception and Throwable 211 Rethrowing an Exception 211 Exercise 14.2 212 Which Exceptions a Method Can Throw 213 Exceptions in an Overriding Method in a Subclass 213 Exercise 14.3 214 Creating and Throwing an Exception 214 Exercise 14.4 215 Answers to the Exercises 215 Review Questions 218 Answers to Review Questions 223 Chapter 15 Multithreading 224 An Overview of Threads 224 Thread States 224 Thread Life Cycles 225 Thread Scheduling and Priorities 226 Creating Threads With java.lang.Thread and java.lang.Runnable 227 Subclassing Thread 227 Implementing Runnable 229 Exercise 15.1 230 Monitors and Synchronization 232 Understanding Synchronization 232 Exercise 15.2 234 Using wait(), notify(), and notifyAll() 234 Exercise 15.3 238 Managing Threads With Wait-Notify and Sleep 238 Why a Thread Might Not Execute 241 Exercise 15.4 242 Other Useful Thread Methods 242 Answers to the Exercises 242 Review Questions 247 Answers to Review Questions 248 Chapter 16 Essential java.lang and java.util 249 Wrapper Classes 249 The Boolean Class 249 The Character Class 250 The Byte, Short, Integer, and Long Classes 250 The Float and Double Classes 251 Using Wrapper Classes 251 Exercise 16.1 252 Working with the Math Class 252 abs() 252 ceil() 253 floor() 253 max() 254 min() 254 random() 254 round() 254 sqrt() 255 sin() 255 cos() 255 tan() 255 Exercise 16.2 255 The String and StringBuffer Classes 255 String Operators 255 The Immutability of Strings 256 Methods of the String Class 257 Exercise 16.3 258 String Buffers 258 Exercise 16.4 259 Collections 260 Overview of the Collections API 260 Collection API Essentials 262 Answers to the Exercises 263 Review Questions 264 Answers to Review Questions 267 Chapter 17 java.io 268 The java.io Package 268 InputStream and OutputStream 269 FilterInputStream and FilterOutputStream 269 DataInput and DataOutput 271 Reader and Writer 272 Working with Files 273 Testing to See if a File Exists 273 Finding Information about a File Node 274 Deleting and Renaming Files 274 Creating Directories 274 Navigating the File System 274 Creating Files 274 Exercise 17.1 275 Creating Temporary Files 275 Streams 276 Sequencing Stream Filters 276 Exercise 17.2 278 Reading and Writing Data Streams 278 Encoded Text Streams 279 Writing To and Reading From Files 281 Exercise 17.3 282 Random Access Files 282 File Descriptors 284 Exercise 17.4 285 Using Buffered Readers to Read Text Input 285 Using Print Writers to Write Text Output 286 Answers to the Exercises 288 Review Questions 290 Answers to Review Questions 291 Chapter 18 java.awt: Graphics and Components 293 The Abstract Windowing Toolkit (AWT) 293 AWT Essentials 295 Applets and AWT 296 The paint() Method 297 Repainting 297 paint() and the Graphics Class 298 Smoother Graphics 298 Drawing Using a Graphics Object 299 drawString() 300 drawLine() 300 drawRect(), fillRect() 301 drawPolygon(), fillPolygon() 302 drawArc(), fillArc() 302 drawImage() 304 Exercise 18.1 304 The Image Class 304 Creating a Blank Image 304 Loading or Transferring an Image 305 Using an ImageObserver 306 Exercise 18.2 308 Working with Frame Windows 308 Constructing the Frame 308 Sizing the Frame 308 Setting the Frame Title 309 Closing the Frame Window 309 Creating a Frame Window in An Application 310 Creating a Frame Window in An Applet 311 How Java Arranges Components Within Containers 313 The Component Class, Subclasses, and Methods 313 TextArea 313 TextField 315 List 315 Exercise 18.3 316 Key Component Methods 317 Answers to the Exercises 319 Review Questions 321 Answers to Review Questions 322 Chapter 19 java.awt: Layout 323 Components and Layout Managers 323 Layout Manager Essentials 323 Using Layout Managers 324 FlowLayout 324 BorderLayout 326 GridLayout 330 Other Types of Layout Managers 332 Default Layout Managers 332 Exercise 19.1 332 Answers to the Exercises 333 Review Questions 334 Answers to Review Questions 335 Chapter 20 java.awt: Event Handling 336 Event Classes 336 Listening for Events 337 The Listener Interfaces 339 ActionListener 339 Interface Method 339 AdjustmentListener 340 ComponentListener 340 ContainerListener 340 FocusListener 341 ItemListener 341 KeyListener 341 MouseListener 341 MouseMotionListener 342 TextListener 342 WindowListener 342 Implementing a Listener Interface 343 Exercise 20.1 344 Semantic and Low-Level Events 344 ActionEvent 344 AdjustmentEvent 344 ItemEvent 345 AWTEvent Subclasses 345 AWTEvent 345 ActionEvent 345 AdjustmentEvent 346 ComponentEvent 346 ContainerEvent 346 FocusEvent 346 InputEvent 346 ItemEvent 346 KeyEvent 346 MouseEvent 347 PaintEvent 347 TextEvent 347 WindowEvent 347 Exercise 20.2 347 Extending Adapter Classes 347 Exercise 20.3 348 Answers to the Exercises 348 Review Questions 351 Answers to Review Questions 352 Chapter 21 Sun Certified Programmer Practice Exam 1 353 Practice Exam Number 1 353 Questions 353 Answers to Exam Questions 374 Chapter 22 Sun Certified Programmer Practice Exam 2 377 Practice Exam Number 2 377 Questions 377 Answers to Exam Questions 393 Chapter 23 Sun Certified Programmer Practice Exam 3 396 Practice Exam Number 3 396 Questions 396 Answers to Exam Questions 416 Part II Studying for The Developer誷 Exam 419 Chapter 24 Developer Exam Roadmap 420 A Quick Overview 420 Concepts You Should Know Before Getting Started 421 The Programming Assignment 421 The Essay Exam 423 How to Sign Up For and Take the Exams 424 How to Acquire the Programming 424 Strategies for Approaching the Developer Assignment 425 Chapter 25 Developing with Java 426 Styles and Conventions 427 Identifiers 427 Comments 427 Error Handling 427 Java誷 Documentation Problem 428 javadoc 429 Helping javadoc 429 javadoc Tags 432 Variables 432 Classes 434 Methods 434 Rules for Using javadoc Comments and Tags 435 Examples of javadoc Comments and Tags 435 Abstract Classes 438 Interfaces 438 Grouping Behavior 438 Enforcing Your API 438 Identifying Intent 439 Exercise 25.1 439 Packages 439 Creating Packages 439 Restricting Access 440 Defining Subsystems 440 Exercise 25.2 441 Defining Access Methods 441 Exercise 25.3 443 Answers to the Exercises 444 Review Questions 446 Answers to Review Questions 447 Chapter 26 Accessing and Managing Databases 448 Roll-Your-Own Databases 449 Accessing Databases from Multiple Clients 451 Two-Tiered and Three-Tiered Architectures 452 Using Java APIs 453 Remote Method Invocation 453 Defining a Remote Interface 454 Defining a Remote Class 454 Defining the Class, Interface, and Constructor 455 Installing the Security Manager and Registering the Bootstrap Naming Service 458 Invoking Remote Objects 459 Stubs and Skeletons 461 Getting Things Going 461 Exercise 26.1 462 JDBC 462 The Need for Drivers 462 Using ODBC 463 Opening a Connection to a Database 464 Interacting with a Database 464 An Example of Using ODBC with Microsoft Access 465 Exercise 26.2 466 Answers to the Exercises 467 Review Questions 468 Answers to Review Questions 469 Chapter 27 Network Programming and Communication 470 A TCP/IP Primer 470 Ports and Sockets 472 Streams 473 Designing Servers and Clients 473 Handling Multiple Clients 474 Client Issues 475 The Networking Package 476 InetAddress 476 Socket and ServerSocket 476 Internet Streams 478 Client-Server Examples 479 Writing a Single-Client Server in Java 479 server() 487 client() 488 action() 488 run() for SingleChatReceive 488 Cleaning Up 489 Writing a Multi-Client Server in Java 489 server() 494 run() 494 broadcast() 495 cleanUp() 495 The Client 495 Cleaning Up 499 Exercise 27.1 499 Exercise 27.2 500 Uniform Resource Locator 500 Answers to the Exercises 501 Review Questions 511 Answers to Review Questions 512 Chapter 28 Designing Application Interfaces 513 Mix and Match! 513 Using One Layout Manager 514 Exercise 28.1 520 Changing Your Look 520 Text 520 Font and Color 521 Replacing a Component 521 Exercise 28.2 521 Working with Frames 522 Closing the Frame 522 Making an Applet a Standalone Application 523 Exercise 28.3 523 Answers to the Exercises 523 Review Questions 527 Answers to Review Questions 528 Chapter 29 Sun Certified Developer Practice Programming Assignment 529 The Scenario 529 The Prototype Specifications 530 The Design Completed So Far 530 The New Design 532 The Code Completed So Far 533 What You Will Add to the Code 537 The Database 538 The Server 538 The Client 538 The User Interface 538 The Protocol 538 Making a New Reservation 539 Delete a Passenger誷 Reservation 539 Finding a Passenger誷 Seat 539 Finding All Open Seats 539 Retrieving the List of Passengers 539 What the User Interface Should Look Like when You誶e Done 539 Updating the Passenger List 541 Final Words of Advice 541 One Possible Answer for the Client Side 542 Client.java 542 TopFrame.java 545 EdnaUI.java 545 NewPassenger.java 548 DeletePassenger.java 549 One Possible Answer for the Server Side 551 Server.java (Including ClientServerThread) 551 The Three Missing Methods from DB.java 558 Chapter 30 Sun Certified Developer Practice Essay Exam 560 Questions 560 Answers 561 Part III Appendices 563 Appendix A What誷 on the CD-ROM 564 How the CD is Organized 564 Source Code 564 Sun誷 JDK 564 Where to Find More 564 What Is the Test Engine? 565 Appendix B The JDK 566 The Pieces of the JDK 566 The Compiler 566 The Runtime Environment for Stand-Alone Programs 567 The Documentation Tool 567 The Applet Viewer 568 |
商品评论(0条)