Starting Out with Java: From Control Structures through Data Structures is designed to be used in a 2 or 3 semester/quarter sequence for beginning programmers. Tony Gaddis emphasizes problem-solving and program design by teaching the Java programming language through a step-by-step detailed presentation. He introduces procedural programming early and covers control structures and methods before objects. Students are engaged and have plenty of opportunity to practice using programming concepts through practical tools that include end-of-section and chapter exercises, case studies and programming projects.

Les mer

Preface xvii

Chapter 1 Java Fundamentals 27

2.1 The Parts of a Java Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

2.2 The print and println Methods, and the Java API . . . . . . . . . . . . 33

2.3 Variables and Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

2.4 Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

2.5 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

2.6 Combined Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

2.7 Conversion between Primitive Data Types . . . . . . . . . . . . . . . . . . . . . 64

2.8 Creating Named Constants with final . . . . . . . . . . . . . . . . . . . . . . 68

2.9 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

2.10 Scope. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

2.11 Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

2.12 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

2.13 Reading Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

2.14 Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

2.15 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

Review Questions and Exercises 100

Programming Challenges 105

Chapter 2 Decision Structures 109

3.1 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

3.2 The if-else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

3.3 Nested if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

3.4 The if-else-if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

3.5 Logical Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

3.6 Comparing String Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

3.7 More about Variable Declaration and Scope . . . . . . . . . . . . . . . . . . 149

3.8 The Conditional Operator (Optional) . . . . . . . . . . . . . . . . . . . . . . . . 150

3.9 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

3.10 Creating Objects with the DecimalFormat Class. . . . . . . . . . . . . . 159

3.11 The printf Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

3.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

Review Questions and Exercises 169

Programming Challenges 174

Chapter 3 Loops and Files 179

4.1 The Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . 179

4.2 The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

4.3 Using the while Loop for Input Validation . . . . . . . . . . . . . . . . . . . 190

4.4 The do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

4.5 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

4.6 Running Totals and Sentinel Values . . . . . . . . . . . . . . . . . . . . . . . . . 206

4.7 Nested Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211

4.8 The break and continue Statements (Optional) . . . . . . . . . . . . . 212

4.9 Deciding Which Loop to Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

4.10 Introduction to File Input and Output . . . . . . . . . . . . . . . . . . . . . . . 213

4.11 The Random Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233

4.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

Review Questions and Exercises 236

Programming Challenges 242

Chapter 4 Methods 247

5.1 Introduction to Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

5.2 Passing Arguments to a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 257

5.3 More about Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

5.4 Returning a Value from a Method . . . . . . . . . . . . . . . . . . . . . . . . . . 271

5.5 Problem Solving with Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280

5.6 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284

Review Questions and Exercises 285

Programming Challenges 289

Chapter 5 A First Look at Classes 297

6.1 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297

6.2 Instance Fields and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320

6.3 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325

6.4 Overloading Methods and Constructors. . . . . . . . . . . . . . . . . . . . . . 334

6.5 Scope of Instance Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342

6.6 Packages and import Statements . . . . . . . . . . . . . . . . . . . . . . . . . . 343

6.7 Focus on Object-Oriented Design: Finding the Classes

and Their Responsibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345

6.8 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352

Review Questions and Exercises 352

Programming Challenges 357

Chapter 6 A First Look at GUI Applications 363

7.1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363

7.2 Creating Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366

7.3 Equipping GUI Classes with a main Method . . . . . . . . . . . . . . . . . . 394

7.4 Layout Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396

7.5 Radio Buttons and Check Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . 412

7.6 Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423

7.7 Focus on Problem Solving: Extending Classes from JPanel . . . . . . 426

7.8 Splash Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438

7.9 Using Console Output to Debug a GUI Application . . . . . . . . . . . . . 439

7.10 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444

Review Questions and Exercises 444

Programming Challenges 448

Chapter 7 Arrays and the ArrayList Class 451

8.1 Introduction to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451

8.2 Processing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461

8.3 Passing Arrays as Arguments to Methods. . . . . . . . . . . . . . . . . . . . . 470

8.4 Some Useful Array Algorithms and Operations. . . . . . . . . . . . . . . . . 474

8.5 Returning Arrays from Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487

8.6 String Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489

8.7 Arrays of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492

8.8 The Sequential Search Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 495

8.9 Two-Dimensional Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498

8.10 Arrays with Three or More Dimensions . . . . . . . . . . . . . . . . . . . . . . 510

8.11 Command-Line Arguments and Variable-Length Argument Lists . . . 511

8.12 The ArrayList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515

8.13 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523

Review Questions and Exercises 523

Programming Challenges 528

Chapter 8 A Second Look at Classes and Objects 533

9.1 Static Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533

9.2 Passing Objects as Arguments to Methods. . . . . . . . . . . . . . . . . . . . 540

9.3 Returning Objects from Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . 543

9.4 The toString Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545

9.5 Writing an equals Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549

9.6 Methods That Copy Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552

9.7 Aggregation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555

9.8 The this Reference Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568

9.9 Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571

9.10 Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580

9.11 Focus on Object-Oriented Design: Class Collaboration . . . . . . . . . . 582

9.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586

Review Questions and Exercises 587

Programming Challenges 591

Chapter 9 Text Processing and More about Wrapper Classes 597

10.1 Introduction to Wrapper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 597

10.2 Character Testing and Conversion with the Character Class. . . . . 598

10.3 More String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606

10.4 The StringBuilder Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620

10.5 Tokenizing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628

10.6 Wrapper Classes for the Numeric Data Types. . . . . . . . . . . . . . . . . . 636

10.7 Focus on Problem Solving: The TestScoreReader Class. . . . . . . . 639

10.8 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643

Review Questions and Exercises 643

Programming Challenges 647

Chapter 10 Inheritance 653

11.1 What Is Inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653

11.2 Calling the Superclass Constructor . . . . . . . . . . . . . . . . . . . . . . . . . 666

11.3 Overriding Superclass Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674

11.4 Protected Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683

11.5 Chains of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689

11.6 The Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695

11.7 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697

11.8 Abstract Classes and Abstract Methods . . . . . . . . . . . . . . . . . . . . . . 702

11.9 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709

11.10 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721

Review Questions and Exercises 722

Programming Challenges 727

Chapter 11 Exceptions and Advanced File I/O 733

12.1 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733

12.2 Throwing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755

12.3 Advanced Topics: Binary Files, Random Access Files,

and Object Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761

12.4 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778

Review Questions and Exercises 778

Programming Challenges 784

Chapter 12 Advanced GUI Applications 787

13.1 The Swing and AWT Class Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . 787

13.2 Read-Only Text Fields. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788

13.3 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790

13.4 Combo Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806

13.5 Displaying Images in Labels and Buttons . . . . . . . . . . . . . . . . . . . . . 812

13.6 Mnemonics and Tool Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818

13.7 File Choosers and Color Choosers . . . . . . . . . . . . . . . . . . . . . . . . . . 820

13.8 Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824

13.9 More about Text Components: Text Areas and Fonts. . . . . . . . . . . . 833

13.10 Sliders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837

13.11 Look and Feel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842

13.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844

Review Questions and Exercises 845

Programming Challenges 850

Chapter 13 Applets and More 855

14.1 Introduction to Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855

14.2 A Brief Introduction to HTML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857

14.3 Creating Applets with Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 866

14.4 Using AWT for Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875

14.5 Drawing Shapes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880

14.6 Handling Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 901

14.7 Timer Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911

14.8 Playing Audio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915

14.9 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920

Review Questions and Exercises 920

Programming Challenges 927

Chapter 14 Recursion 929

15.1 Introduction to Recursion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929

15.2 Solving Problems with Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 932

15.3 Examples of Recursive Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937

15.4 The Towers of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943

15.5 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947

Review Questions and Exercises 948

Programming Challenges 951

Chapter 15 Stacks and Queues 1219

21.1 Stacks and Their Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1219

21.2 Array Implementation of Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . 1222

21.3 Linked Implementation of Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . 1228

21.4 Queues and Their Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1233

21.5 Array Implementation of Queues . . . . . . . . . . . . . . . . . . . . . . . . . . 1233

21.6 Linked List Implementation of Queues . . . . . . . . . . . . . . . . . . . . . . 1244

21.7 Generic Implementation of Stacks and Queues . . . . . . . . . . . . . . . 1249

21.8 Queues and Breadth-First Search . . . . . . . . . . . . . . . . . . . . . . . . . . 1252

21.9 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1255

Review Questions and Exercises 1256

Programming Challenges 1258

Chapter 16 Binary Trees, AVL Trees, and Priority Queues 1261

22.1 Binary Trees and Their Applications . . . . . . . . . . . . . . . . . . . . . . . . 1261

22.2 Binary Search Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1271

22.3 AVL Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1288

22.4 Priority Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1301

22.5 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1318

Review Questions and Exercises 1318

Programming Challenges 1321

Index 1325

Les mer

Produktdetaljer

ISBN
9780321545862
Publisert
2011-07-05
Utgave
2. utgave
Utgiver
Vendor
Pearson
Vekt
2120 gr
Høyde
254 mm
Bredde
203 mm
Dybde
44 mm
Aldersnivå
05, U
Språk
Product language
Engelsk
Format
Product format
Heftet
Antall sider
1392

Biografisk notat

Tony Gaddis is the principal author of the Starting Out With series of textbooks. Tony has nearly 20 years experience teaching computer science courses at Haywood Community College in North Carolina. He is a highly acclaimed instructor who was previously selected as the North Carolina Community College Teacher of the Year and has received the Teaching Excellence award from the National Institute for Staff and Organizational Development. TheStarting Out With series includes introductory books using the C++ programming language, the Java (TM) programming language, Microsoft (R) Visual Basic (R), Microsoft (R) C# (R), Python, Programming Logic and Design, and Alice, all published by the Addison-Wesley imprint of Pearson.

Godfrey Muganda is an Associate Professor of Computer Science at North Central College. He teaches a wide variety of courses at the undergraduate and graduate levels including courses in Linux and Unix programming, Windows and .NET programming, web application development, web services, data structures, and algorithms. He is a past winner of the North Central College faculty award for outstanding scholarship. His primary research interests are in the area of fuzzy sets and systems.