ABSOLUTE JAVA™ 6th Edition Global Edition
معرفی کتاب «ABSOLUTE JAVA™ 6th Edition Global Edition» نوشتهٔ Savitch, Walter J;Mock, Kenrick، منتشرشده توسط نشر Pearson; Pearson Higher Education در سال 2015. این کتاب در فرمت pdf، زبان انگلیسی ارائه شده است. «ABSOLUTE JAVA™ 6th Edition Global Edition» در دستهٔ بدون دستهبندی قرار دارد.
For courses in computer programming and engineering. Beginner to Intermediate Programming in Java This book is designed to serve as a textbook and reference for programming in the Java language. Although it does include programming techniques, it is organized around the features of the Java language rather than any particular curriculum of programming techniques. The main audience is undergraduate students who have not had extensive programming experience with the Java language. The introductory chapters are written at a level that is accessible to beginners, while the boxed sections of those chapters serve to quickly introduce more experienced programmers to basic Java syntax. Later chapters are still designed to be accessible, but are written at a level suitable for students who have progressed to these more advanced topics. MyProgrammingLab(TM) not included. Students, if MyProgrammingLab is a recommended/mandatory component of the course, please ask your instructor for the correct ISBN and course ID. MyProgrammingLab should only be purchased when required by an instructor. Instructors, contact your Pearson representative for more information. MyProgrammingLab is an online homework, tutorial, and assessment product designed to personalize learning and improve results. With a wide range of interactive, engaging, and assignable activities, students are encouraged to actively learn and retain tough course concepts. Cover......Page 1 Title Page......Page 4 Copyright Page......Page 5 Preface......Page 6 Acknowledgments......Page 8 Brief Contents......Page 14 Contents......Page 16 Chapter 1 Getting Started......Page 34 Origins of the Java Language......Page 35 Objects and Methods......Page 36 Applets......Page 37 A Sample Java Application Program......Page 38 Byte-Code and the Java Virtual Machine......Page 41 Compiling a Java Program or Class......Page 43 Running a Java Program......Page 44 TIP: Error Messages......Page 45 Identifiers......Page 46 Variables......Page 48 Assignment Statements......Page 49 TIP: Initialize Variables......Page 51 More Assignment Statements......Page 52 Assignment Compatibility......Page 53 Constants......Page 54 Arithmetic Operators and Expressions......Page 56 Parentheses and Precedence Rules......Page 57 Integer and Floating-Point Division......Page 59 PITFALL: Round-Off Errors in Floating-Point Numbers......Page 60 PITFALL: Division with Whole Numbers......Page 61 Type Casting......Page 62 Increment and Decrement Operators......Page 63 String Constants and Variables......Page 66 Concatenation of Strings......Page 67 Classes......Page 68 String Methods......Page 70 Escape Sequences......Page 75 The Unicode Character Set......Page 76 Naming Constants......Page 79 Java Spelling Conventions......Page 81 Comments......Page 82 Indenting......Page 83 Chapter Summary......Page 84 Answers to Self-Test Exercises......Page 85 Programming Projects......Page 87 Chapter 2 Console Input and Output......Page 90 System.out.println......Page 91 Formatting Output with printf......Page 94 TIP: Formatting Monetary Amounts with printf......Page 98 TIP: Legacy Code......Page 99 Money Formats Using NumberFormat......Page 100 Importing Packages and Classes......Page 103 The DecimalFormat Class......Page 105 The Scanner Class......Page 109 PITFALL: Dealing with the Line Terminator, '\n'......Page 116 TIP: Echo Input......Page 117 Example: Self-Service Checkout......Page 119 Other Input Delimiters......Page 120 The Scanner Class for Text File Input......Page 122 Answers to Self-Test Exercises......Page 125 Programming Projects......Page 128 Chapter 3 Flow of Control......Page 132 if-else Statements......Page 133 Omitting the else......Page 134 Compound Statements......Page 135 TIP: Placing of Braces......Page 136 Multiway if-else Statement......Page 137 Example: State Income Tax......Page 138 The switch Statement......Page 140 PITFALL: Forgetting a break in a switch Statement......Page 144 The Conditional Operator......Page 145 Simple Boolean Expressions......Page 146 PITFALL: Using = in Place of ==......Page 147 PITFALL: Using == with Strings......Page 148 Lexicographic and Alphabetic Order......Page 149 Building Boolean Expressions......Page 152 Evaluating Boolean Expressions......Page 153 TIP: Naming Boolean Variables......Page 156 Short-Circuit and Complete Evaluation......Page 157 Precedence and Associativity Rules......Page 158 while Statement and do-while Statement......Page 165 Algorithms and Pseudocode......Page 167 Example: Averaging a List of Scores......Page 170 The for Statement......Page 171 The Comma in for Statements......Page 174 PITFALL: Extra Semicolon in a for Statement......Page 176 PITFALL: Infinite Loops......Page 177 Nested Loops......Page 178 The break and continue Statements......Page 181 The exit Statement......Page 182 Tracing Variables......Page 183 General Debugging Techniques......Page 184 Example: Debugging an Input Validation Loop......Page 185 Preventive Coding......Page 189 Assertion Checks......Page 190 The Random Object......Page 192 The Math.random() Method......Page 194 Answers to Self-Test Exercises......Page 195 Programming Projects......Page 201 Chapter 4 Defining Classes I......Page 206 4.1 CLASS DEFINITIONS......Page 207 Instance Variables and Methods......Page 210 More about Methods......Page 213 TIP: Any Method Can Be Used as a void Method......Page 217 Local Variables......Page 219 Blocks......Page 220 Parameters of a Primitive Type......Page 221 PITFALL: Use of the Terms “Parameter” and “Argument”......Page 228 The this Parameter......Page 230 Methods That Return a Boolean Value......Page 232 The Methods equals and toString......Page 235 TIP: Testing Methods......Page 238 4.2 INFORMATION HIDING AND ENCAPSULATION......Page 240 public and private Modifiers......Page 241 Example: Yet Another Date Class......Page 242 Accessor and Mutator Methods......Page 243 TIP: A Class Has Access to Private Members of All Objects of the Class......Page 248 TIP: Mutator Methods Can Return a Boolean Value......Page 249 Preconditions and Postconditions......Page 250 Rules for Overloading......Page 251 PITFALL: Overloading and Automatic Type Conversion......Page 255 PITFALL: You Cannot Overload Based on the Type Returned......Page 257 Constructor Definitions......Page 259 TIP: A Constructor Has a this Parameter......Page 267 TIP: Include a No-Argument Constructor......Page 268 Example: The Final Date Class......Page 269 An Alternative Way to Initialize Instance Variables......Page 270 Example: A Pet Record Class......Page 271 The StringTokenizer Class......Page 275 Chapter Summary......Page 280 Answers to Self-Test Exercises......Page 281 Programming Projects......Page 286 Chapter 5 Defining Classes II......Page 292 Static Methods......Page 294 PITFALL: Invoking a Nonstatic Method Within a Static Method......Page 296 TIP: You Can Put a main in Any Class......Page 297 Static Variables......Page 301 The Math Class......Page 306 Wrapper Classes......Page 310 Automatic Boxing and Unboxing......Page 311 Static Methods in Wrapper Classes......Page 313 PITFALL: A Wrapper Class Does Not Have a No-Argument Constructor......Page 316 5.2 REFERENCES AND CLASS PARAMETERS......Page 317 Variables and Memory......Page 318 References......Page 319 Class Parameters......Page 324 PITFALL: Use of = and == with Variables of a Class Type......Page 328 The Constant null......Page 330 The new Operator and Anonymous Objects......Page 331 Example: Another Approach to Keyboard Input......Page 332 TIP: Use Static Imports......Page 334 5.3 USING AND MISUSING REFERENCES......Page 336 Example: A Person Class......Page 337 PITFALL: null Can Be an Argument to a Method......Page 342 Copy Constructors......Page 346 PITFALL: Privacy Leaks......Page 348 Mutable and Immutable Classes......Page 352 Tip: Deep Copy versus Shallow Copy......Page 354 5.4 PACKAGES AND JAVADOC......Page 355 Packages and import Statements......Page 356 Package Names and Directories......Page 357 The Default Package......Page 360 Specifying a Class Path When You Compile......Page 361 Name Clashes......Page 362 Commenting Classes for javadoc......Page 363 Running javadoc......Page 365 Chapter Summary......Page 367 Answers to Self-Test Exercises......Page 368 Programming Projects......Page 372 Chapter 6 Arrays......Page 378 6.1 INTRODUCTION TO ARRAYS......Page 379 Creating and Accessing Arrays......Page 380 The length Instance Variable......Page 383 PITFALL: Array Index Out of Bounds......Page 385 Initializing Arrays......Page 386 PITFALL: An Array of Characters Is Not a String......Page 388 Arrays Are Objects......Page 389 Array Parameters......Page 391 PITFALL: Use of = and == with Arrays......Page 393 Arguments for the Method main......Page 398 Methods that Return an Array......Page 400 6.3 PROGRAMMING WITH ARRAYS......Page 401 Partially Filled Arrays......Page 402 Example: A Class for Partially Filled Arrays......Page 405 The “for-each” Loop......Page 409 Methods with a Variable Number of Parameters......Page 413 Example: A String Processing Example......Page 416 Privacy Leaks with Array Instance Variables......Page 417 Example: Sorting an Array......Page 421 Enumerated Types......Page 425 TIP: Enumerated Types in switch Statements......Page 430 Multidimensional Array Basics......Page 432 Using the length Instance Variable......Page 435 Multidimensional Array Parameters and Returned Values......Page 436 Example: A Grade Book Class......Page 437 Chapter Summary......Page 443 Answers to Self-Test Exercises......Page 444 Programming Projects......Page 451 Chapter 7 Inheritance......Page 460 7.1 INHERITANCE BASICS......Page 461 Derived Classes......Page 462 Changing the Return Type of an Overridden Method......Page 472 Changing the Access Permission of an Overridden Method......Page 473 PITFALL: Overriding versus Overloading......Page 474 The super Constructor......Page 475 The this Constructor......Page 477 TIP: An Object of a Derived Class Has More than One Type......Page 478 PITFALL: The Terms Subclass and Superclass......Page 481 Example: An Enhanced StringTokenizer Class......Page 482 7.2 ENCAPSULATION AND INHERITANCE......Page 485 PITFALL: Use of Private Instance Variables from the Base Class......Page 486 PITFALL: Private Methods Are Effectively Not Inherited......Page 487 Protected and Package Access......Page 488 PITFALL: A Restriction on Protected Access......Page 491 Access to a Redefined Base Method......Page 494 PITFALL: You Cannot Use Multiple supers......Page 496 The Class Object......Page 497 The Right Way to Define equals......Page 498 TIP: getClass versus instanceof......Page 500 Chapter Summary......Page 505 Answers to Self-Test Exercises......Page 506 Programming Projects......Page 509 Chapter 8 Polymorphism and Abstract Classes......Page 516 8.1 POLYMORPHISM......Page 517 Late Binding......Page 518 The final Modifier......Page 520 Example: Sales Records......Page 521 Late Binding with toString......Page 528 PITFALL: No Late Binding for Static Methods......Page 529 Downcasting and Upcasting......Page 530 TIP: Checking to See Whether Downcasting Is Legitimate......Page 534 A First Look at the clone Method......Page 537 PITFALL: Sometimes the clone Method Return Type Is Object......Page 538 PITFALL: Limitations of Copy Constructors......Page 539 8.2 ABSTRACT CLASSES......Page 542 Abstract Classes......Page 543 PITFALL: You Cannot Create Instances of an Abstract Class......Page 547 TIP: An Abstract Class Is a Type......Page 548 Answers to Self-Test Exercises......Page 549 Programming Projects......Page 551 Chapter 9 Exception Handling......Page 556 try-catch Mechanism......Page 558 Exception Handling with the Scanner Class......Page 560 TIP: Exception Controlled Loops......Page 561 Throwing Exceptions......Page 563 EXAMPLE: A Toy Example of Exception Handling......Page 565 Exception Classes......Page 570 Exception Classes from Standard Packages......Page 571 Defining Exception Classes......Page 573 TIP: Preserve getMessage......Page 577 TIP: An Exception Class Can Carry a Message of Any Type......Page 579 Multiple catch Blocks......Page 584 PITFALL: Catch the More Specific Exception First......Page 586 Throwing an Exception in a Method......Page 589 Declaring Exceptions in a throws Clause......Page 591 Exceptions to the Catch or Declare Rule......Page 594 throws Clause in Derived Classes......Page 595 When to Use Exceptions......Page 596 Example: Retrieving a High Score......Page 597 Event-Driven Programming......Page 600 The finally Block......Page 602 The AssertionError Class......Page 604 Chapter Summary......Page 605 Answers to Self-Test Exercises......Page 606 Programming Projects......Page 610 Chapter 10 File I/O......Page 614 Streams......Page 615 Text Files and Binary Files......Page 616 Writing to a Text File......Page 617 PITFALL: Overwriting an Output File......Page 623 Appending to a Text File......Page 624 TIP: toString Helps with Text File Output......Page 625 Reading a Text File Using Scanner......Page 626 Testing for the End of a Text File with Scanner......Page 629 Reading a Text File Using BufferedReader......Page 636 Testing for the End of a Text File with BufferedReader......Page 640 Path Names......Page 642 Nested Constructor Invocations......Page 643 System.in, System.out, and System.err......Page 644 Programming with the File Class......Page 646 10.4 BINARY FILES......Page 650 Writing Simple Data to a Binary File......Page 651 UTF and writeUTF......Page 655 Reading Simple Data from a Binary File......Page 656 Checking for the End of a Binary File......Page 661 PITFALL: Checking for the End of a File in the Wrong Way......Page 662 Binary I/O of Objects......Page 663 The Serializable Interface......Page 664 Array Objects in Binary Files......Page 667 Reading and Writing to the Same File......Page 669 Chapter Summary......Page 675 Answers to Self-Test Exercises......Page 676 Programming Projects......Page 680 Chapter 11 Recursion......Page 684 Example: Vertical Numbers......Page 686 Tracing a Recursive Call......Page 689 A Closer Look at Recursion......Page 692 PITFALL: Infinite Recursion......Page 694 Stacks for Recursion......Page 695 Recursion versus Iteration......Page 697 11.2 RECURSIVE METHODS THAT RETURN A VALUE......Page 698 Example: Another Powers Method......Page 699 Recursive Design Techniques......Page 704 Binary Search......Page 705 Efficiency of Binary Search......Page 711 Example: Finding a File......Page 713 Answers to Self-Test Exercises......Page 716 Programming Projects......Page 721 Chapter 12 UML and Patterns......Page 726 12.1 UML......Page 727 UML Class Diagrams......Page 728 Inheritance Diagrams......Page 729 More UML......Page 731 Adaptor Pattern......Page 732 The Model-View-Controller Pattern......Page 733 EXAMPLE: A Sorting Pattern......Page 734 Efficiency of the Sorting Pattern......Page 740 Pattern Formalism......Page 741 Answers to Self-Test Exercises......Page 742 Programming Projects......Page 744 Chapter 13 Interfaces and Inner Classes......Page 748 Interfaces......Page 750 Derived Interfaces......Page 752 PITFALL: Interface Semantics Are Not Enforced......Page 754 The Comparable Interface......Page 756 Example: Using the Comparable Interface......Page 757 Defined Constants in Interfaces......Page 762 PITFALL: Inconsistent Interfaces......Page 763 The Cloneable Interface......Page 766 Helping Classes......Page 771 Example: A Bank Account Class......Page 772 The .class File for an Inner Class......Page 776 Static Inner Classes......Page 777 Public Inner Classes......Page 778 TIP: Referring to a Method of the Outer Class......Page 780 Inner Classes and Inheritance......Page 782 Anonymous Classes......Page 783 TIP: Why Use Inner Classes?......Page 785 Answers to Self-Test Exercises......Page 786 Programming Projects......Page 791 Chapter 14 Generics and the ArrayList Class......Page 796 14.1 THE ARRAYLIST CLASS......Page 798 Using the ArrayList Class......Page 799 TIP: Summary of Adding to an ArrayList......Page 803 Methods in the Class ArrayList......Page 804 The “for-each” Loop......Page 807 Example: Golf Scores......Page 810 PITFALL: The clone Method Makes a Shallow Copy......Page 813 The Vector Class......Page 814 14.2 GENERICS......Page 815 Generic Basics......Page 816 Example: A Generic Class for Ordered Pairs......Page 818 PITFALL: A Generic Constructor Name Has No Type Parameter......Page 821 PITFALL: A Type Parameter Cannot Be Used Everywhere a Type Name Can Be Used......Page 822 PITFALL: An Instantiation of a Generic Class Cannot be an Array Base Type......Page 823 TIP: A Class Definition Can Have More Than One Type Parameter......Page 824 PITFALL: A Generic Class Cannot Be an Exception Class......Page 825 Bounds for Type Parameters......Page 826 Generic Methods......Page 829 Inheritance with Generic Classes......Page 831 Answers to Self-Test Exercises......Page 833 Programming Projects......Page 836 Chapter 15 Linked Data Structures......Page 840 Example: A Simple Linked List Class......Page 843 Working with Linked Lists......Page 847 PITFALL: Privacy Leaks......Page 852 Node Inner Classes......Page 853 Example: A Generic Linked List......Page 856 The equals Method for Linked Lists......Page 861 Simple Copy Constructors and clone Methods......Page 863 Exceptions......Page 864 PITFALL: The clone Method Is Protected in object......Page 866 TIP: Use a Type Parameter Bound for a Better clone......Page 867 Example: A Linked List with a Deep Copy clone Method......Page 871 15.3 ITERATORS......Page 874 Defining an Iterator Class......Page 875 Adding and Deleting Nodes......Page 880 Doubly Linked List......Page 885 The Stack Data Structure......Page 894 The Queue Data Structure......Page 896 Running Times and Big-O Notation......Page 899 Efficiency of Linked Lists......Page 904 15.5 HASH TABLES WITH CHAINING......Page 905 A Hash Function for Strings......Page 906 Efficiency of Hash Tables......Page 909 15.6 SETS......Page 910 Fundamental Set Operations......Page 911 Efficiency of Sets Using Linked Lists......Page 916 Tree Properties......Page 917 Example: A Binary Search Tree Class......Page 920 Efficiency of Binary Search Trees......Page 925 Chapter Summary......Page 926 Answers to Self-Test Exercises......Page 927 Programming Projects......Page 932 Chapter 16 Collections, Maps and Iterators......Page 936 16.1 COLLECTIONS......Page 937 The Collection Framework......Page 939 PITFALL: Optional Operations......Page 945 TIP: Dealing with All Those Exceptions......Page 946 Concrete Collection Classes......Page 947 Nonparameterized Version of the Collection Framework......Page 957 16.2 MAPS......Page 958 Concrete Map Classes......Page 961 The Iterator Interface......Page 965 TIP: For-Each Loops as Iterators......Page 968 List Iterators......Page 969 PITFALL: next Can Return a Reference......Page 971 TIP: Defining Your Own Iterator Classes......Page 973 Answers to Self-Test Exercises......Page 974 Programming Projects......Page 975 Chapter 17 Swing I......Page 982 Events and Listeners......Page 984 17.2 BUTTONS, EVENTS, AND OTHER SWING BASICS......Page 985 Example: A Simple Window......Page 986 PITFALL: Forgetting to Program the Close-Window Button......Page 991 Buttons......Page 992 Action Listeners and Action Events......Page 993 TIP: Ending a Swing Program......Page 995 Example: A Better Version of Our First Swing GUI......Page 996 Labels......Page 999 Color......Page 1000 Example: A GUI with a Label and Color......Page 1001 17.3 CONTAINERS AND LAYOUT MANAGERS......Page 1003 Border Layout Managers......Page 1004 Flow Layout Managers......Page 1007 Grid Layout Managers......Page 1008 Panels......Page 1012 Example: A Tricolor Built with Panels......Page 1013 The Container Class......Page 1017 TIP: Code a GUI’s Look and Actions Separately......Page 1020 The Model-View-Controller Pattern......Page 1021 Menus, Menu Items, and Menu Bars......Page 1022 The AbstractButton Class......Page 1027 The setActionCommand Method......Page 1030 Listeners as Inner Classes......Page 1031 17.5 TEXT FIELDS AND TEXT AREAS......Page 1034 Text Areas and Text Fields......Page 1035 TIP: Inputting and Outputting Numbers......Page 1041 A Swing Calculator......Page 1042 Chapter Summary......Page 1047 Answers to Self-Test Exercises......Page 1048 Programming Projects......Page 1054 Chapter 18 Swing II......Page 1058 18.1 WINDOW LISTENERS......Page 1059 Example: A Window Listener Inner Class......Page 1061 The dispose Method......Page 1064 The WindowAdapter Class......Page 1065 Icons......Page 1067 Scroll Bars......Page 1073 Example: Components with Changing Visibility......Page 1078 Coordinate System for Graphics Objects......Page 1082 The Method paint and the Class Graphics......Page 1083 Drawing Arcs......Page 1088 Rounded Rectangles......Page 1092 Action Drawings and repaint......Page 1093 18.4 COLORS......Page 1099 Specifying a Drawing Color......Page 1100 Defining Colors......Page 1101 PITFALL: Using doubles to Define a Color......Page 1102 The JColorChooser Dialog Window......Page 1103 The drawString Method......Page 1106 Fonts......Page 1109 Answers to Self-Test Exercises......Page 1112 Programming Projects......Page 1116 Chapter 19 Java Never Ends......Page 1120 19.1 MULTITHREADING......Page 1121 Thread.sleep......Page 1122 The getGraphics Method......Page 1126 Example: A Multithreaded Program......Page 1127 The Class Thread......Page 1128 The Runnable Interface......Page 1131 Race Conditions and Thread Synchronization......Page 1134 Sockets......Page 1139 Sockets and Threading......Page 1143 19.3 JAVA BEANS......Page 1144 The JavaBeans Model......Page 1145 Relational Databases......Page 1146 Java DB and JDBC......Page 1147 SQL......Page 1148 Applets, Servlets, and Java Server Pages......Page 1159 Oracle GlassFish Enterprise Server......Page 1161 HTML Forms—the Common Gateway Interface......Page 1162 JSP Declarations, Expressions, Scriptlets, and Directives......Page 1164 19.6 INTRODUCTION TO FUNCTIONAL PROGRAMMING IN JAVA 8......Page 1173 19.7 INTRODUCTION TO JAVA FX......Page 1181 Chapter Summary......Page 1194 Answers to Self-Test Exercises......Page 1195 Programming Projects......Page 1197 Appendix 1 Keywords......Page 1200 Appendix 2 Precedence and Associativity Rules......Page 1202 Appendix 3 ASCII Character Set......Page 1204 Appendix 4 Format Specifications for printf......Page 1206 Appendix 5 Summary of Classes and Interfaces......Page 1208 A......Page 1276 B......Page 1277 C......Page 1278 D......Page 1281 E......Page 1282 F......Page 1283 I......Page 1284 L......Page 1286 M......Page 1287 O......Page 1290 P......Page 1291 R......Page 1292 S......Page 1293 U......Page 1295 Y......Page 1296 Cover 1 Title Page 4 Copyright Page 5 Preface 6 Acknowledgments 8 Brief Contents 14 Contents 16 Chapter 1 Getting Started 34 1.1 INTRODUCTION TO JAVA 35 Origins of the Java Language 35 Objects and Methods 36 Applets 37 A Sample Java Application Program 38 Byte-Code and the Java Virtual Machine 41 Class Loader 43 Compiling a Java Program or Class 43 Running a Java Program 44 TIP: Error Messages 45 1.2 EXPRESSIONS AND ASSIGNMENT STATEMENTS 46 Identifiers 46 Variables 48 Assignment Statements 49 TIP: Initialize Variables 51 More Assignment Statements 52 Assignment Compatibility 53 Constants 54 Arithmetic Operators and Expressions 56 Parentheses and Precedence Rules 57 Integer and Floating-Point Division 59 PITFALL: Round-Off Errors in Floating-Point Numbers 60 PITFALL: Division with Whole Numbers 61 Type Casting 62 Increment and Decrement Operators 63 1.3 THE CLASS STRING 66 String Constants and Variables 66 Concatenation of Strings 67 Classes 68 String Methods 70 Escape Sequences 75 String Processing 76 The Unicode Character Set 76 1.4 PROGRAM STYLE 79 Naming Constants 79 Java Spelling Conventions 81 Comments 82 Indenting 83 Chapter Summary 84 Answers to Self-Test Exercises 85 Programming Projects 87 Chapter 2 Console Input and Output 90 2.1 SCREEN OUTPUT 91 System.out.println 91 TIP: Different Approaches to Formatting Output 94 Formatting Output with printf 94 TIP: Formatting Monetary Amounts with printf 98 TIP: Legacy Code 99 Money Formats Using NumberFormat 100 Importing Packages and Classes 103 The DecimalFormat Class 105 2.2 CONSOLE INPUT USING THE SCANNER CLASS 109 The Scanner Class 109 PITFALL: Dealing with the Line Terminator, '\n' 116 The Empty String 117 TIP: Prompt for Input 117 TIP: Echo Input 117 Example: Self-Service Checkout 119 Other Input Delimiters 120 2.3 INTRODUCTION TO FILE INPUT 122 The Scanner Class for Text File Input 122 Chapter Summary 125 Answers to Self-Test Exercises 125 Programming Projects 128 Chapter 3 Flow of Control 132 3.1 BRANCHING MECHANISM 133 if-else Statements 133 Omitting the else 134 Compound Statements 135 TIP: Placing of Braces 136 Nested Statements 137 Multiway if-else Statement 137 Example: State Income Tax 138 The switch Statement 140 PITFALL: Forgetting a break in a switch Statement 144 The Conditional Operator 145 3.2 BOOLEAN EXPRESSIONS 146 Simple Boolean Expressions 146 PITFALL: Using = in Place of == 147 PITFALL: Using == with Strings 148 Lexicographic and Alphabetic Order 149 Building Boolean Expressions 152 PITFALL: Strings of Inequalities 153 Evaluating Boolean Expressions 153 TIP: Naming Boolean Variables 156 Short-Circuit and Complete Evaluation 157 Precedence and Associativity Rules 158 3.3 LOOPS 165 while Statement and do-while Statement 165 Algorithms and Pseudocode 167 Example: Averaging a List of Scores 170 The for Statement 171 The Comma in for Statements 174 TIP: Repeat N Times Loops 176 PITFALL: Extra Semicolon in a for Statement 176 PITFALL: Infinite Loops 177 Nested Loops 178 The break and continue Statements 181 The exit Statement 182 3.4 DEBUGGING 183 Loop Bugs 183 Tracing Variables 183 General Debugging Techniques 184 Example: Debugging an Input Validation Loop 185 Preventive Coding 189 Assertion Checks 190 3.5 RANDOM NUMBER GENERATION 192 The Random Object 192 The Math.random() Method 194 Chapter Summary 195 Answers to Self-Test Exercises 195 Programming Projects 201 Chapter 4 Defining Classes I 206 4.1 CLASS DEFINITIONS 207 Instance Variables and Methods 210 More about Methods 213 TIP: Any Method Can Be Used as a void Method 217 Local Variables 219 Blocks 220 TIP: Declaring Variables in a for Statement 221 Parameters of a Primitive Type 221 PITFALL: Use of the Terms “Parameter” and “Argument” 228 Simple Cases with Class Parameters 230 The this Parameter 230 Methods That Return a Boolean Value 232 The Methods equals and toString 235 Recursive Methods 238 TIP: Testing Methods 238 4.2 INFORMATION HIDING AND ENCAPSULATION 240 public and private Modifiers 241 Example: Yet Another Date Class 242 Accessor and Mutator Methods 243 TIP: A Class Has Access to Private Members of All Objects of the Class 248 TIP: Mutator Methods Can Return a Boolean Value 249 Preconditions and Postconditions 250 4.3 OVERLOADING 251 Rules for Overloading 251 PITFALL: Overloading and Automatic Type Conversion 255 PITFALL: You Cannot Overload Based on the Type Returned 257 4.4 CONSTRUCTORS 259 Constructor Definitions 259 TIP: You Can Invoke Another Method in a Constructor 267 TIP: A Constructor Has a this Parameter 267 TIP: Include a No-Argument Constructor 268 Example: The Final Date Class 269 Default Variable Initializations 270 An Alternative Way to Initialize Instance Variables 270 Example: A Pet Record Class 271 The StringTokenizer Class 275 Chapter Summary 280 Answers to Self-Test Exercises 281 Programming Projects 286 Chapter 5 Defining Classes II 292 5.1 STATIC METHODS AND STATIC VARIABLES 294 Static Methods 294 PITFALL: Invoking a Nonstatic Method Within a Static Method 296 TIP: You Can Put a main in Any Class 297 Static Variables 301 The Math Class 306 Wrapper Classes 310 Automatic Boxing and Unboxing 311 Static Methods in Wrapper Classes 313 PITFALL: A Wrapper Class Does Not Have a No-Argument Constructor 316 5.2 REFERENCES AND CLASS PARAMETERS 317 Variables and Memory 318 References 319 Class Parameters 324 PITFALL: Use of = and == with Variables of a Class Type 328 The Constant null 330 PITFALL: Null Pointer Exception 331 The new Operator and Anonymous Objects 331 Example: Another Approach to Keyboard Input 332 TIP: Use Static Imports 334 5.3 USING AND MISUSING REFERENCES 336 Example: A Person Class 337 PITFALL: null Can Be an Argument to a Method 342 Copy Constructors 346 PITFALL: Privacy Leaks 348 Mutable and Immutable Classes 352 Tip: Deep Copy versus Shallow Copy 354 TIP: Assume Your Coworkers Are Malicious 355 5.4 PACKAGES AND JAVADOC 355 Packages and import Statements 356 The Package java.lang 357 Package Names and Directories 357 PITFALL: Subdirectories Are Not Automatically Imported 360 The Default Package 360 PITFALL: Not Including the Current Directory in Your Class Path 361 Specifying a Class Path When You Compile 361 Name Clashes 362 Introduction to javadoc 363 Commenting Classes for javadoc 363 Running javadoc 365 Chapter Summary 367 Answers to Self-Test Exercises 368 Programming Projects 372 Chapter 6 Arrays 378 6.1 INTRODUCTION TO ARRAYS 379 Creating and Accessing Arrays 380 The length Instance Variable 383 TIP: Use for Loops with Arrays 385 PITFALL: Array Indices Always Start with Zero 385 PITFALL: Array Index Out of Bounds 385 Initializing Arrays 386 PITFALL: An Array of Characters Is Not a String 388 6.2 ARRAYS AND REFERENCES 389 Arrays Are Objects 389 PITFALL: Arrays with a Class Base Type 391 Array Parameters 391 PITFALL: Use of = and == with Arrays 393 Arguments for the Method main 398 Methods that Return an Array 400 6.3 PROGRAMMING WITH ARRAYS 401 Partially Filled Arrays 402 Example: A Class for Partially Filled Arrays 405 TIP: Accessor Methods Need Not Simply Return Instance Variables 409 The “for-each” Loop 409 Methods with a Variable Number of Parameters 413 Example: A String Processing Example 416 Privacy Leaks with Array Instance Variables 417 Example: Sorting an Array 421 Enumerated Types 425 TIP: Enumerated Types in switch Statements 430 6.4 MULTIDIMENSIONAL ARRAYS 432 Multidimensional Array Basics 432 Using the length Instance Variable 435 Ragged Arrays 436 Multidimensional Array Parameters and Returned Values 436 Example: A Grade Book Class 437 Chapter Summary 443 Answers to Self-Test Exercises 444 Programming Projects 451 Chapter 7 Inheritance 460 7.1 INHERITANCE BASICS 461 Derived Classes 462 Overriding a Method Definition 472 Changing the Return Type of an Overridden Method 472 Changing the Access Permission of an Overridden Method 473 PITFALL: Overriding versus Overloading 474 The super Constructor 475 The this Constructor 477 TIP: An Object of a Derived Class Has More than One Type 478 PITFALL: The Terms Subclass and Superclass 481 Example: An Enhanced StringTokenizer Class 482 7.2 ENCAPSULATION AND INHERITANCE 485 PITFALL: Use of Private Instance Variables from the Base Class 486 PITFALL: Private Methods Are Effectively Not Inherited 487 Protected and Package Access 488 PITFALL: Forgetting about the Default Package 491 PITFALL: A Restriction on Protected Access 491 7.3 PROGRAMMING WITH INHERITANCE 494 TIP: Static Variables Are Inherited 494 TIP: “is a” versus “has a” 494 Access to a Redefined Base Method 494 PITFALL: You Cannot Use Multiple supers 496 The Class Object 497 The Right Way to Define equals 498 TIP: getClass versus instanceof 500 Chapter Summary 505 Answers to Self-Test Exercises 506 Programming Projects 509 Chapter 8 Polymorphism and Abstract Classes 516 8.1 POLYMORPHISM 517 Late Binding 518 The final Modifier 520 Example: Sales Records 521 Late Binding with toString 528 PITFALL: No Late Binding for Static Methods 529 Downcasting and Upcasting 530 PITFALL: Downcasting 534 TIP: Checking to See Whether Downcasting Is Legitimate 534 A First Look at the clone Method 537 PITFALL: Sometimes the clone Method Return Type Is Object 538 PITFALL: Limitations of Copy Constructors 539 8.2 ABSTRACT CLASSES 542 Abstract Classes 543 PITFALL: You Cannot Create Instances of an Abstract Class 547 TIP: An Abstract Class Is a Type 548 Chapter Summary 549 Answers to Self-Test Exercises 549 Programming Projects 551 Chapter 9 Exception Handling 556 9.1 EXCEPTION HANDLING BASICS 558 try-catch Mechanism 558 Exception Handling with the Scanner Class 560 TIP: Exception Controlled Loops 561 Throwing Exceptions 563 EXAMPLE: A Toy Example of Exception Handling 565 Exception Classes 570 Exception Classes from Standard Packages 571 Defining Exception Classes 573 TIP: Preserve getMessage 577 TIP: An Exception Class Can Carry a Message of Any Type 579 Multiple catch Blocks 584 PITFALL: Catch the More Specific Exception First 586 9.2 THROWING EXCEPTIONS IN METHODS 589 Throwing an Exception in a Method 589 Declaring Exceptions in a throws Clause 591 Exceptions to the Catch or Declare Rule 594 throws Clause in Derived Classes 595 When to Use Exceptions 596 Example: Retrieving a High Score 597 Event-Driven Programming 600 9.3 MORE PROGRAMMING TECHNIQUES FOR EXCEPTION HANDLING 602 PITFALL: Nested try-catch Blocks 602 The finally Block 602 Rethrowing an Exception 604 The AssertionError Class 604 ArrayIndexOutOfBoundsException 605 Chapter Summary 605 Answers to Self-Test Exercises 606 Programming Projects 610 Chapter 10 File I/O 614 10.1 INTRODUCTION TO FILE I/O 615 Streams 615 Text Files and Binary Files 616 10.2 TEXT FILES 617 Writing to a Text File 617 PITFALL: A try Block Is a Block 623 PITFALL: Overwriting an Output File 623 Appending to a Text File 624 TIP: toString Helps with Text File Output 625 Reading from a Text File 626 Reading a Text File Using Scanner 626 Testing for the End of a Text File with Scanner 629 Reading a Text File Using BufferedReader 636 TIP: Reading Numbers with BufferedReader 640 Testing for the End of a Text File with BufferedReader 640 Path Names 642 Nested Constructor Invocations 643 System.in, System.out, and System.err 644 10.3 THE FILE CLASS 646 Programming with the File Class 646 10.4 BINARY FILES 650 Writing Simple Data to a Binary File 651 UTF and writeUTF 655 Reading Simple Data from a Binary File 656 Checking for the End of a Binary File 661 PITFALL: Checking for the End of a File in the Wrong Way 662 Binary I/O of Objects 663 The Serializable Interface 664 PITFALL: Mixing Class Types in the Same File 667 Array Objects in Binary Files 667 10.5 RANDOM ACCESS TO BINARY FILES 669 Reading and Writing to the Same File 669 PITFALL: RandomAccessFile Need Not Start Empty 675 Chapter Summary 675 Answers to Self-Test Exercises 676 Programming Projects 680 Chapter 11 Recursion 684 11.1 RECURSIVE VOID METHODS 686 Example: Vertical Numbers 686 Tracing a Recursive Call 689 A Closer Look at Recursion 692 PITFALL: Infinite Recursion 694 Stacks for Recursion 695 PITFALL: Stack Overflow 697 Recursion versus Iteration 697 11.2 RECURSIVE METHODS THAT RETURN A VALUE 698 General Form for a Recursive Method That Returns a Value 699 Example: Another Powers Method 699 11.3 THINKING RECURSIVELY 704 Recursive Design Techniques 704 Binary Search 705 Efficiency of Binary Search 711 Example: Finding a File 713 Chapter Summary 716 Answers to Self-Test Exercises 716 Programming Projects 721 Chapter 12 UML and Patterns 726 12.1 UML 727 History of UML 728 UML Class Diagrams 728 Class Interactions 729 Inheritance Diagrams 729 More UML 731 12.2 PATTERNS 732 Adaptor Pattern 732 The Model-View-Controller Pattern 733 EXAMPLE: A Sorting Pattern 734 Restrictions on the Sorting Pattern 740 Efficiency of the Sorting Pattern 740 TIP: Pragmatics and Patterns 741 Pattern Formalism 741 Chapter Summary 742 Answers to Self-Test Exercises 742 Programming Projects 744 Chapter 13 Interfaces and Inner Classes 748 13.1 INTERFACES 750 Interfaces 750 Abstract Classes Implementing Interfaces 752 Derived Interfaces 752 PITFALL: Interface Semantics Are Not Enforced 754 The Comparable Interface 756 Example: Using the Comparable Interface 757 Defined Constants in Interfaces 762 PITFALL: Inconsistent Interfaces 763 The Serializable Interface 766 The Cloneable Interface 766 13.2 SIMPLE USES OF INNER CLASSES 771 Helping Classes 771 TIP: Inner and Outer Classes Have Access to Each Other’s Private Members 772 Example: A Bank Account Class 772 The .class File for an Inner Class 776 PITFALL: Other Uses of Inner Classes 777 13.3 MORE ABOUT INNER CLASSES 777 Static Inner Classes 777 Public Inner Classes 778 TIP: Referring to a Method of the Outer Class 780 Nesting Inner Classes 782 Inner Classes and Inheritance 782 Anonymous Classes 783 TIP: Why Use Inner Classes? 785 Chapter Summary 786 Answers to Self-Test Exercises 786 Programming Projects 791 Chapter 14 Generics and the ArrayList Class 796 14.1 THE ARRAYLIST CLASS 798 Using the ArrayList Class 799 TIP: Summary of Adding to an ArrayList 803 Methods in the Class ArrayList 804 The “for-each” Loop 807 Example: Golf Scores 810 TIP: Use trimToSize to Save Memory 813 PITFALL: The clone Method Makes a Shallow Copy 813 The Vector Class 814 Parameterized Classes and Generics 815 PITFALL: Nonparameterized ArrayList and Vector Classes 815 14.2 GENERICS 815 Generic Basics 816 TIP: Compile with the -Xlint Option 818 Example: A Generic Class for Ordered Pairs 818 PITFALL: A Generic Constructor Name Has No Type Parameter 821 PITFALL: You Cannot Plug in a Primitive Type for a Type Parameter 822 PITFALL: A Type Parameter Cannot Be Used Everywhere a Type Name Can Be Used 822 PITFALL: An Instantiation of a Generic Class Cannot be an Array Base Type 823 TIP: A Class Definition Can Have More Than One Type Parameter 824 PITFALL: A Generic Class Cannot Be an Exception Class 825 Bounds for Type Parameters 826 TIP: Generic Interfaces 829 Generic Methods 829 Inheritance with Generic Classes 831 Chapter Summary 833 Answers to Self-Test Exercises 833 Programming Projects 836 Chapter 15 Linked Data Structures 840 15.1 JAVA LINKED LISTS 843 Example: A Simple Linked List Class 843 Working with Linked Lists 847 PITFALL: Privacy Leaks 852 Node Inner Classes 853 Example: A Generic Linked List 856 PITFALL: Using Node Instead of Node 861 The equals Method for Linked Lists 861 15.2 COPY CONSTRUCTORS AND THE CLONE METHOD 863 Simple Copy Constructors and clone Methods 863 Exceptions 864 PITFALL: The clone Method Is Protected in object 866 TIP: Use a Type Parameter Bound for a Better clone 867 Example: A Linked List with a Deep Copy clone Method 871 TIP: Cloning Is an “All or Nothing” Affair 874 15.3 ITERATORS 874 Defining an Iterator Class 875 Adding and Deleting Nodes 880 15.4 VARIATIONS ON A LINKED LIST 885 Doubly Linked List 885 The Stack Data Structure 894 The Queue Data Structure 896 Running Times and Big-O Notation 899 Efficiency of Linked Lists 904 15.5 HASH TABLES WITH CHAINING 905 A Hash Function for Strings 906 Efficiency of Hash Tables 909 15.6 SETS 910 Fundamental Set Operations 911 Efficiency of Sets Using Linked Lists 916 15.7 TREES 917 Tree Properties 917 Example: A Binary Search Tree Class 920 Efficiency of Binary Search Trees 925 Chapter Summary 926 Answers to Self-Test Exercises 927 Programming Projects 932 Chapter 16 Collections, Maps and Iterators 936 16.1 COLLECTIONS 937 Wildcards 939 The Collection Framework 939 PITFALL: Optional Operations 945 TIP: Dealing with All Those Exceptions 946 Concrete Collection Classes 947 Differences between ArrayList and Vector 957 Nonparameterized Version of the Collection Framework 957 PITFALL: Omitting the 958 16.2 MAPS 958 Concrete Map Classes 961 16.3 ITERATORS 965 The Iterator Concept 965 The Iterator Interface 965 TIP: For-Each Loops as Iterators 968 List Iterators 969 PITFALL: next Can Return a Reference 971 TIP: Defining Your Own Iterator Classes 973 Chapter Summary 974 Answers to Self-Test Exercises 974 Programming Projects 975 Chapter 17 Swing I 982 17.1 EVENT-DRIVEN PROGRAMMING 984 Events and Listeners 984 17.2 BUTTONS, EVENTS, AND OTHER SWING BASICS 985 Example: A Simple Window 986 PITFALL: Forgetting to Program the Close-Window Button 991 Buttons 992 Action Listeners and Action Events 993 PITFALL: Changing the Heading for actionPerformed 995 TIP: Ending a Swing Program 995 Example: A Better Version of Our First Swing GUI 996 Labels 999 Color 1000 Example: A GUI with a Label and Color 1001 17.3 CONTAINERS AND LAYOUT MANAGERS 1003 Border Layout Managers 1004 Flow Layout Managers 1007 Grid Layout Managers 1008 Panels 1012 Example: A Tricolor Built with Panels 1013 The Container Class 1017 TIP: Code a GUI’s Look and Actions Separately 1020 The Model-View-Controller Pattern 1021 17.4 MENUS AND BUTTONS 1022 Example: A GUI with a Menu 1022 Menus, Menu Items, and Menu Bars 1022 Nested Menus 1027 The AbstractButton Class 1027 The setActionCommand Method 1030 Listeners as Inner Classes 1031 17.5 TEXT FIELDS AND TEXT AREAS 1034 Text Areas and Text Fields 1035 TIP: Labeling a Text Field 1041 TIP: Inputting and Outputting Numbers 1041 A Swing Calculator 1042 Chapter Summary 1047 Answers to Self-Test Exercises 1048 Programming Projects 1054 Chapter 18 Swing II 1058 18.1 WINDOW LISTENERS 1059 Example: A Window Listener Inner Class 1061 The dispose Method 1064 PITFALL: Forgetting to Invoke setDefaultCloseOperation 1065 The WindowAdapter Class 1065 18.2 ICONS AND SCROLL BARS 1067 Icons 1067 Scroll Bars 1073 Example: Components with Changing Visibility 1078 18.3 THE GRAPHICS CLASS 1082 Coordinate System for Graphics Objects 1082 The Method paint and the Class Graphics 1083 Drawing Ovals 1088 Drawing Arcs 1088 Rounded Rectangles 1092 paintComponent for Panels 1093 Action Drawings and repaint 1093 Some More Details on Updating a GUI 1099 18.4 COLORS 1099 Specifying a Drawing Color 1100 Defining Colors 1101 PITFALL: Using doubles to Define a Color 1102 The JColorChooser Dialog Window 1103 18.5 Fonts and the drawString Method 1106 The drawString Method 1106 Fonts 1109 Chapter Summary 1112 Answers to Self-Test Exercises 1112 Programming Projects 1116 Chapter 19 Java Never Ends 1120 19.1 MULTITHREADING 1121 Example: A Nonresponsive GUI 1122 Thread.sleep 1122 The getGraphics Method 1126 Fixing a Nonresponsive Program Using Threads 1127 Example: A Multithreaded Program 1127 The Class Thread 1128 The Runnable Interface 1131 Race Conditions and Thread Synchronization 1134 19.2 NETWORKING WITH STREAM SOCKETS 1139 Sockets 1139 Sockets and Threading 1143 The URL Class 1144 19.3 JAVA BEANS 1144 The Component Model 1145 The JavaBeans Model 1145 19.4 JAVA AND DATABASE CONNECTIONS 1146 Relational Databases 1146 Java DB and JDBC 1147 SQL 1148 19.5 WEB PROGRAMMING WITH JAVA SERVER PAGES 1159 Applets, Servlets, and Java Server Pages 1159 Oracle GlassFish Enterprise Server 1161 HTML Forms—the Common Gateway Interface 1162 JSP Declarations, Expressions, Scriptlets, and Directives 1164 19.6 INTRODUCTION TO FUNCTIONAL PROGRAMMING IN JAVA 8 1173 19.7 INTRODUCTION TO JAVA FX 1181 Chapter Summary 1194 Answers to Self-Test Exercises 1195 Programming Projects 1197 Appendix 1 Keywords 1200 Appendix 2 Precedence and Associativity Rules 1202 Appendix 3 ASCII Character Set 1204 Appendix 4 Format Specifications for printf 1206 Appendix 5 Summary of Classes and Interfaces 1208 Index 1276 A 1276 B 1277 C 1278 D 1281 E 1282 F 1283 G 1284 H 1284 I 1284 J 1286 K 1286 L 1286 M 1287 N 1290 O 1290 P 1291 Q 1292 R 1292 S 1293 T 1295 U 1295 V 1296 W 1296 X 1296 Y 1296 For courses in computer programming and engineering. Beginner to Intermediate Programming in Java This book is designed to serve as a textbook and reference for programming in the Java language. Although it does include programming techniques, it is organised around the features of the Java language rather than any particular curriculum of programming techniques. The main audience is undergraduate students who have not had extensive programming experience with the Java language. The introductory chapters are written at a level that is accessible to beginners, while the boxed sections of those chapters serve to quickly introduce more experienced programmers to basic Java syntax. Later chapters are still designed to be accessible, but are written at a level suitable for students who have progressed to these more advanced topics. The full text downloaded to your computer With eBooks you can: search for key concepts, words and phrases make highlights and notes as you study share your notes with friends eBooks are downloaded to your computer and accessible either offline through the Bookshelf (available as a free download), available online and also via the iPad and Android apps. Upon purchase, you'll gain instant access to this eBook. Time limit The eBooks products do not have an expiry date. You will continue to access your digital ebook products whilst you have your Bookshelf installed. This book is designed to serve as a textbook and reference for programming in the Java language. Although it does include programming techniques, it is organized around the features of the Java language rather than any particular curriculum of programming techniques. The main audience I had in mind when writing this book was undergraduate students who have not had extensive programming experience with the Java language. As such, it would be a suitable Java text or reference for either a first programming course or a later computer science course that uses Java. This book is designed to accommodate a wide range of users. The introductory chapters are written at a level that is accessible to beginners, while the boxed sections of those chapters serve to quickly introduce more experienced programmers to basic Java syntax. Later chapters are still designed to be accessible, but are written at a level suitable for students who have progressed to these more advanced topics For courses in computer programming and engineering. Beginner to Intermediate Programming in Java Absolute Java provides a comprehensive reference to programming in the Java language. Accessible to both beginner and intermediate programmers, the text focuses around specifically using the Java language to practice programming techniques. The Sixth Edition is extremely flexible and easily applicable to a wide range of users. Standalone and optional chapters allow instructors to adapt the text to a variety of curse content. Highly up-to-date with new content and information regarding the use of Java, this text introduces readers to the world of programming through a widely used and relevant language.
دانلود کتاب ABSOLUTE JAVA™ 6th Edition Global Edition