Absolute Java - Walter Savitch

Absolute Java

United States Edition

Walter Savitch (Autor)

Media-Kombination
1272 Seiten
2009 | 4th edition
Pearson
978-0-13-608382-5 (ISBN)
138,95 inkl. MwSt
  • Titel erscheint in neuer Auflage
  • Artikel merken
For undergraduate students in Computer Science and Computer Programming courses.

 

Praised for providing an engaging balance of thoughtful examples and explanatory discussion, Absolute Java™, Fourth Edition has been updated with numerous additional programming projects and coverage of web and database programming.  Best-selling author Walt Savitch and contributor Kenrick Mock explain concepts and techniques in a straightforward style using understandable language and code enhanced by a suite of pedagogical tools.  This edition also includes an extensive set of VideoNotes.  Absolute Java is appropriate for both introductory and intermediate programming courses introducing Java.

 

SUPPLEMENTS



PowerPoint Slides
Source Code from the book
VideoNotes
MyCodeMate Online Tutorial Service
Instructors Solutions Manual
Test Bank
Text-specific Website

Walter Savitch is Professor Emeritus of Computer Science at the University of California at San Diego. He received his PhD in mathematics from the University of California at Berkeley in 1969. Since that time he has been on the faculty of the University of California in San Diego (UCSD). He served as director of the UCSD Interdisciplinary PhD program in Cognitive Science for over ten years. He has served as a visiting researcher at the Computer Science departments of the University of Washington in Seattle and and at the University of Colorado in Boulder, and has been a visiting scholar at the Centrum voor Wiskunde en Informatica in Amsterdam.

Chapter 1 Getting Started 1
1.1 INTRODUCTION TO JAVA 2
Origins of the Java Language 2
Objects and Methods 3
Applets 4
A Sample Java Application Program 5
Byte-Code and the Java Virtual Machine 8
Class Loader 10
Compiling a Java Program or Class 10
Running a Java Program 11
Tip: Error Messages 12

1.2 EXPRESSIONS AND ASSIGNMENT STATEMENTS 13
Identifiers 13
Variables 15
Assignment Statements 16
Tip: Initialize Variables 18
More Assignment Statements 19
Assignment Compatibility 20
Constants 21
Arithmetic Operators and Expressions 23
Parentheses and Precedence Rules 24
Integer and Floating-Point Division 26
Pitfall: Round-Off Errors in Floating-Point Numbers 27
Pitfall: Division with Whole Numbers 28
Type Casting 29
Increment and Decrement Operators 30

1.3 THE CLASS STRING 33
String Constants and Variables 33
Concatenation of Strings 34
Classes 35
String Methods 37
Escape Sequences 42
String Processing 43
The Unicode Character Set 43

1.4 PROGRAM STYLE 46
Naming Constants 46
Java Spelling Conventions 48
Comments 49
Indenting 50
Chapter Summary 51
Answers to Self-Test Exercises 52
Programming Projects 54

Chapter 2 Console Input and Output 57
2.1 SCREEN OUTPUT 58
System.out.println 58
Tip: Different Approaches to Formatting Output 61
Formatting Output with printf 61
Tip: Formatting Money Amounts with printf 65
Tip: Legacy Code 66
Money Formats Using NumberFormat 67
Importing Packages and Classes 70
The DecimalFormat Class 72

2.2 CONSOLE INPUT USING THE SCANNER CLASS 76
The Scanner Class 76
Pitfall: Dealing with the Line Terminator, '/n' 83
The Empty String 84
Tip: Prompt for Input 84
Tip: Echo Input 84
Example: Self-Service Check Out 86
Other Input Delimiters 86
Chapter Summary 89
Answers to Self-Test Exercises 89
Programming Projects 91

Chapter 3 Flow of Control 95
3.1 BRANCHING MECHANISM 96
if-else Statements 96
Omitting the else 97
Compound Statements 98
Tip: Placing of Braces 99
Nested Statements 100
Multiway if-else Statement 100
Example: State Income Tax 101
The switch Statement 103
Pitfall: Forgetting a break in a switch Statement 107
The Conditional Operator 108

3.2 BOOLEAN EXPRESSIONS 109
Simple Boolean Expressions 109
Pitfall: Using = in Place of == 110
Pitfall: Using == with Strings 111
Lexicographic and Alphabetical Order 112
Building Boolean Expressions 115
Pitfall: Strings of Inequalities 116
Evaluating Boolean Expressions 116
Tip: Naming Boolean Variables 119
Short-Circuit and Complete Evaluation 120
Precedence and Associativity Rules 121

3.3 LOOPS 128
while Statement and do-while Statement 128
Algorithms and Pseudocode 130
Example: Averaging a List of Scores 133
The for Statement 134
The Comma in for Statements 137
Tip: Repeat N Times Loops 139
Pitfall: Extra Semicolon in a for Statement 139
Pitfall: Infinite Loops 140
Nested Loops 141
The break and continue Statements144
The exit Statement 145

3.4 DEBUGGING 146
Loop Bugs 146
Tracing Variables 146
General Debugging Techniques 147
Example: Debugging an Input Validation Loop 148
Preventive Coding 152
Assertion Checks 153
Chapter Summary 155
Answers to Self-Test Exercises 156
Programming Projects 161

Chapter 4 Defining Classes I 167
4.1 CLASS DEFINITIONS 168
Instance Variables and Methods 171
More about Methods 174
Tip: Any Method Can Be Used as a void Method 178
Local Variables 180
Blocks 181
Tip: Declaring Variables in a for Statement 182
Parameters of a Primitive Type 182
Pitfall: Use of the Terms “Parameter” and “Argument” 189
Simple Cases with Class Parameters 191
The this Parameter 191
Methods That Return a Boolean Value 193
The Methods equals and toString 196
Recursive Methods 199
Tip: Testing Methods 199

4.2 INFORMATION HIDING AND ENCAPSULATION 201
public and private Modifiers 202
Example: Yet Another Date Class 203
Accessor and Mutator Methods 204
Tip: A Class Has Access to Private Members of All Objects of the Class 209
Tip: Mutator Methods Can Return a Boolean Value 210
Preconditions and Postconditions 211

4.3 OVERLOADING 212
Rules for Overloading 212
Pitfall: Overloading and Automatic Type Conversion 216
Pitfall: You Cannot Overload Based on the Type Returned 218

4.4 CONSTRUCTORS 220
Constructor Definitions 220
Tip: You Can Invoke Another Method in a Constructor 228
Tip: A Constructor Has a this Parameter 228
Tip: Include a No-Argument Constructor 229
Example: The Final Date Class 230
Default Variable Initializations 231
An Alternative Way to Initialize Instance Variables 231
Example: A Pet Record Class 232
The StringTokenizer Class 236
Chapter Summary 241
Answers to Self-Test Exercises 242
Programming Projects 247

Chapter 5 Defining Classes II 253
5.1 STATIC METHODS AND STATIC VARIABLES 255
Static Methods 255
Pitfall: Invoking a Nonstatic Method Within a Static Method 257
Tip: You Can Put a main in Any Class 258
Static Variables 262
The Math Class 267
Wrapper Classes 271
Automatic Boxing and Unboxing 272
Static Methods in Wrapper Classes 274
Pitfall: A Wrapper Class Does Not Have a No-Argument Constructor 277

5.2 REFERENCES AND CLASS PARAMETERS 278
Variables and Memory 279
References 280
Class Parameters 285
Pitfall: Use of = and == with Variables of a Class Type 289
The Constant null 291
Pitfall: Null Pointer Exception 292
The new Operator and Anonymous Objects 292
Example: Another Approach to Keyboard Input 293
Tip: Use Static Imports 295

5.3 USING AND MISUSING REFERENCES 297
Example: A Person Class 298
Pitfall: null Can Be an Argument to a Method 303
Copy Constructors 307
Pitfall: Privacy Leaks 309
Tip: Deep Copy versus Shallow Copy 315
Mutable and Immutable Classes 313
Tip: Assume Your Coworkers Are Malicious 316

5.4 PACKAGES AND JAVADOC 316
Packages and import Statements 317
The Package java.lang 318
Package Names and Directories 318
Pitfall: Subdirectories Are Not Automatically Imported 321
The Default Package 321
Pitfall: Not Including the Current Directory in Your Class Path 322
Specifying a Class Path When You Compile 322
Name Clashes 323
Introduction to javadoc 324
Commenting Classes for javadoc 324
Running javadoc 326
Chapter Summary 328
Answers to Self-Test Exercises 329
Programming Projects 333

Chapter 6 Arrays 339
6.1 INTRODUCTION TO ARRAYS 340
Creating and Accessing Arrays 341
The length Instance Variable 344
Tip: Use for Loops with Arrays 346
Pitfall: Array Indices Always Start with Zero 346
Pitfall: Array Index Out of Bounds 346
Initializing Arrays 347
Pitfall: An Array of Characters Is Not a String 349

6.2 ARRAYS AND REFERENCES 350
Arrays Are Objects 350
Pitfall: Arrays with a Class Base Type 352
Array Parameters 352
Pitfall: Use of = and == with Arrays 354
Arguments for the Method main 359
Methods that Return an Array 361

6.3 PROGRAMMING WITH ARRAYS 362
Partially Filled Arrays 363
Example: A Class for Partially Filled Arrays 366
Tip: Accessor Methods Need Not Simply Return Instance Variables 370
The “for-each” Loop 370
Methods with a Variable Number of Parameters 374
Example: A String Processing Example 377
Privacy Leaks with Array Instance Variables 378
Example: Sorting an Array 382
Enumerated Types 386
Tip: Enumerated Types in switch Statements 391

6.4 MULTIDIMENSIONAL ARRAYS 393
Multidimensional Array Basics 393
Using the length Instance Variable 396
Ragged Arrays 397
Multidimensional Array Parameters and Returned Values 397
Example: A Grade Book Class 398
Chapter Summary 404
Answers to Self-Test Exercises 405
Programming Projects 412

Chapter 7 Inheritance 419
7.1 INHERITANCE BASICS 420
Derived Classes 421
Overriding a Method Definition 431
Changing the Return Type of an Overridden Method 431
Changing the Access Permission of an Overridden Method 432
Pitfall: Overriding versus Overloading 433
The super Constructor 434
The this Constructor 436
Tip: An Object of a Derived Class Has More than One Type 437
Pitfall: The Terms Subclass and Superclass 440
Example: An Enhanced StringTokenizer Class 441
7.2 ENCAPSULATION AND INHERITANCE 444
Pitfall: Use of Private Instance Variables from the Base Class 444
Pitfall: Private Methods Are Effectively Not Inherited 446
Protected and Package Access 446
Pitfall: Forgetting about the Default Package 448
Pitfall: A Restriction on Protected Access 450
7.3 PROGRAMMING WITH INHERITANCE 452
Tip: Static Variables Are Inherited 452
Tip: “is a” Versus “has a” 453
Access to a Redefined Base Method 453
Pitfall: You Cannot Use Multiple supers 454
The Class Object 455
The Right Way to Define equals 456
Tip: getClass Versus instanceof 459
Chapter Summary 464
Answers to Self-Test Exercises 465
Programming Projects 468

Chapter 8 Polymorphism and Abstract Classes 473
8.1 POLYMORPHISM 474
Late Binding 475
The final Modifier 477
Example: Sales Records 478
Late Binding with toString 485
Pitfall: No Late Binding for Static Methods 486
Downcasting and Upcasting 487
Pitfall: Downcasting 491
Tip: Checking to See Whether Downcasting Is Legitimate 491
A First Look at the clone Method 494
Pitfall: Sometimes the clone Method Return Type Is Object 495
Pitfall: Limitations of Copy Constructors 496

8.2 ABSTRACT CLASSES 500
Abstract Classes 500
Pitfall: You Cannot Create Instances of an Abstract Class 505
Tip: An Abstract Class Is a Type 505
Chapter Summary 507
Answers to Self-Test Exercises 507
Programming Projects 509

Chapter 9 Exception Handling 513
9.1 EXCEPTION HANDLING BASICS 515
Example: A Toy Example of Exception Handling 515
try-throw-catch Mechanism 520
Exception Classes 524
Exception Classes from Standard Packages 525
Defining Exception Classes 527
Tip: Preserve getMessage 531
Tip: An Exception Class Can Carry a Message of Any Type 533
Multiple catch Blocks 538
Pitfall: Catch the More Specific Exception First 540

9.2 THROWING EXCEPTIONS IN METHODS 542
Throwing an Exception in a Method 543
Declaring Exceptions in a throws Clause 545
Exceptions to the Catch or Declare Rule 547v throws Clause in Derived Classes 549
When to Use Exceptions 549
Event-Driven Programming 551

9.3 MORE PROGRAMMING TECHNIQUES
FOR EXCEPTION HANDLING 552
Pitfall: Nested try-catch Blocks 553
The finally Block 553
Rethrowing an Exception 555
The AssertionError Class 555
Exception Handling with the Scanner Class 555
Tip: Exception Controlled Loops 556
ArrayIndexOutOfBoundsException 558
Chapter Summary 558
Answers to Self-Test Exercises 559
Programming Projects 563

Chapter 10 File I/O 567
10.1 INTRODUCTION TO FILE I/O 568
Streams 569
Text Files and Binary Files 569
10.2 TEXT FILES 570
Writing to a Text File 570
Pitfall: A try Block Is a Block 576
Pitfall: Overwriting an Output File 577
Appending to a Text File 577
Tip: toString Helps with Text File Output 578
Reading from a Text File 580
Reading a Text File Using Scanner 580
Testing for the End of a Text File with Scanner 583
Reading a Text File Using BufferedReader 590
Tip: Reading Numbers with BufferedReader 594
Testing for the End of a Text File with BufferedReader 594
Path Names 597
Nested Constructor Invocations 598
System.in, System.out, and System.err 598

10.3 THE FILE CLASS 600
Programming with the File Class 601

10.4 BINARY FILES 604
Writing Simple Data to a Binary File 605
UTF and writeUTF 609
Reading Simple Data from a Binary File 610
Checking for the End of a Binary File 615
Pitfall: Checking for the End of a File in the Wrong Way 617
Binary I/O of Objects 618
The Serializable Interface 618
Pitfall: Mixing Class Types in the Same File 621
Array Objects in Binary Files 622

10.5 RANDOM ACCESS TO BINARY FILES 624
Reading and Writing to the Same File 624
Pitfall: A RandomAccessFile Need Not Start Empty 630
Chapter Summary 630
Answers to Self-Test Exercises 631
Programming Projects 635

Chapter 11 Recursion 639
11.1 RECURSIVE VOID METHODS 641
Example: Vertical Numbers 641
Tracing a Recursive Call 644
A Closer Look at Recursion 647
Pitfall: Infinite Recursion 649
Stacks for Recursion 650
Pitfall: Stack Overflow 652
Recursion versus Iteration 652

11.2 RECURSIVE METHODS THAT RETURN A VALUE 653
General Form for a Recursive Method That Returns a Value 653
Example: Another Powers Method 654

11.3 THINKING RECURSIVELY 659
Recursive Design Techniques 659
Binary Search 650
Efficiency of Binary Search 666
Chapter Summary 668
Answers to Self-Test Exercises 669
Programming Projects 672

Chapter 12 UML and Patterns 677
12.1 UML 678
History of UML 679
UML Class Diagrams 679
Class Interactions 680
Inheritance Diagrams 680
More UML 682

12.2 PATTERNS 683
Adaptor Pattern 683
The Model-View-Controller Pattern 684
Example: A Sorting Pattern 685
Restrictions on the Sorting Pattern 691
Efficiency of the Sorting Pattern 691
Tip: Pragmatics and Patterns 692
Pattern Formalism 692
Chapter Summary 693
Answers to Self-Test Exercises 693
Programming Projects 695

Chapter 13 Interfaces and Inner Classes 699
13.1 INTERFACES 701
Interfaces 701
Abstract Classes Implementing Interfaces 703
Derived Interfaces 703
Pitfall: Interface Semantics Are Not Enforced 705
The Comparable Interface 707
Example: Using the Comparable Interface 708
Defined Constants in Interfaces 713
Pitfall: Inconsistent Interfaces 714
The Serializable Interface 717
The Cloneable Interface 717
13.2 SIMPLE USES OF INNER CLASSES 722
Helping Classes 722
Tip: Inner and Outer Classes Have Access to Each Other’s Private Members 723
Example: A Bank Account Class 723
The .class File for an Inner Class 727
Pitfall: Other Uses of Inner Classes 728

13.3 MORE ABOUT INNER CLASSES 728
Static Inner Classes 728
Public Inner Classes 729
Tip: Referring to a Method of the Outer Class 731
Nesting Inner Classes 733
Inner Classes and Inheritance 733
Anonymous Classes 734
Tip: Why Use Inner Classes? 736v Chapter Summary 737
Answers to Self-Test Exercises 738
Programming Projects 742

Chapter 14 Generics and the ArrayList Class 747
14.1 THE ARRAYLIST CLASS 749
Using the ArrayList Class 750
Tip: Summary of Adding to an ArrayList 754
Methods in the Class ArrayList 755
The “for-each” Loop 759
Example: Golf Scores 762
Tip: Use trimToSize to Save Memory 765
Pitfall: The clone Method Makes a Shallow Copy 765
The Vector Class 766
Parameterized Classes and Generics 767
Pitfall: Nonparameterized ArrayList and Vector Classes 767

14.2 GENERICS 767
Generic Basics 767
Tip: Compile with the -Xlint Option 769
Example: A Generic Class for Ordered Pairs 770
Pitfall: A Generic Constructor Name Has No Type Parameter 773
Pitfall: You Cannot Plug in a Primitive Type for a Type Parameter 774
Pitfall: A Type Parameter Cannot Be Used Everywhere a Type Name Can Be Used 774
Pitfall: An Instantiation of a Generic Class Cannot be an Array Base Type 774
Tip: A Class Definition Can Have More Than One Type Parameter 775
Pitfall: A Generic Class Cannot Be an Exception Class 777
Bounds for Type Parameters 778
Tip: Generic Interfaces 780
Generic Methods 780
Inheritance with Generic Classes 782
Chapter Summary 784
Answers to Self-Test Exercises 785
Programming Projects 787

Chapter 15 Linked Data Structures 791
15.1 JAVA LINKED LISTS 794
Example: A Simple Linked List Class 794
Working with Linked Lists 798
Pitfall: Privacy Leaks 803
Node Inner Classes 804v Example: A Generic Linked List 807
Pitfall: Using Node Instead of Node 812
The equals Method for Linked Lists 813

15.2 COPY CONSTRUCTORS AND THE CLONE METHOD 814
Simple Copy Constructors and clone Methods 815
Exceptions 815
Pitfall: The clone Method Is Protected in Object 818
Tip: Use a Type Parameter Bound for a Better clone 819
Example: A Linked List with a Deep Copy clone Method 822
Tip: Cloning Is an “All or Nothing” Affair 826

15.3 ITERATORS 826
Defining an Iterator Class 827
Adding and Deleting Nodes 832

15.4 VARIATIONS ON A LINKED LIST 835
Doubly Linked List 837
The Stack Data Structure 846
The Queue Data Structure 848
Running Times and Big-O Notation 851
Efficiency of Linked Lists 856

15.5 HASH TABLES WITH CHAINING 857
A Hash Function for Strings 858
Efficiency of Hash Tables 862

15.6 SETS 863
Fundamental Set Operations 863
Efficiency of Sets Using Linked Lists 868

15.7 TREES 869
Tree Properties 870
Example: A Binary Search Tree Class 872
Efficiency of Binary Search Trees 878
Chapter Summary 879
Answers to Self-Test Exercises 880
Programming Projects 885

Chapter 16 Collections, Maps and Iterators 889
16.1 COLLECTIONS 890
Wildcards 892
The Collection Framework 892
Pitfall: Optional Operations 898
Tip: Dealing with All Those Exceptions 899
Concrete Collection Classes 900
Differences Between ArrayList and Vector 909
Nonparameterized Version of the Collection Framework 909
Pitfall: Omitting the 910

16.2 MAPS 910
Concrete Map Classes 913

16.3 ITERATORS 917
The Iterator Concept 917
The Iterator Interface 917
Tip: For-Each Loops as Iterators 920
List Iterators 921
Pitfall: next Can Return a Reference 923
Tip: Defining Your Own Iterator Classes 925
Chapter Summary 926
Answers to Self-Test Exercises 926
Programming Projects 927

Chapter 17 Swing I 933
17.1 EVENT-DRIVEN PROGRAMMING 935
Events and Listeners 935

17.2 BUTTONS, EVENTS, AND OTHER SWING BASICS 936
Example: A Simple Window 937
Pitfall: Forgetting to Program the Close-Window Button 942
Buttons 943
Action Listeners and Action Events 944
Pitfall: Changing the Heading for actionPerformed 946
Tip: Ending a Swing Program 947
Example: A Better Version of Our First Swing GUI 947
Labels 951
Color 952
Example: A GUI with a Label and Color 953

17.3 CONTAINERS AND LAYOUT MANAGERS 956
Border Layout Managers 956
Flow Layout Managers 959
Grid Layout Managers 961
Panels 965
Example: A Tricolor Built with Panels 966
The Container Class 970
Tip: Code a GUI’s Look and Actions Separately 973
The Model-View-Controller Pattern 974

17.4 MENUS AND BUTTONS 975
Example: A GUI with a Menu 975
Menu Bars, Menus, and Menu Items 975
Nested Menus 980
The AbstractButton Class 980
The setActionCommand Method 983
Listeners as Inner Classes 985

17.5 TEXT FIELDS AND TEXT AREAS 988
Text Areas and Text Fields 988
Tip: Labeling a Text Field 994
Tip: Inputting and Outputting Numbers 995
A Swing Calculator 995
Chapter Summary 1001
Answers to Self-Test Exercises 1002
Programming Projects 1009

Chapter 18 Swing II 1011
18.1 WINDOW LISTENERS 1012
Example: A Window Listener Inner Class 1014
The dispose Method 1018
Pitfall: Forgetting to Invoke setDefaultCloseOperation 1018
The WindowAdapter Class 1019

18.2 ICONS AND SCROLL BARS 1020
Icons 1020
Scroll Bars 1027
Example: Components with Changing Visibility 1033

18.3 THE GRAPHICS CLASS 1036
Coordinate System for Graphics Objects 1037
The Method paint and the Class Graphics 1037
Drawing Ovals 1043
Drawing Arcs 1043
Rounded Rectangles 1047
paintComponent for Panels 1048
Action Drawings and repaint 1048
Some More Details on Updating a GUI 1054

18.4 COLORS 1054
Specifying a Drawing Color 1055
Defining Colors 1056
Pitfall: Using doubles to Define a Color 1057
The JColorChooser Dialog Window 1058

18.5 FONTS AND THE DRAWSTRING METHOD 1061
The drawString Method 1061
Fonts 1064
Chapter Summary 1067
Answers to Self-Test Exercises 1068
Programming Projects 1071

Chapter 19 Java Never Ends 1077
19.1 MULTITHREADING 1078
Example: A Nonresponsive GUI 1079
Thread.sleep 1079
The getGraphics Method 1083
Fixing a Nonresponsive Program Using Threads 1084
Example: A Multithreaded Program 1084
The Class Thread 1085
The Runnable Interface 1088

19.2 NETWORKING WITH STREAM SOCKETS 1091
Sockets 1091
Sockets and Threading 1095

19.3 JAVABEANS 1096
The Component Model 1096
The JavaBeans Model 1096

19.4 JAVA AND DATABASE CONNECTIONS 1098
Relational Databases 1098
Java DB and JDBC 1098
SQL 1100

19.5 WEB PROGRAMMING WITH JAVA SERVER PAGES 1110
Applets, Servlets, and Java Server Pages 1110
Sun GlassFish Enterprise Server 1112
HTML Forms–the Common Gateway Interface 1113
JSP Declarations, Expressions, Scriptlets, and Directives 1115
Chapter Summary 1124
Answers to Self-Test Exercises 1124
Programming Projects 1126

Chapter 20 Applets and HTML (online at www.aw.com/savitch )

Appendix 1 Keywords 1131
Appendix 2 Precedence and Associativity Rules 1133
Appendix 3 Unicode Character Set 1135
Appendix 4 Format Specifications for printf 1137
Appendix 5 Summary of Classes and Interfaces 1139
Index 1207

Erscheint lt. Verlag 22.6.2009
Sprache englisch
Maße 187 x 232 mm
Gewicht 1840 g
Themenwelt Informatik Programmiersprachen / -werkzeuge Java
ISBN-10 0-13-608382-X / 013608382X
ISBN-13 978-0-13-608382-5 / 9780136083825
Zustand Neuware
Haben Sie eine Frage zum Produkt?
Mehr entdecken
aus dem Bereich