Starting Out with Java: Early Objects - Tony Gaddis

Starting Out with Java: Early Objects

International Edition

(Autor)

Buch | Softcover
1128 Seiten
2011 | 4th edition
Pearson Education Limited (Verlag)
978-0-273-75122-9 (ISBN)
69,95 inkl. MwSt
zur Neuauflage
  • Titel erscheint in neuer Auflage
  • Artikel merken
Zu diesem Artikel existiert eine Nachauflage
Tony Gaddis's accessible, step-by-step presentation helps beginning students understand the important details necessary to become skilled programmers at an introductory level. Gaddis motivates the study of both programming skills and the Java programming language by presenting all the details needed to understand the "how" and the "why" but never losing sight of the fact that most beginners struggle with this material. His approach is both gradual and highly accessible, ensuring that students understand the logic behind developing high-quality programs.

In Starting Out with Java: Early Objects, Gaddis looks at objects the fundamentals of classes and methods before covering procedural programming. As with all Gaddis texts, clear and easy-to-read code listings, concise and practical real-world examples, and an abundance of exercises appear in every chapter.

Preface xv
Chapter 1 Introduction to Computers and Java 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Why Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Computer Systems: Hardware and Software . . . . . . . . . . . . . . . . . . . . . 2
1.4 Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5 What Is a Program Made of? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.6 The Programming Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.7 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Review Questions and Exercises 24
Programming Challenge 28


Chapter 2 Java Fundamentals 31
2.1 The Parts of a Java Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.2 The print and printIn Methods, and the Java API . . . . . . . . . . . . . . 37
2.3 Variables and Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.4 Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.5 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2.6 Combined Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
2.7 Conversion between Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . 70
2.8 Creating Named Constants with final . . . . . . . . . . . . . . . . . . . . . . . . 74
2.9 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
2.10 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
2.11 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
2.12 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
2.13 Reading Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
2.14 Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
2.15 The printf Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
2.16 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Review Questions and Exercises 109
Programming Challenges 113

Chapter 3 A First Look at Classes and Objects 119
3.1 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
3.2 More about Passing Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
3.3 Instance Fields and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
3.4 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
3.5 A Bank Account Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
3.6 Classes, Variables, and Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
3.7 Packages and import Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
3.8 Focus on Object-Oriented Design: Finding the Classes and Their Responsibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
3.9 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Review Questions and Exercises 173
Programming Challenges 177


Chapter 4 Decision Structures 181
4.1 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
4.2 The if-else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
4.3 The Payroll Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
4.4 Nested i f Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
4.5 The if-else-if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
4.6 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
4.7 Comparing String Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
4.8 More about Variable Declaration and Scope . . . . . . . . . . . . . . . . . . . 223
4.9 The Conditional Operator (Optional) . . . . . . . . . . . . . . . . . . . . . . . . . 225
4.10 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
4.11 Formatting Numbers with the Decimal Format Class. . . . . . . . . . . . 236
4.12 Focus on Problem Solving: The Sales Commission Class . . . . . . . . . . 240
4.13 The Random Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
4.14 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Review Questions and Exercises 251
Programming Challenges 256


Chapter 5 Loops and Files 263
5.1 The Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . 263
5.2 The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
5.3 Using the while Loop for Input Validation . . . . . . . . . . . . . . . . . . . . . 274
5.4 The do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
5.5 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
5.6 Running Totals and Sentinel Values . . . . . . . . . . . . . . . . . . . . . . . . . . 291
5.7 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
5.8 The break and continue Statements . . . . . . . . . . . . . . . . . . . . . . . . 299
5.9 Deciding Which Loop to Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
5.10 Introduction to File Input and Output . . . . . . . . . . . . . . . . . . . . . . . . 300
5.11 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Review Questions and Exercises 321
Programming Challenges 326


Chapter 6 A Second Look at Classes and Objects 333
6.1 Static Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
6.2 Overloaded Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
6.3 Overloaded Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
6.4 Passing Objects as Arguments to Methods . . . . . . . . . . . . . . . . . . . . . 352
6.5 Returning Objects from Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
6.6 The toString Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
6.7 Writing an equals Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
6.8 Methods That Copy Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
6.9 Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
6.10 The this Reference Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
6.11 Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
6.12 Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
6.13 Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
6.14 Focus on Object-Oriented Design: Class Collaboration . . . . . . . . . . . 397
6.15 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Review Questions and Exercises 402
Programming Challenges 407


Chapter 7 Arrays and the ArrayList Class 413
7.1 Introduction to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
7.2 Processing Array Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
7.3 Passing Arrays as Arguments to Methods . . . . . . . . . . . . . . . . . . . . . . 436
7.4 Some Useful Array Algorithms and Operations . . . . . . . . . . . . . . . . . . 440
7.5 Returning Arrays from Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
7.6 String Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
7.7 Arrays of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
7.8 The Sequential Search Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
7.9 The Selection Sort and the Binary Search Algorithms . . . . . . . . . . . . . 465
7.10 Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
7.11 Arrays with Three or More Dimensions . . . . . . . . . . . . . . . . . . . . . . . 485
7.12 Command-Line Arguments and Variable-Length Argument Lists . . . . 486
7.13 The ArrayList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
7.14 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Review Questions and Exercises 499
Programming Challenges 503

Chapter 8 Text Processing and Wrapper Classes 511
8.1 Introduction to Wrapper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
8.2 Character Testing and Conversion with the Character Class . . . . . . . 512
8.3 More about String Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
8.4 The StringBuilder Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
8.5 Tokenizing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
8.6 Wrapper Classes for the Numeric Data Types . . . . . . . . . . . . . . . . . . . 551
8.7 Focus on Problem Solving: The Test Score Reader Class . . . . . . . . . . 555
8.8 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
Review Questions and Exercises 559
Programming Challenges 563


Chapter 9 Inheritance 569
9.1 What Is Inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
9.2 Calling the Superclass Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
9.3 Overriding Superclass Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
9.4 Protected Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
9.5 Classes That Inherit from Subclasses. . . . . . . . . . . . . . . . . . . . . . . . . . 603
9.6 The Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
9.7 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
9.8 Abstract Classes and Abstract Methods . . . . . . . . . . . . . . . . . . . . . . . 615
9.9 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
9.10 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
Review Questions and Exercises 633
Programming Challenges 639


Chapter 10 Exceptions and Advanced File I /O 645
10.1 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
10.2 Throwing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
10.3 Advanced Topics: Binary Files, Random Access Files, and Object Serialization . . . . . . . . . . . . . . . . . . 674
10.4 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690
Review Questions and Exercises 691
Programming Challenges 697


Chapter 11 GUI Applications Part 1 701
11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
11.2 Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
11.3 Creating Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
11.4 Equipping GUI Classes with a main Method. . . . . . . . . . . . . . . . . . . . 739
11.5 Layout Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741
11.6 Radio Buttons and Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758
11.7 Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771
11.8 Focus on Problem Solving: Extending the JPanel Class . . . . . . . . . . . 773
11.9 Splash Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
11.10 Using Console Output to Debug a GUI Application . . . . . . . . . . . . . . 788
11.11 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 792
Review Questions and Exercises 793
Programming Challenges 798


Chapter 12 GUI Applications Part 2 803
12.1 Read-Only Text Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803
12.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
12.3 Combo Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821
12.4 Displaying Images in Labels and Buttons . . . . . . . . . . . . . . . . . . . . . . 827
12.5 Mnemonics and Tool Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833
12.6 File Choosers and Color Choosers . . . . . . . . . . . . . . . . . . . . . . . . . . . 835
12.7 Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839
12.8 More about Text Components: Text Areas and Fonts . . . . . . . . . . . . . 848
12.9 Sliders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853
12.10 Look and Feel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857
12.11 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859
Review Questions and Exercises 860
Programming Challenges 865


Chapter 13 Applets and More 869
13.1 Introduction to Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869
13.2 A Brief Introduction to HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 871
13.3 Creating Applets with Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881
13.4 Using AWT for Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 889
13.5 Drawing Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894
13.6 Handling Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 914
13.7 Timer Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925
13.8 Playing Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929
13.9 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934
Review Questions and Exercises 934
Programming Challenges 940


Chapter 14 Recursion 943
14.1 Introduction to Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943
14.2 Solving Problems with Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946
14.3 Examples of Recursive Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 950
14.4 A Recursive Binary Search Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 957
14.5 The Towers of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960
14.6 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964
Review Questions and Exercises 965
Programming Challenges 968

Chapter 15 Databases 971
15.1 Introduction to Database Management Systems . . . . . . . . . . . . . . . . 971
15.2 Tables, Rows, and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974
15.3 The SQL SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977
15.4 Introduction to JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986
15.5 Inserting Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994
15.6 Updating and Deleting Existing Rows . . . . . . . . . . . . . . . . . . . . . . . . 996
15.7 Creating and Deleting Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 998
15.8 Creating a New Database with Java DB . . . . . . . . . . . . . . . . . . . . . . 1001
15.9 Scrollable Result Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003
15.10 Result Set Meta Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1005
15.11 Displaying Query Results in a J Table . . . . . . . . . . . . . . . . . . . . . . . 1008
15.12 Relational Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1019
15.13 Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1040
15.14 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1042
Review Questions and Exercises 1042
Programming Challenges 1047
Appendix A Getting Started with Alice 1049
Index 1077


Available on the book's online resource page at www.pearsonhighered.com/gaddis:
Appendix B The ASCII/Unicode Characters
Appendix C Operator Precedence and Associativity
Appendix D Java Key Words
Appendix E Installing the JDK and JDK Documentation
Appendix F Using the javadoc Utility
Appendix G More about the Math Class
Appendix H Packages
Appendix I Working with Records and Random-Access Files
Appendix J Installing Java DB
Appendix K The QuickSort Algorithm
Appendix L Answers to Checkpoints Questions
Appendix M Answers to Odd-Numbered Review Questions
Case Study 1 The Amortization Class
Case Study 2 The PinTester Class
Case Study 3 Parallel Arrays
Case Study 4 The Serial Number Class
Case Study 5 A Simple Text Editor Application

Erscheint lt. Verlag 7.3.2011
Verlagsort Harlow
Sprache englisch
Maße 203 x 258 mm
Gewicht 1720 g
Themenwelt Informatik Programmiersprachen / -werkzeuge Java
Mathematik / Informatik Informatik Web / Internet
ISBN-10 0-273-75122-0 / 0273751220
ISBN-13 978-0-273-75122-9 / 9780273751229
Zustand Neuware
Haben Sie eine Frage zum Produkt?
Mehr entdecken
aus dem Bereich
mit über 150 Workouts in Java und Python

von Luigi Lo Iacono; Stephan Wiefling; Michael Schneider

Buch (2023)
Carl Hanser (Verlag)
29,99
Einführung, Ausbildung, Praxis

von Christian Ullenboom

Buch | Hardcover (2023)
Rheinwerk (Verlag)
49,90