Product Cover Image

Starting Out with Java: From Control Structures through Objects, CourseSmart eTextbook, 5th Edition

By Tony Gaddis

Published by Addison-Wesley

Published Date: Mar 6, 2012

More Product Info

Description

In Starting Out with Java: From Control Structures through Objects, Gaddis covers procedural programming–control structures and methods–before introducing object-oriented 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.

MyProgrammingLab, Pearson's new online homework and assessment tool, is available with this edition.

Table of Contents

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 21
Programming Challenge 25

Chapter 2 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 . . . . . . . . . . . . . . . . . . . . . 65
2.8 Creating Named Constants with final . . . . . . . . . . . . . . . . . . . . . . . . 69
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 3 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 The System.out.printf Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
3.11 Creating Objects with the DecimalFormat Class . . . . . . . . . . . . . . . . . 174
3.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Review Questions and Exercises 182
Programming Challenges 187

Chapter 4 Loops and Files 193
4.1 The Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . 193
4.2 The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
4.3 Using the while Loop for Input Validation . . . . . . . . . . . . . . . . . . . . 204
4.4 The do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
4.5 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
4.6 Running Totals and Sentinel Values . . . . . . . . . . . . . . . . . . . . . . . . . . 220
4.7 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
4.8 The break and continue Statements (Optional) . . . . . . . . . . . . . . . 233
4.9 Deciding Which Loop to Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
4.10 Introduction to File Input and Output . . . . . . . . . . . . . . . . . . . . . . . 234
4.11 Generating Random Numbers with the Random Class . . . . . . . . . . . . 253
4.12 Common Errors to Avoid 259
Review Questions and Exercises 260
Programming Challenges 266

Chapter 5 Methods 273
5.1 Introduction to Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
5.2 Passing Arguments to a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
5.3 More about Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
5.4 Returning a Value from a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
5.5 Problem Solving with Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
5.6 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Review Questions and Exercises 311
Programming Challenges 316

Chapter 6 A First Look at Classes 323
6.1 Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
6.2 Writing a Simple Class, Step by Step . . . . . . . . . . . . . . . . . . . . . . . . 330
6.3 Instance Fields and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
6.4 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
6.5 Passing Objects as Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
6.6 Overloading Methods and Constructors . . . . . . . . . . . . . . . . . . . . . . 376
6.7 Scope of Instance Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
6.8 Packages and import Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
6.9 Focus on Object-Oriented Design: Finding the Classes
and Their Responsibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
6.10 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Review Questions and Exercises 395
Programming Challenges 400

Chapter 7 Arrays and the ArrayList Class 407
7.1 Introduction to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
7.2 Processing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
7.3 Passing Arrays As Arguments to Methods . . . . . . . . . . . . . . . . . . . . . 426
7.4 Some Useful Array Algorithms and Operations . . . . . . . . . . . . . . . . . 430
7.5 Returning Arrays from Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
7.6 String Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
7.7 Arrays of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
7.8 The Sequential Search Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
7.9 Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
7.10 Arrays with Three or More Dimensions . . . . . . . . . . . . . . . . . . . . . . . 466
7.11 The Selection Sort and the Binary Search Algorithms . . . . . . . . . . . . 467
7.12 Command-Line Arguments and Variable-Length Argument Lists . . . 472
7.13 The ArrayList Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
7.14 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Review Questions and Exercises 485
Programming Challenges 490

Chapter 8 A Second Look at Classes and Objects 497
8.1 Static Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
8.2 Passing Objects As Arguments to Methods . . . . . . . . . . . . . . . . . . . . 504
8.3 Returning Objects from Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
8.4 The toString Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
8.5 Writing an equals Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
8.6 Methods That Copy Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
8.7 Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
8.8 The this Reference Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
8.9 Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
8.10 Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
8.11 Focus on Object-Oriented Design: Class Collaboration . . . . . . . . . . . 546
8.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
Review Questions and Exercises 551
Programming Challenges 555

Chapter 9 Text Processing and More about Wrapper Classes 561
9.1 Introduction to Wrapper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
9.2 Character Testing and Conversion with the Character Class . . . . . . . 562
9.3 More String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
9.4 The StringBuilder Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
9.5 Tokenizing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
9.6 Wrapper Classes for the Numeric Data Types . . . . . . . . . . . . . . . . . . 603
9.7 Focus on Problem Solving: The TestScoreReader Class . . . . . . . . . . . 606
9.8 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
Review Questions and Exercises 610
Programming Challenges 614

Chapter 10 Inheritance 619
10.1 What Is Inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
10.2 Calling the Superclass Constructor . . . . . . . . . . . . . . . . . . . . . . . . . 632
10.3 Overriding Superclass Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640
10.4 Protected Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
10.5 Chains of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
10.6 The Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
10.7 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
10.8 Abstract Classes and Abstract Methods . . . . . . . . . . . . . . . . . . . . . . 668
10.9 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
10.10 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
Review Questions and Exercises 688
Programming Challenges 693

Chapter 11 Exceptions and Advanced File I/O 699
11.1 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
11.2 Throwing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
11.3 Advanced Topics: Binary Files, Random Access Files,
and Object Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
11.4 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746
Review Questions and Exercises 746
Programming Challenges 752

Chapter 12 A First Look at GUI Applications 755
12.1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
12.2 Creating Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758
12.3 Layout Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
12.4 Radio Buttons and Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
12.5 Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815
12.6 Focus on Problem Solving: Extending Classes from JPanel . . . . 818
12.7 Splash Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
12.8 Using Console Output to Debug a GUI Application . . . . . . . . . . . . . 831
12.9 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836
Review Questions and Exercises 836
Programming Challenges 839

Chapter 13 Advanced GUI Applications 843
13.1 The Swing and AWT Class Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . 843
13.2 Read-Only Text Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844
13.3 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846
13.4 Combo Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862
13.5 Displaying Images in Labels and Buttons . . . . . . . . . . . . . . . . . . . . . 868
13.6 Mnemonics and Tool Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874
13.7 File Choosers and Color Choosers . . . . . . . . . . . . . . . . . . . . . . . . . . 876
13.8 Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880
13.9 More about Text Components: Text Areas and Fonts . . . . . . . . . . . . 889
13.10 Sliders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 893
13.11 Look and Feel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898
13.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 900
Review Questions and Exercises 901
Programming Challenges 906

Chapter 14 Applets and More 911
14.1 Introduction to Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911
14.2 A Brief Introduction to HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 913
14.3 Creating Applets with Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922
14.4 Using AWT for Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931
14.5 Drawing Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936
14.6 Handling Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957
14.7 Timer Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 967
14.8 Playing Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971
14.9 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 976
Review Questions and Exercises 976
Programming Challenges 982

Chapter 15 Recursion 985
15.1 Introduction to Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985
15.2 Solving Problems with Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 988
15.3 Examples of Recursive Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993
15.4 A Recursive Binary Search Method . . . . . . . . . . . . . . . . . . . . . . . . . . 999
15.5 The Towers of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1002
15.6 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1007
Review Questions and Exercises 1007
Programming Challenges 1010

Chapter 16 Databases 1013

16.1 Introduction to Database Management Systems . . . . . . . . . . . . . . 1013
16.2 Tables, Rows, and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1019
16.3 Introduction to the SQL SELECT Statement . . . . . . . . . . . . . . . . . . 1022
16.4 Inserting Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1044
16.5 Updating and Deleting Existing Rows. . . . . . . . . . . . . . . . . . . . . . . 1048
16.6 Creating and Deleting Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1057
16.7 Creating a New Database with JDBC . . . . . . . . . . . . . . . . . . . . . . . 1060
16.8 Scrollable Result Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1062
16.9 Result Set Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1063
16.10 Displaying Query Results in a JTable . . . . . . . . . . . . . . . . . . . . . . . . . .1067
16.11 Relational Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1077
16.12 Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1099
16.13 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1101
Review Questions and Exercises 1101
Programming Challenges 1106
Java TM Quick Reference 1109
Index
 
Student CD-ROM:
Appendix A Getting Started with Alice
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 More about JOptionPane Dialog Boxes
Appendix K Answers to Checkpoints
Appendix L Answers to Odd-Numbered Review Questions
Case Study 1 Calculating Sales Commission
Case Study 2 The Amortization Class
Case Study 3 The PinTester Class
Case Study 4 Parallel Arrays
Case Study 5 The FeetInches Class
Case Study 6 The SerialNumber Class
Case Study 7 A Simple Text Editor Application

Purchase Info ?

With CourseSmart eTextbooks and eResources, you save up to 60% off the price of new print textbooks, and can switch between studying online or offline to suit your needs.

Once you have purchased your eTextbooks and added them to your CourseSmart bookshelf, you can access them anytime, anywhere.

Buy Access

Starting Out with Java: From Control Structures through Objects, CourseSmart eTextbook, 5th Edition
Format: Safari Book

$59.99 | ISBN-13: 978-0-13-285591-4