Home > Store

Android How to Program, 2nd Edition

Register your product to gain access to bonus material or receive a coupon.

Android How to Program, 2nd Edition


  • Sorry, this book is no longer in print.
Not for Sale




Add an App Component to your Java Course

  • 300-Page Optional Introduction to Object-Oriented Programming in Java. An extensive, optional introduction to programming using the Java language makes this book appropriate for Java courses. The appendices provide a condensed, friendly introduction to Java and the object-oriented programming techniques needed to develop Android apps. (Early Objects) Java content in the appendices can be used with Java SE 6 or Java SE 7. Studying objects and classes early helps novice readers master these concepts more thoroughly before attempting the object-oriented Android material.
  • GUI Presentation.Appendix I introduces Java GUI development. Android provides its own GUI components, so this appendix presents a few Java GUI components and focuses on nested classes and anonymous inner classes, which are used extensively for event-handling in Android GUIs.
  • Java Exception Handling.The Deitels integrate basic exception handling early in the Java content then present a richer treatment in Appendix H; we use exception handling throughout the Android chapters.
  • Classes Arrays and ArrayList; Collections.Appendix E covers class Arrays–which contains methods for performing common array manipulations–and generic class ArrayList–which implements a dynamically resizable array-like data structure. Appendix J introduces Java’s generic collections that are used frequently in our Android treatment.
  • Java Multithreading.Maintaining app responsiveness is a key to building robust Android apps and requires extensive use of Android multithreading. Appendix J introduces multithreading fundamentals so that you can understand the authors’ use of the Android AsyncTask class in Chapter 8.

Motivate Students with an App-driven Approach to Android 4.3 and 4.4 Development

  • App-Driven Approach. Concepts are presented in the context of  complete working Android Apps (seven in the print book and more online), complete with syntax coloring, code walkthroughs and sample outputs. Each of the App chapters begins with an introduction to the App, an App test-drive showing one or more sample executions and a technologies overview. Then a detailed code walkthrough of the App’s source code discusses the programming concepts and demonstrates the functionality of the Android APIs used in the App.
  • Android SDK 4.3 and 4.4. The authors cover various new Android Software Development Kit (SDK) 4.3 and 4.4 features. [Note: The apps in this book are configured to run on Android devices with Android 4.3 and higher; however, most apps will work in 4.0 and higher by changing their minimum required SDK.]
  • Fragments. Starting with Chapter 5, the Deitels use Fragments to create and manage portions of each app’s GUI. Students can combine several fragments to create user interfaces that take advantage of tablet screen sizes. They also can easily interchange fragments to make GUIs more dynamic (Chapter 8).
  • Support for multiple screen sizes and resolutions.Throughout the app chapters the authors demonstrate how to use Android’s mechanisms for automatically choosing resources (layouts, images, etc.) based on a device’s size and orientation.
  • Eclipse-Based Android Development Tools (ADT) coverage in the print book.The free ADT integrated development environment (IDE)–which includes Eclipse and the ADT plugin–combined with the free Java Development Kit (JDK) provide all the software you’ll need to create, run and debug Android apps, export them for distribution (e.g., upload them to Google Play™) and more.
  • Android Studio.This is the preferred IDE for the future of Android app development. Because this IDE is evolving quickly, the authors put their discussions of it online at: http://www.deitel.com/books/AndroidHTP2
  • Immersive Mode.The status bar at the top of the screen and the menu buttons at the bottom can be hidden, allowing your apps to fill more of the screen. Users can access the status bar by swiping down from the top of the screen, and the system bar (with the back button, home button and recent apps button) by swiping up from the bottom.
  • Printing Framework.Android 4.4 KitKat allows you to add printing functionality to your apps, such as locating available printers over Wi-Fi or the cloud, selecting the paper size and specifying which pages to print.
  • Testing on Android Smartphones, Tablets and the Android Emulator.For the best app-development experience, you should test your apps on actual Android smartphones and tablets. You can still have a meaningful experience using the Android emulator (see the Before You Begin section), however it’s processor-intensive and can be slow–particularly with games that have a lot of moving parts. In Chapter 1, we mention some Android features that are not supported on the emulator.
  • Multimedia.The apps in the print book use a broad range of Android multimedia capabilities, including graphics, images, frame-by-frame animation and audio. The apps in the online chapters use property animation, video, speech synthesis and speech recognition.
  • Android Best Practices. The authors adhere to accepted Android best practices, pointing them out in the detailed code walkthroughs. For more information, visit http://developer.android.com/guide/practices/index.html.

Enhance Learning with Outstanding Pedagogical Features

  • Syntax Shading.For readability, the authors use shade syntax for the code that is similar to Eclipse’s and Android Studio’s use of syntax coloring.
  • Code Highlighting.Emphasis is put on key code segments in each program by enclosing them in light gray rectangles.
  • Using Fonts for Emphasis.The authors use various font conventions for easy reference
  • Using the > Character.The > character is used to indicate selecting a menu item from a menu.
  • Source Code.All of the book’s source code is available for download from:
  • Chapter Objectives.Each chapter begins with a list of learning objectives.
  • Figures.Hundreds of tables, source code listings and screen shots are included.
  • Software Engineering. The authors stress program clarity and performance, and concentrate on building well-engineered, object-oriented software.
  • Self-Review Exercises and Answers.Extensive self-review exercises and answers are included for self-study.
  • Exercises with a Current Flair.Students will develop apps using a broad array of current technologies.
    • All of the Android programming exercises require the implementation of complete apps where students will: enhance the existing chapter apps; develop similar apps; use creativity to develop their own apps that use the chapter technologies; and build new apps based on open-source apps available on the Internet. The Android exercises also include short-answer fill-in and true/false questions.
    • In the Java exercises, students will recall important terms and concepts; indicate what code segments do; indicate what’s wrong with a portion of code; write Java statements, methods and classes; and write complete Java programs.
  • Index.An extensive index is included for reference. The page number of the defining occurrence of each key term in the book is highlighted in the index in bold.
  • Student Companion Website. Students can access source code for the examples in the book as well as additional app-development chapters at www.pearsonhighered.com/deitel.
  • Instructor Supplements. The Pearson Instructor Resource Center (IRC) includes:
    • PowerPoint® slides containing all the code and figures in the text.
    • Test Item File of short-answer questions.
    • Solutions Manual with solutions to the end-of-chapter short-answer exercises for both the Java and Android content. For the Java content, solutions are provided for most of the programming exercises. The suggested Android app-development project exercises are not typical homework problems. These tend to be substantial projects–many of which could require weeks of effort, possibly with students working in teams. Selected solutions only are provided for these project exercises–these will be available on the IRC for fall semester 2014 classes. Contact the authors at deitel@deitel.com if you have any questions.


  • Copyright 2015
  • Dimensions: 7" x 9-1/8"
  • Pages: 736
  • Edition: 2nd
  • Book
  • ISBN-10: 0-13-376403-6
  • ISBN-13: 978-0-13-376403-1

Android How to Program, Second Edition provides a clear and entertaining App-driven introduction to Android 4.3 and 4.4 development for both introductory- and intermediate-level programming courses. It also serves as a great reference and tutorial to learn Android programming.

The Deitels' App-driven Approach is simply the best way to master Android programming! The Deitels teach Android programming through seven complete, working Android Apps in the print book and more online. Each chapter presents new concepts through a single App. The authors first provide an introduction to the app, an app test-drive showing one or more sample executions, and a technologies overview. Next, the authors proceed with a detailed code walkthrough of the app’s source code in which they discuss the programming concepts and demonstrate the functionality of the Android APIs used in the app. The book also has an extensive introduction to programming using the Java language, making this book appropriate for Java courses that want to add an App-programming flavor.

Teaching and Learning Experience

This program will provide a better teaching and learning experience—for you and your students.

  • Add an App Component to your Java Course: The appendices provide a condensed, friendly introduction to Java and the object-oriented programming techniques students will need to develop Android apps.
  • Motivate Students with an App-driven Approach to Android 4.3 and 4.4 Development: Concepts are presented in the context of 7 complete working Android Apps, using the latest mobile computing technologies.
  • Enhance Learning with Outstanding Pedagogical Features: The Deitels present hundreds of Android short-answer questions and app-development exercises complete with syntax coloring, code walkthroughs and sample outputs.

Sample Content

Table of Contents

Preface xix
Before You Begin xxxi
1 Introduction to Android 1
1.1 Introduction 2
1.2 Android–The World’s Leading Mobile Operating System 3
1.3 Android Features 3
1.4 Android Operating System 7
1.4.1 Android 2.2 (Froyo) 7
1.4.2 Android 2.3 (Gingerbread) 8
1.4.3 Android 3.0 through 3.2 (Honeycomb) 8
1.4.4 Android 4.0 through 4.0.4 (Ice Cream Sandwich) 8
1.4.5 Android 4.1—4.3 (Jelly Bean) 9
1.4.6 Android 4.4 (KitKat) 10
1.5 Downloading Apps from Google Play 11
1.6 Packages 12
1.7 Android Software Development Kit (SDK) 13
1.8 Object-Oriented Programming: A Quick Refresher 16
1.8.1 The Automobile as an Object 17
1.8.2 Methods and Classes 17
1.8.3 Instantiation 17
1.8.4 Reuse 17
1.8.5 Messages and Method Calls 17
1.8.6 Attributes and Instance Variables 18
1.8.7 Encapsulation 18
1.8.8 Inheritance 18
1.8.9 Object-Oriented Analysis and Design (OOAD) 18
1.9 Test-Driving the Doodlz App in an Android Virtual Device (AVD) 19
1.9.1 Running the Doodlz App in the Nexus 4 Smartphone AVD 19
1.9.2 Running the Doodlz App in a Tablet AVD 28
1.9.3 Running the Doodlz App on an Android Device 30
1.10 Building Great Android Apps 30
1.11 Android Development Resources 32
1.12 Wrap-Up 35

2 Welcome App 38
Dive-Into® the Android Developer Tools: Introducing Visual GUI Design, Layouts, Accessibility and Internationalization
2.1 Introduction 39
2.2 Technologies Overview 40
2.2.1 Android Developer Tools IDE 40
2.2.2 TextViews and ImageViews 40
2.2.3 App Resources 40
2.2.4 Accessibility 40
2.2.5 Internationalization 40
2.3 Creating an App 41
2.3.1 Launching the Android Developer Tools IDE 41
2.3.2 Creating a New Project 41
2.3.3 New Android Application Dialog 42
2.3.4 Configure Project Step 43
2.3.5 Configure Launcher Icon Step 43
2.3.6 Create Activity Step 45
2.3.7 Blank Activity Step 46
2.4 Android Developer Tools Window 47
2.4.1 Package Explorer Window 48
2.4.2 Editor Windows 48
2.4.3 Outline Window 48
2.4.4 App Resource Files 48
2.4.5 Graphical Layout Editor 49
2.4.6 The Default GUI 49
2.5 Building the App’s GUI with the Graphical Layout Editor 51
2.5.1 Adding Images to the Project 51
2.5.2 Changing the Id Property of the RelativeLayout and the TextView 52
2.5.3 Configuring the TextView 53
2.5.4 Adding ImageViews to Display the Images 57
2.6 Running the Welcome App 59
2.7 Making Your App Accessible 60
2.8 Internationalizing Your App 62
2.9 Wrap-Up 66

3 Tip Calculator App 69
Introducing GridLayout, LinearLayout, EditText, SeekBar, Event Handling, NumberFormat and Defining App Functionality with Java
3.1 Introduction 70
3.2 Test-Driving the Tip Calculator App 71
3.3 Technologies Overview 72
3.3.1 Class Activity 72
3.3.2 Activity Lifecycle Methods 72
3.3.3 Arranging Views with LinearLayout and GridLayout 73
3.3.4 Creating and Customizing the GUI with the Graphical Layout Editor and the Outline and Properties Windows 73
3.3.5 Formatting Numbers as Locale-Specific Currency and Percentage Strings 74
3.3.6 Implementing Interface TextWatcher for Handling EditText Text Changes 74
3.3.7 Implementing Interface OnSeekBarChangeListener for Handling SeekBar Thumb Position Changes 74
3.3.8 AndroidManifest.xml 75
3.4 Building the App’s GUI 75
3.4.1 GridLayout Introduction 75
3.4.2 Creating the TipCalculator Project 77
3.4.3 Changing to a GridLayout 77
3.4.4 Adding the TextViews, EditText, SeekBar and LinearLayouts 78
3.4.5 Customizing the Views to Complete the Design 80
3.5 Adding Functionality to the App 84
3.6 AndroidManifest.xml 92
3.7 Wrap-Up 93

4 Twitter® Searches App 97
SharedPreferences, Collections, ImageButton, ListView, ListActivity, ArrayAdapter, Implicit Intents and AlertDialogs
4.1 Introduction 98
4.2 Test-Driving the App 99
4.2.1 Importing the App and Running It 99
4.2.2 Adding a Favorite Search 100
4.2.3 Viewing Twitter Search Results 101
4.2.4 Editing a Search 102
4.2.5 Sharing a Search 104
4.2.6 Deleting a Search 104
4.2.7 Scrolling Through Saved Searches 105
4.3 Technologies Overview 105
4.3.1 ListView 105
4.3.2 ListActivity 106
4.3.3 Customizing a ListActivity’s Layout 106
4.3.4 ImageButton 106
4.3.5 SharedPreferences 106
4.3.6 Intents for Launching Other Activities 107
4.3.7 AlertDialog 107
4.3.8 AndroidManifest.xml 108
4.4 Building the App’s GUI 108
4.4.1 Creating the Project 108
4.4.2 activity_main.xml Overview 109
4.4.3 Adding the GridLayout and Components 110
4.4.4 Graphical Layout Editor Toolbar 115
4.4.5 ListView Item’s Layout: list_item.xml 116
4.5 Building the MainActivity Class 117
4.5.1 package and import Statements 117
4.5.2 Extending ListActivity 119
4.5.3 Fields of Class MainActivity 119
4.5.4 Overriding Activity Method onCreate 120
4.5.5 Anonymous Inner Class That Implements the saveButton’s OnClickListener to Save a New or Updated Search 122
4.5.6 addTaggedSearch Method 124
4.5.7 Anonymous Inner Class That Implements the ListView’s OnItemClickListener to Display Search Results 125
4.5.8 Anonymous Inner Class That Implements the ListView’s OnItemLongClickListener to Share, Edit or Delete a Search 127
4.5.9 shareSearch Method 129
4.5.10 deleteSearch Method 130
4.6 AndroidManifest.xml 132
4.7 Wrap-Up 132

5 Flag Quiz App 136
Fragments, Menus, Preferences, AssetManager, Tweened Animations, Handler, Toasts, Explicit Intents, Layouts for Multiple Device Orientations
5.1 Introduction 137
5.2 Test-Driving the Flag Quiz App 139
5.2.1 Importing the App and Running It 139
5.2.2 Configuring the Quiz 139
5.2.3 Taking the Quiz 141
5.3 Technologies Overview 143
5.3.1 Menus 143
5.3.2 Fragments 143
5.3.3 Fragment Lifecycle Methods 144
5.3.4 Managing Fragments 144
5.3.5 Preferences 144
5.3.6 assets Folder 144
5.3.7 Resource Folders 145
5.3.8 Supporting Different Screen Sizes and Resolutions 145
5.3.9 Determining the Screen Size 146
5.3.10 Toasts for Displaying Messages 146
5.3.11 Using a Handler to Execute a Runnable in the Future 146
5.3.12 Applying an Animation to a View 146
5.3.13 Logging Exception Messages 147
5.3.14 Using an Explicit Intent to Launch Another Activity in the Same App 147
5.3.15 Java Data Structures 147
5.4 Building the GUI and Resource Files 147
5.4.1 Creating the Project 147
5.4.2 strings.xml and Formatted String Resources 148
5.4.3 arrays.xml 149
5.4.4 colors.xml 150
5.4.5 dimens.xml 150
5.4.6 activity_settings.xml Layout 151
5.4.7 activity_main.xml Layout for Phone and Tablet Portrait Orientation 151
5.4.8 fragment_quiz.xml Layout 151
5.4.9 activity_main.xml Layout for Tablet Landscape Orientation 154
5.4.10 preferences.xml for Specifying the App’s Settings 155
5.4.11 Creating the Flag Shake Animation 156
5.5 MainActivity Class 158
5.5.1 package Statement, import Statements and Fields 158
5.5.2 Overridden Activity Method onCreate 159
5.5.3 Overridden Activity Method onStart 161
5.5.4 Overridden Activity Method onCreateOptionsMenu 161
5.5.5 Overridden Activity Method onOptionsItemSelected 162
5.5.6 Anonymous Inner Class That Implements OnSharedPreferenceChangeListener 163
5.6 QuizFragment Class 164
5.6.1 package Statement and import Statements 164
5.6.2 Fields 165
5.6.3 Overridden Fragment Method onCreateView 166
5.6.4 Method updateGuessRows 168
5.6.5 Method updateRegions 169
5.6.6 Method resetQuiz 169
5.6.7 Method loadNextFlag 171
5.6.8 Method getCountryName 173
5.6.9 Anonymous Inner Class That Implements OnClickListener 173
5.6.10 Method disableButtons 176
5.7 SettingsFragment Class 176
5.8 SettingsActivity Class 177
5.9 AndroidManifest.xml 177
5.10 Wrap-Up 178

6 Cannon Game App 182
Listening for Touches, Manual Frame-By-Frame Animation, Graphics, Sound, Threading, SurfaceView and SurfaceHolder
6.1 Introduction 183
6.2 Test-Driving the Cannon Game App 185
6.3 Technologies Overview 185
6.3.1 Attaching a Custom View to a Layout 185
6.3.2 Using the Resource Folder raw 185
6.3.3 Activity and Fragment Lifecycle Methods 185
6.3.4 Overriding View Method onTouchEvent 186
6.3.5 Adding Sound with SoundPool and AudioManager 186
6.3.6 Frame-by-Frame Animation with Threads, SurfaceView and SurfaceHolder 186
6.3.7 Simple Collision Detection 187
6.3.8 Drawing Graphics Using Paint and Canvas 187
6.4 Building the App’s GUI and Resource Files 187
6.4.1 Creating the Project 187
6.4.2 strings.xml 188
6.4.3 fragment_game.xml 188
6.4.4 activity_main.xml 189
6.4.5 Adding the Sounds to the App 189
6.5 Class Line Maintains a Line’s Endpoints 189
6.6 MainActivity Subclass of Activity 190
6.7 CannonGameFragment Subclass of Fragment 190
6.8 CannonView Subclass of View 192
6.8.1 package and import Statements 192
6.8.2 Instance Variables and Constants 193
6.8.3 Constructor 194
6.8.4 Overriding View Method onSizeChanged 196
6.8.5 Method newGame 197
6.8.6 Method updatePositions 198
6.8.7 Method fireCannonball 201
6.8.8 Method alignCannon 202
6.8.9 Method drawGameElements 203
6.8.10 Method showGameOverDialog 205
6.8.11 Methods stopGame and releaseResources 206
6.8.12 Implementing the SurfaceHolder.Callback Methods 207
6.8.13 Overriding View Method onTouchEvent 208
6.8.14 CannonThread: Using a Thread to Create a Game Loop 209
6.9 Wrap-Up 210

7 Doodlz App 215
Two-Dimensional Graphics, Canvas, Bitmap, Accelerometer, SensorManager, Multitouch Events, MediaStore, Printing, Immersive Mode
7.1 Introduction 216
7.2 Technologies Overview 218
7.2.1 Using SensorManager to Listen for Accelerometer Events 218
7.2.2 Custom DialogFragments 218
7.2.3 Drawing with Canvas and Bitmap 219
7.2.4 Processing Multiple Touch Events and Storing Lines in Paths 219
7.2.5 Android 4.4 Immersive Mode 219
7.2.6 GestureDetector and SimpleOnGestureListener 219
7.2.7 Saving the Drawing to the Device’s Gallery 219
7.2.8 Android 4.4 Printing and the Android Support Library’s PrintHelper Class 220
7.3 Building the App’s GUI and Resource Files 220
7.3.1 Creating the Project 220
7.3.2 strings.xml 220
7.3.3 dimens.xml 221
7.3.4 Menu for the DoodleFragment 222
7.3.5 activity_main.xml Layout for MainActivity 223
7.3.6 fragment_doodle.xml Layout for DoodleFragment 223
7.3.7 fragment_color.xml Layout for ColorDialogFragment 224
7.3.8 fragment_line_width.xml Layout for LineWidthDialogFragment 226
7.3.9 Adding Class EraseImageDialogFragment 227
7.4 MainActivity Class 228
7.5 DoodleFragment Class 229
7.6 DoodleView Class 236
7.7 ColorDialogFragment Class 248
7.8 LineWidthDialogFragment Class 251
7.9 EraseImageDialogFragment Class 255
7.10 Wrap-Up 256

8 Address Book App 260
ListFragment, FragmentTransactions and the Fragment Back Stack, Threading and AsyncTasks, CursorAdapter, SQLite and GUI Styles
8.1 Introduction 261
8.2 Test-Driving the Address Book App 264
8.3 Technologies Overview 264
8.3.1 Displaying Fragments with FragmentTransactions 265
8.3.2 Communicating Data Between a Fragment and a Host Activity 265
8.3.3 Method onSaveInstanceState 265
8.3.4 Defining Styles and Applying Them to GUI Components 265
8.3.5 Specifying a Background for a TextView 265
8.3.6 Extending Class ListFragment to Create a Fragment That Contains a ListView 266
8.3.7 Manipulating a SQLite Database 266
8.3.8 Performing Database Operations Outside the GUI Thread with AsyncTasks 266
8.4 Building the GUI and Resource Files 266
8.4.1 Creating the Project 266
8.4.2 Creating the App’s Classes 267
8.4.3 strings.xml 267
8.4.4 styles.xml 268
8.4.5 textview_border.xml 269
8.4.6 MainActivity’s Layout: activity_main.xml 270
8.4.7 DetailsFragment’s Layout: fragment_details.xml 270
8.4.8 AddEditFragment’s Layout: fragment_add_edit.xml 272
8.4.9 Defining the Fragments’ Menus 273
8.5 MainActivity Class 274
8.6 ContactListFragment Class 280
8.7 AddEditFragment Class 287
8.8 DetailsFragment Class 293
8.9 DatabaseConnector Utility Class 301
8.10 Wrap-Up 306

9 Google Play and App Business Issues 310
9.1 Introduction 311
9.2 Preparing Your Apps for Publication 311
9.2.1 Testing Your App 312
9.2.2 End User License Agreement 312
9.2.3 Icons and Labels 312
9.2.4 Versioning Your App 313
9.2.5 Licensing to Control Access to Paid Apps 313
9.2.6 Obfuscating Your Code 313
9.2.7 Getting a Private Key for Digitally Signing Your App 314
9.2.8 Screenshots 314
9.2.9 Promotional App Video 315
9.3 Pricing Your App: Free or Fee 316
9.3.1 Paid Apps 317
9.3.2 Free Apps 317
9.4 Monetizing Apps with In-App Advertising 318
9.5 Monetizing Apps: Using In-App Billing to Sell Virtual Goods 319
9.6 Registering at Google Play 320
9.7 Setting Up a Google Wallet Merchant Account 321
9.8 Uploading Your Apps to Google Play 322
9.9 Launching the Play Store from Within Your App 323
9.10 Managing Your Apps in Google Play 324
9.11 Other Android App Marketplaces 324
9.12 Other Popular Mobile App Platforms 324
9.13 Marketing Your Apps 325
9.14 Wrap-Up 329

A Introduction to Java Applications 332
A.1 Introduction 333
A.2 Your First Program in Java: Printing a Line of Text 333
A.3 Modifying Your First Java Program 337
A.4 Displaying Text with printf 339
A.5 Another Application: Adding Integers 339
A.6 Memory Concepts 343
A.7 Arithmetic 344
A.8 Decision Making: Equality and Relational Operators 347
A.9 Wrap-Up 351

B Introduction to Classes, Objects, Methods and Strings 356
B.1 Introduction 357
B.2 Declaring a Class with a Method and Instantiating an Object of a Class 357
B.3 Declaring a Method with a Parameter 360
B.4 Instance Variables, set Methods and get Methods 363
B.5 Primitive Types vs. Reference Types 367
B.6 Initializing Objects with Constructors 368
B.7 Floating-Point Numbers and Type double 370
B.8 Wrap-Up 374

C Control Statements 378
C.1 Introduction 379
C.2 Algorithms 379
C.3 Pseudocode 380
C.4 Control Structures 380
C.5 if Single-Selection Statement 381
C.6 if…else Double-Selection Statement 381
C.7 while Repetition Statement 384
C.8 Case Study: Counter-Controlled Repetition 384
C.9 Case Study: Sentinel-Controlled Repetition 388
C.10 Case Study: Nested Control Statements 393
C.11 Compound Assignment Operators 396
C.12 Increment and Decrement Operators 396
C.13 Primitive Types 398
C.14 Essentials of Counter-Controlled Repetition 399
C.15 for Repetition Statement 400
C.16 Examples Using the for Statement 402
C.17 do…while Repetition Statement 404
C.18 switch Multiple-Selection Statement 405
C.19 break and continue Statements 412
C.20 Logical Operators 412
C.21 Wrap-Up 415

D Methods: A Deeper Look 423
D.1 Introduction 424
D.2 ProgramModules in Java 424
D.3 static Methods, static Fields and Class Math 425
D.4 Declaring Methods with Multiple Parameters 427
D.5 Notes on Declaring and Using Methods 430
D.6 Method-Call Stack and Activation Records 431
D.7 Argument Promotion and Casting 431
D.8 Java API Packages 432
D.9 Introduction to Random-Number Generation 433
D.9.1 Scaling and Shifting of Random Numbers 434
D.9.2 Random-Number Repeatability for Testing and Debugging 435
D.10 Case Study: A Game of Chance; Introducing Enumerations 436
D.11 Scope of Declarations 440
D.12 Method Overloading 442
D.13 Wrap-Up 445

E Arrays and ArrayLists 452
E.1 Introduction 453
E.2 Arrays 453
E.3 Declaring and Creating Arrays 454
E.4 Examples Using Arrays 455
E.5 Case Study: Card Shuffling and Dealing Simulation 464
E.6 Enhanced for Statement 468
E.7 Passing Arrays to Methods 469
E.8 Case Study: Class GradeBook Using an Array to Store Grades 473
E.9 Multidimensional Arrays 478
E.10 Case Study: Class GradeBook Using a Two-Dimensional Array 482
E.11 Class Arrays 488
E.12 Introduction to Collections and Class ArrayList 490
E.13 Wrap-Up 493

F Classes and Objects: A Deeper Look 498
F.1 Introduction 499
F.2 Time Class Case Study 499
F.3 Controlling Access to Members 503
F.4 Referring to the Current Object’s Members with the this Reference 504
F.5 Time Class Case Study: Overloaded Constructors 506
F.6 Default and No-Argument Constructors 512
F.7 Composition 513
F.8 Enumerations 516
F.9 Garbage Collection 518
F.10 static Class Members 519
F.11 final Instance Variables 523
F.12 Packages 523
F.13 Package Access 524
F.14 Wrap-Up 524

G Object-Oriented Programming: Inheritance and Polymorphism 527
G.1 Introduction to Inheritance 528
G.2 Superclasses and Subclasses 529
G.3 protected Members 530
G.4 Relationship between Superclasses and Subclasses 531
G.4.1 Creating and Using a CommissionEmployee Class 531
G.4.2 Creating and Using a BasePlusCommissionEmployee Class 536
G.4.3 Creating a CommissionEmployee—BasePlusCommissionEmployee Inheritance Hierarchy 541
G.4.4 CommissionEmployee—BasePlusCommissionEmployee Inheritance Hierarchy Using protected Instance Variables 544
G.4.5 CommissionEmployee—BasePlusCommissionEmployee Inheritance Hierarchy Using private Instance Variables 547
G.5 Class Object 552
G.6 Introduction to Polymorphism 553
G.7 Polymorphism: An Example 554
G.8 Demonstrating Polymorphic Behavior 555
G.9 Abstract Classes and Methods 558
G.10 Case Study: Payroll SystemUsing Polymorphism 559
G.10.1 Abstract Superclass Employee 560
G.10.2 Concrete Subclass SalariedEmployee 563
G.10.3 Concrete Subclass HourlyEmployee 565
G.10.4 Concrete Subclass CommissionEmployee 566
G.10.5 Indirect Concrete Subclass BasePlusCommissionEmployee 568
G.10.6 Polymorphic Processing, Operator instanceof and Downcasting 569
G.10.7 Summary of the Allowed Assignments Between Superclass and Subclass Variables 574
G.11 final Methods and Classes 575
G.12 Case Study: Creating and Using Interfaces 576
G.12.1 Developing a Payable Hierarchy 577
G.12.2 Interface Payable 578
G.12.3 Class Invoice 579
G.12.4 Modifying Class Employee to Implement Interface Payable 581
G.12.5 Modifying Class SalariedEmployee for Use in the Payable Hierarchy 583
G.12.6 Using Interface Payable to Process Invoices and Employees Polymorphically 585
G.13 Common Interfaces of the Java API 586
G.14 Wrap-Up 587

H Exception Handling: A Deeper Look 591
H.1 Introduction 592
H.2 Example: Divide by Zero without Exception Handling 592
H.3 Example: Handling ArithmeticExceptions and InputMismatchExceptions 594
H.4 When to Use Exception Handling 599
H.5 Java Exception Hierarchy 599
H.6 finally Block 602
H.7 Stack Unwinding and Obtaining Information from an Exception Object 606
H.8 Wrap-Up 609

I GUI Components and Event Handling 612
I.1 Introduction 613
I.2 Nimbus Look-and-Feel 613
I.3 Text Fields and an Introduction to Event Handling with Nested Classes 614
I.4 Common GUI Event Types and Listener Interfaces 620
I.5 How Event Handling Works 621
I.6 JButton 623
I.7 JComboBox; Using an Anonymous Inner Class for Event Handling 627
I.8 Adapter Classes 630
I.9 Wrap-Up 631

J Other Java Topics 632
J.1 Introduction 633
J.2 Collections Overview 633
J.3 Type-Wrapper Classes for Primitive Types 634
J.4 Interface Collection and Class Collections 634
J.5 Lists 635
J.5.1 ArrayList and Iterator 635
J.5.2 LinkedList 637
J.5.3 Views into Collections and Arrays Method as List 640
J.6 Collections Methods 642
J.6.1 Method sort 642
J.6.2 Method shuffle 644
J.7 Interface Queue 645
J.8 Sets 645
J.9 Maps 646
J.10 Introduction to Files and Streams 649
J.11 Class File 650
J.12 Introduction to Object Serialization 651
J.13 Introduction to Multithreading 652
J.14 Creating and Executing Threads with the Executor Framework 653
J.15 Overview of Thread Synchronization 657
J.16 Concurrent Collections Overview 658
J.17 Multithreading with GUI 658
J.18 Wrap-Up 665

K Operator Precedence Chart 668

L Primitive Types 670

Index 671


Submit Errata

More Information

Unlimited one-month access with your purchase
Free Safari Membership