Java How to Program (Early Objects) [10th ed] 9780133807806, 1292018194, 9781292018195, 0133807800, 9780133813036, 0133813037

"Java How to Program (Early Objects), " Tenth Edition" is intended for use in the Java programming course

701 74 54MB

English Pages 1248 [1250] Year 2014;2015

Report DMCA / Copyright

DOWNLOAD FILE

Java How to Program (Early Objects) [10th ed]
 9780133807806, 1292018194, 9781292018195, 0133807800, 9780133813036, 0133813037

Table of contents :
Cover......Page 1
Title......Page 5
Copyright......Page 6
Foreword......Page 25
Preface......Page 27
Before You Begin......Page 41
1 Introduction to Computers, the Internet and Java......Page 45
1.1 Introduction......Page 46
1.2.1 Moore’s Law......Page 48
1.2.2 Computer Organization......Page 49
1.3 Data Hierarchy......Page 50
1.4 Machine Languages, Assembly Languages and High-Level Languages......Page 53
1.5.1 The Automobile as an Object......Page 54
1.5.6 Attributes and Instance Variables......Page 55
1.5.10 Object-Oriented Analysis and Design (OOAD)......Page 56
1.6.1 Windows—A Proprietary Operating System......Page 57
1.6.3 Android......Page 58
1.7 Programming Languages......Page 59
1.9 A Typical Java Development Environment......Page 61
1.10 Test-Driving a Java Application......Page 65
1.11 Internet and World Wide Web......Page 69
1.11.3 Web Services and Mashups......Page 70
1.11.5 The Internet of Things......Page 71
1.12 Software Technologies......Page 72
1.13 Keeping Up-to-Date with Information Technologies......Page 74
2 Introduction to Java Applications; Input/Output and Operators......Page 78
2.2 Your First Program in Java: Printing a Line of Text......Page 79
2.3 Modifying Your First Java Program......Page 85
2.4 Displaying Text with printf......Page 87
2.5.1 import Declarations......Page 89
2.5.3 Declaring and Creating a Scanner to Obtain User Input from the Keyboard......Page 90
2.5.4 Declaring Variables to Store Integers......Page 91
2.5.6 Obtaining an int as Input from the User......Page 92
2.5.10 Java API Documentation......Page 93
2.6 Memory Concepts......Page 94
2.7 Arithmetic......Page 95
2.8 Decision Making: Equality and Relational Operators......Page 98
2.9 Wrap-Up......Page 102
3 Introduction to Classes, Objects, Methods and Strings......Page 113
3.1 Introduction......Page 114
3.2.1 Account Class with an Instance Variable, a set Method and a get Method......Page 115
3.2.2 AccountTest Class That Creates and Uses an Object of ClassAccount......Page 118
3.2.4 Account UML Class Diagram with an Instance Variable and set and get Methods......Page 121
3.2.5 Additional Notes on Class AccountTest......Page 122
3.2.6 Software Engineering with private Instance Variables and public set and get Methods......Page 123
3.3 Primitive Types vs. Reference Types......Page 124
3.4.1 Declaring an Account Constructor for Custom Object Initialization......Page 125
3.4.2 Class AccountTest: Initializing Account Objects When They’re Created......Page 126
3.5 Account Class with a Balance; Floating-Point Numbers......Page 128
3.5.1 Account Class with a balance Instance Variable of Type double......Page 129
3.5.2 AccountTest Class to Use Class Account......Page 130
3.6 (Optional) GUI and Graphics Case Study: Using Dialog Boxes......Page 134
3.7 Wrap-Up......Page 137
4 Control Statements: Part 1; Assignment, ++ and -- Operators......Page 145
4.2 Algorithms......Page 146
4.4 Control Structures......Page 147
4.5 if Single-Selection Statement......Page 149
4.6 if…else Double-Selection Statement......Page 150
4.7 Student Class: Nested if…else Statements......Page 155
4.8 while Repetition Statement......Page 157
4.9 Formulating Algorithms: Counter-Controlled Repetition......Page 159
4.10 Formulating Algorithms: Sentinel-Controlled Repetition......Page 163
4.11 Formulating Algorithms: Nested Control Statements......Page 170
4.13 Increment and Decrement Operators......Page 175
4.14 Primitive Types......Page 178
4.15 (Optional) GUI and Graphics Case Study: Creating Simple Drawings......Page 179
4.16 Wrap-Up......Page 183
5 Control Statements: Part 2; Logical Operators......Page 196
5.2 Essentials of Counter-Controlled Repetition......Page 197
5.3 for Repetition Statement......Page 199
5.4 Examples Using the for Statement......Page 203
5.5 do…while Repetition Statement......Page 207
5.6 switch Multiple-Selection Statement......Page 209
5.7 Class AutoPolicy Case Study: Strings in switch Statements......Page 215
5.8 break and continue Statements......Page 218
5.9 Logical Operators......Page 220
5.10 Structured Programming Summary......Page 226
5.11 (Optional) GUI and Graphics Case Study: Drawing Rectangles and Ovals......Page 231
5.12 Wrap-Up......Page 234
6 Methods: A Deeper Look......Page 244
6.2 Program Modules in Java......Page 245
6.3 static Methods, static Fields and Class Math......Page 247
6.4 Declaring Methods with Multiple Parameters......Page 249
6.5 Notes on Declaring and Using Methods......Page 252
6.6 Method-Call Stack and Stack Frames......Page 253
6.7 Argument Promotion and Casting......Page 254
6.8 Java API Packages......Page 255
6.9 Case Study: Secure Random-Number Generation......Page 257
6.10 Case Study: A Game of Chance; Introducing enum Types......Page 262
6.11 Scope of Declarations......Page 266
6.12 Method Overloading......Page 269
6.13 (Optional) GUI and Graphics Case Study: Colors and Filled Shapes......Page 271
6.14 Wrap-Up......Page 274
7 Arrays and ArrayLists......Page 287
7.1 Introduction......Page 288
7.2 Arrays......Page 289
7.3 Declaring and Creating Arrays......Page 290
7.4.1 Creating and Initializing an Array......Page 291
7.4.2 Using an Array Initializer......Page 292
7.4.3 Calculating the Values to Store in an Array......Page 293
7.4.5 Using Bar Charts to Display Array Data Graphically......Page 295
7.4.6 Using the Elements of an Array as Counters......Page 297
7.4.7 Using Arrays to Analyze Survey Results......Page 298
7.5.2 Executing the catch Block......Page 300
7.6 Case Study: Card Shuffling and Dealing Simulation......Page 301
7.7 Enhanced for Statement......Page 306
7.8 Passing Arrays to Methods......Page 307
7.9 Pass-By-Value vs. Pass-By-Reference......Page 309
7.10 Case Study: Class GradeBook Using an Array to Store Grades......Page 310
7.11 Multidimensional Arrays......Page 316
7.12 Case Study: Class GradeBook Using a Two-Dimensional Array......Page 319
7.13 Variable-Length Argument Lists......Page 325
7.14 Using Command-Line Arguments......Page 327
7.15 Class Arrays......Page 329
7.16 Introduction to Collections and Class ArrayList......Page 331
7.17 (Optional) GUI and Graphics Case Study: Drawing Arcs......Page 335
7.18 Wrap-Up......Page 338
8 Classes and Objects: A Deeper Look......Page 359
8.2 Time Class Case Study......Page 360
8.3 Controlling Access to Members......Page 365
8.4 Referring to the Current Object’s Members with the this Reference......Page 366
8.5 Time Class Case Study: Overloaded Constructors......Page 368
8.7 Notes on Set and Get Methods......Page 374
8.8 Composition......Page 376
8.9 enum Types......Page 379
8.10 Garbage Collection......Page 381
8.11 static Class Members......Page 382
8.12 static Import......Page 386
8.13 final Instance Variables......Page 387
8.14 Package Access......Page 388
8.15 Using BigDecimal for Precise Monetary Calculations......Page 389
8.16 (Optional) GUI and Graphics Case Study: Using Objects with Graphics......Page 392
8.17 Wrap-Up......Page 396
9 Object-Oriented Programming: Inheritance......Page 404
9.1 Introduction......Page 405
9.2 Superclasses and Subclasses......Page 406
9.3 protected Members......Page 408
9.4.1 Creating and Using a CommissionEmployee Class......Page 409
9.4.2 Creating and Using a BasePlusCommissionEmployee Class......Page 415
9.4.3Creating a CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy......Page 420
9.4.4 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using protected Instance Variables......Page 423
9.4.5 CommissionEmployee–BasePlusCommissionEmployeeInheritance Hierarchy Using private Instance Variables......Page 426
9.6 Class Object......Page 431
9.7 (Optional) GUI and Graphics Case Study: Displaying Text and Images Using Labels......Page 432
9.8 Wrap-Up......Page 435
10 Object-Oriented Programming: Polymorphism and Interfaces......Page 439
10.1 Introduction......Page 440
10.2 Polymorphism Examples......Page 442
10.3 Demonstrating Polymorphic Behavior......Page 443
10.4 Abstract Classes and Methods......Page 445
10.5 Case Study: Payroll System Using Polymorphism......Page 448
10.5.1 Abstract Superclass Employee......Page 449
10.5.2 Concrete Subclass SalariedEmployee......Page 451
10.5.3 Concrete Subclass HourlyEmployee......Page 453
10.5.4 Concrete Subclass CommissionEmployee......Page 455
10.5.5 Indirect Concrete Subclass BasePlusCommissionEmployee......Page 457
10.5.6 Polymorphic Processing, Operator instanceof and Downcasting......Page 458
10.7 final Methods and Classes......Page 463
10.8 A Deeper Explanation of Issues with Calling Methods from Constructors......Page 464
10.9 Creating and Using Interfaces......Page 465
10.9.1 Developing a Payable Hierarchy......Page 466
10.9.2 Interface Payable......Page 467
10.9.3 Class Invoice......Page 468
10.9.4 Modifying Class Employee to Implement Interface Payable......Page 470
10.9.5 Modifying Class SalariedEmployee for Use in the Payable Hierarchy......Page 472
10.9.6 Using Interface Payable to Process Invoices and Employees Polymorphically......Page 474
10.9.7 Some Common Interfaces of the Java API......Page 475
10.10.1 default Interface Methods......Page 476
10.11 (Optional) GUI and Graphics Case Study: Drawing with Polymorphism......Page 477
10.12 Wrap-Up......Page 480
11 Exception Handling: A Deeper Look......Page 485
11.1 Introduction......Page 486
11.2 Example: Divide by Zero without Exception Handling......Page 487
11.3 Example: Handling ArithmeticExceptions and InputMismatchExceptions......Page 489
11.5 Java Exception Hierarchy......Page 495
11.6 finally Block......Page 498
11.7 Stack Unwinding and Obtaining Information from an Exception Object......Page 503
11.8 Chained Exceptions......Page 505
11.9 Declaring New Exception Types......Page 508
11.11 Assertions......Page 509
11.13 Wrap-Up......Page 511
12 GUI Components: Part 1......Page 517
12.1 Introduction......Page 518
12.2 Java’s Nimbus Look-and-Feel......Page 519
12.3 Simple GUI-Based Input/Output with JOptionPane......Page 520
12.4 Overview of Swing Components......Page 523
12.5 Displaying Text and Images in a Window......Page 525
12.6 Text Fields and an Introduction to Event Handling with Nested Classes......Page 529
12.7 Common GUI Event Types and Listener Interfaces......Page 535
12.8 How Event Handling Works......Page 537
12.9 JButton......Page 539
12.10 Buttons That Maintain State......Page 542
12.10.1 JCheckBox......Page 543
12.10.2 JRadioButton......Page 545
12.11 JComboBox; Using an Anonymous Inner Class for Event Handling......Page 548
12.12 JList......Page 552
12.13 Multiple-Selection Lists......Page 555
12.14 Mouse Event Handling......Page 557
12.15 Adapter Classes......Page 562
12.16 JPanel Subclass for Drawing with the Mouse......Page 566
12.17 Key Event Handling......Page 569
12.18 Introduction to Layout Managers......Page 572
12.18.1 FlowLayout......Page 574
12.18.2 BorderLayout......Page 576
12.18.3 GridLayout......Page 580
12.19 Using Panels to Manage More Complex Layouts......Page 582
12.20 JTextArea......Page 583
12.21 Wrap-Up......Page 586
13 Graphics and Java 2D......Page 599
13.1 Introduction......Page 600
13.2 Graphics Contexts and Graphics Objects......Page 602
13.3 Color Control......Page 603
13.4 Manipulating Fonts......Page 610
13.5 Drawing Lines, Rectangles and Ovals......Page 615
13.6 Drawing Arcs......Page 619
13.7 Drawing Polygons and Polylines......Page 622
13.8 Java 2D API......Page 625
13.9 Wrap-Up......Page 632
14 Strings, Characters and Regular Expressions......Page 640
14.2 Fundamentals of Characters and Strings......Page 641
14.3.1 String Constructors......Page 642
14.3.2 String Methods length, charAt and getChars......Page 643
14.3.3 Comparing Strings......Page 644
14.3.4 Locating Characters and Substrings in Strings......Page 649
14.3.5 Extracting Substrings from Strings......Page 651
14.3.7 Miscellaneous String Methods......Page 652
14.3.8 String Method valueOf......Page 654
14.4 Class StringBuilder......Page 655
14.4.2 StringBuilder Methods length, capacity, setLength and ensureCapacity......Page 656
14.4.3 StringBuilder Methods charAt, setCharAt, getChars and reverse......Page 658
14.4.4 StringBuilder append Methods......Page 659
14.4.5 StringBuilder Insertion and Deletion Methods......Page 661
14.5 Class Character......Page 662
14.6 Tokenizing Strings......Page 667
14.7 Regular Expressions, Class Pattern and ClassMatcher......Page 668
14.8 Wrap-Up......Page 677
15 Files, Streams and Object Serialization......Page 688
15.2 Files and Streams......Page 689
15.3 Using NIO Classes and Interfaces to Get File and Directory Information......Page 691
15.4.1 Creating a Sequential-Access Text File......Page 695
15.4.2 Reading Data from a Sequential-Access Text File......Page 699
15.4.3 Case Study: A Credit-Inquiry Program......Page 701
15.4.4 Updating Sequential-Access Files......Page 705
15.5 Object Serialization......Page 706
15.5.1 Creating a Sequential-Access File Using Object Serialization......Page 707
15.5.2 Reading and Deserializing Data from a Sequential-Access File......Page 712
15.6 Opening Files with JFileChooser......Page 714
15.7.1 Interfaces and Classes for Byte-Based Input and Output......Page 717
15.7.2 Interfaces and Classes for Character-Based Input and Output......Page 719
15.8 Wrap-Up......Page 720
16 Generic Collections......Page 728
16.2 Collections Overview......Page 729
16.5 Interface Collection and Class Collections......Page 731
16.6 Lists......Page 732
16.6.1 ArrayList and Iterator......Page 733
16.6.2 LinkedList......Page 735
16.7 Collections Methods......Page 740
16.7.1 Method sort......Page 741
16.7.2 Method shuffle......Page 744
16.7.3 Methods reverse, fill, copy, max and min......Page 746
16.7.4 Method binarySearch......Page 748
16.7.5 Methods addAll, frequency and disjoint......Page 750
16.8 Stack Class of Package java.util......Page 752
16.9 Class PriorityQueue and Interface Queue......Page 754
16.10 Sets......Page 755
16.11 Maps......Page 758
16.12 Properties Class......Page 762
16.14 Unmodifiable Collections......Page 765
16.16 Wrap-Up......Page 766
17 Java SE 8 Lambdas and Streams......Page 773
17.1 Introduction......Page 774
17.2 Functional Programming Technologies Overview......Page 775
17.2.1 Functional Interfaces......Page 776
17.2.2 Lambda Expressions......Page 777
17.2.3 Streams......Page 778
17.3 IntStream Operations......Page 780
17.3.1 Creating an IntStream and Displaying Its Values with theforEach Terminal Operation......Page 782
17.3.3 Terminal Operation reduce......Page 783
17.3.4 Intermediate Operations: Filtering and Sorting IntStreamValues......Page 785
17.3.5 Intermediate Operation: Mapping......Page 786
17.4 Stream Manipulations......Page 787
17.4.1 Creating a Stream......Page 788
17.4.5 Sorting Previously Collected Results......Page 789
17.5 Stream Manipulations......Page 790
17.5.1 Mapping Strings to Uppercase Using a Method Reference......Page 791
17.6 Stream Manipulations......Page 792
17.6.1 Creating and Displaying a List......Page 794
17.6.2 Filtering Employees with Salaries in a Specified Range......Page 795
17.6.3 Sorting Employees By Multiple Fields......Page 796
17.6.4 Mapping Employees to Unique Last Name Strings......Page 798
17.6.5 Grouping Employees By Department......Page 799
17.6.7 Summing and Averaging Employee Salaries......Page 800
17.7 Creating a Stream from a File......Page 802
17.8 Generating Streams of Random Values......Page 805
17.10 Additional Notes on Java SE 8 Interfaces......Page 807
17.12 Wrap-Up......Page 808
18Recursion......Page 820
18.1 Introduction......Page 821
18.2 Recursion Concepts......Page 822
18.3 Example Using Recursion: Factorials......Page 823
18.4 Reimplementing Class FactorialCalculatorUsing Class BigInteger......Page 825
18.5 Example Using Recursion: Fibonacci Series......Page 827
18.6 Recursion and the Method-Call Stack......Page 830
18.7 Recursion vs. Iteration......Page 831
18.8 Towers of Hanoi......Page 833
18.9.1 Koch Curve Fractal......Page 835
18.9.2 (Optional) Case Study: Lo Feather Fractal......Page 836
18.10 Recursive Backtracking......Page 845
18.11 Wrap-Up......Page 846
19 Searching, Sorting and Big O......Page 854
19.1 Introduction......Page 855
19.2 Linear Search......Page 856
19.3.1 O(1) Algorithms......Page 858
19.3.3 O(n2) Algorithms......Page 859
19.4 Binary Search......Page 860
19.4.1 Binary Search Implementation......Page 861
19.5 Sorting Algorithms......Page 864
19.6.1 Selection Sort Implementation......Page 865
19.7 Insertion Sort......Page 868
19.7.1 Insertion Sort Implementation......Page 869
19.8 Merge Sort......Page 871
19.8.1 Merge Sort Implementation......Page 872
19.8.2 Efficiency of the Merge Sort......Page 876
19.9 Big O Summary for This Chapter’s Searching and Sorting Algorithms......Page 877
19.10 Wrap-Up......Page 878
20 Generic Classes and Methods......Page 883
20.2 Motivation for Generic Methods......Page 884
20.3 Generic Methods: Implementation and Compile-Time Translation......Page 886
20.4 Additional Compile-Time Translation Issues:Methods That Use a Type Parameter as the Return Type......Page 889
20.5 Overloading Generic Methods......Page 892
20.6 Generic Classes......Page 893
20.7 Raw Types......Page 900
20.8 Wildcards in Methods That Accept TypeParameters......Page 904
20.9 Wrap-Up......Page 908
21 Custom Generic Data Structures......Page 913
21.1 Introduction......Page 914
21.3 Dynamic Memory Allocation......Page 915
21.4.1 Singly Linked Lists......Page 916
21.4.2 Implementing a Generic List Class......Page 917
21.4.5 List Method insertAtFront......Page 922
21.4.6 List Method insertAtBack......Page 923
21.4.7 List Method removeFromFront......Page 924
21.4.8 List Method removeFromBack......Page 925
21.4.10 Creating Your Own Packages......Page 926
21.5 Stacks......Page 930
21.6 Queues......Page 934
21.7 Trees......Page 937
21.8 Wrap-Up......Page 944
22 GUI Components: Part 2......Page 955
22.2 JSlider......Page 956
22.3 Understanding Windows in Java......Page 960
22.4 Using Menus with Frames......Page 961
22.5 JPopupMenu......Page 969
22.6 Pluggable Look-and-Feel......Page 972
22.7 JDesktopPane and JInternalFrame......Page 977
22.8 JTabbedPane......Page 980
22.9 BoxLayout Layout Manager......Page 982
22.10 GridBagLayout Layout Manager......Page 986
22.11 Wrap-Up......Page 996
23 Concurrency......Page 1001
23.1 Introduction......Page 1002
23.2 Thread States and Life Cycle......Page 1004
23.2.5 Terminated State......Page 1005
23.2.7 Thread Priorities and Thread Scheduling......Page 1006
23.3 Creating and Executing Threads with the Executor Framework......Page 1007
23.4 Thread Synchronization......Page 1011
23.4.2 Monitors......Page 1012
23.4.3 Unsynchronized Mutable Data Sharing......Page 1013
23.4.4 Synchronized Mutable Data Sharing—Making Operations Atomic......Page 1018
23.5 Producer/Consumer Relationship without Synchronization......Page 1020
23.6 Producer/Consumer Relationship: ArrayBlockingQueue......Page 1028
23.7 (Advanced) Producer/Consumer Relationship with synchronized,wait, notify and notifyAll......Page 1031
23.8 (Advanced) Producer/Consumer Relationship: Bounded Buffers......Page 1038
23.9 (Advanced) Producer/Consumer Relationship: The Lock and Condition Interfaces......Page 1046
23.10 Concurrent Collections......Page 1053
23.11 Multithreading with GUI: SwingWorker......Page 1055
23.11.1 Performing Computations in a Worker Thread:Fibonacci Numbers......Page 1056
23.11.2 Processing Intermediate Results: Sieve of Eratosthenes......Page 1062
23.12 sort and parallelSort Timings with the Java SE 8 Date/Time API......Page 1069
23.13 Java SE 8: Sequential vs. Parallel Streams......Page 1071
23.14 (Advanced) Interfaces Callable and Future......Page 1074
23.16 Wrap-Up......Page 1078
24 Accessing Databases with JDBC......Page 1089
24.1 Introduction......Page 1090
24.2 Relational Databases......Page 1091
24.3 A books Database......Page 1092
24.4.1 Basic SELECT Query......Page 1096
24.4.2 WHERE Clause......Page 1097
24.4.3 ORDER BY Clause......Page 1099
24.4.4 Merging Data from Multiple Tables: INNER JOIN......Page 1100
24.4.5 INSERT Statement......Page 1102
24.4.6 UPDATE Statement......Page 1103
24.5 Setting up a Java DB Database......Page 1104
24.5.1 Creating the Chapter’s Databases on Windows......Page 1105
24.5.2 Creating the Chapter’s Databases on Mac OS X......Page 1106
24.6.1 Connecting to and Querying a Database......Page 1107
24.6.2 Querying the books Database......Page 1111
24.7 RowSet Interface......Page 1124
24.8 PreparedStatements......Page 1126
24.10 Transaction Processing......Page 1142
24.11 Wrap-Up......Page 1143
25 JavaFX GUI: Part 1......Page 1151
25.1 Introduction......Page 1152
25.2 JavaFX Scene Builder and the NetBeans IDE......Page 1153
25.3 JavaFX App Window Structure......Page 1154
25.4.1 Creating the App’s Project......Page 1155
25.4.2 NetBeans Projects Window—Viewing the Project Contents......Page 1157
25.4.4 Opening JavaFX Scene Builder from NetBeans......Page 1158
25.4.5 Changing to a VBox Layout Container......Page 1159
25.4.8 Adding and Configuring an ImageView......Page 1160
25.4.9 Running the Welcome App......Page 1161
25.5 Tip Calculator App—Introduction to Event Handling......Page 1162
25.5.2 Technologies Overview......Page 1163
25.5.3 Building the App’s GUI......Page 1166
25.5.4 TipCalculator Class......Page 1170
25.5.5 TipCalculatorController Class......Page 1172
25.6 Features Covered in the Online JavaFX Chapters......Page 1177
25.7 Wrap-Up......Page 1178
Chapters on the Web......Page 1185
A Operator Precedence Chart......Page 1187
B ASCII Character Set......Page 1189
C Keywords and Reserved Words......Page 1190
D Primitive Types......Page 1191
E Using the Debugger......Page 1192
E.2 Breakpoints and the run, stop, cont and print Commands......Page 1193
E.3 The print and set Commands......Page 1197
E.4 Controlling Execution Using the step, step up and next Commands......Page 1199
E.5 The watch Command......Page 1202
E.6 The clear Command......Page 1204
E.7 Wrap-Up......Page 1206
Appendices on the Web......Page 1209
A......Page 1211
B......Page 1213
C......Page 1214
D......Page 1218
E......Page 1219
F......Page 1220
G......Page 1221
I......Page 1223
J......Page 1226
L......Page 1228
M......Page 1229
N......Page 1230
O......Page 1231
P......Page 1232
R......Page 1234
S......Page 1235
T......Page 1239
W......Page 1241
Z......Page 1242

Polecaj historie