100% found this document useful (4 votes)
24 views

Starting Out with Java: From Control Structures through Objects (6th Edition) Gaddis instant download

The document is a promotional material for the book 'Starting Out with Java: From Control Structures through Objects (6th Edition)' by Tony Gaddis, along with links to download it and other related educational resources. It includes a brief overview of the book's contents, covering fundamental Java programming concepts, control structures, and object-oriented programming. Additionally, it lists other books by the same author and provides links for further exploration of educational materials.

Uploaded by

reddipborne6b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
24 views

Starting Out with Java: From Control Structures through Objects (6th Edition) Gaddis instant download

The document is a promotional material for the book 'Starting Out with Java: From Control Structures through Objects (6th Edition)' by Tony Gaddis, along with links to download it and other related educational resources. It includes a brief overview of the book's contents, covering fundamental Java programming concepts, control structures, and object-oriented programming. Additionally, it lists other books by the same author and provides links for further exploration of educational materials.

Uploaded by

reddipborne6b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

Starting Out with Java: From Control Structures

through Objects (6th Edition) Gaddis download

https://textbookfull.com/product/starting-out-with-java-from-
control-structures-through-objects-6th-edition-gaddis/

Download more ebook from https://textbookfull.com


We believe these products will be a great fit for you. Click
the link to download now, or visit textbookfull.com
to discover even more!

Starting Out with Java: From Control Structures through


Objects (What's New in Computer Science) 7th Edition
Gaddis

https://textbookfull.com/product/starting-out-with-java-from-
control-structures-through-objects-whats-new-in-computer-
science-7th-edition-gaddis/

Starting Out with C++: From Control Structures through


Objects, Brief Edition, 9/e Tony Gaddis

https://textbookfull.com/product/starting-out-with-c-from-
control-structures-through-objects-brief-edition-9-e-tony-gaddis/

Starting out with C++. From control structures through


objects Second Custom Edition For Penn State University
Tony Gaddis

https://textbookfull.com/product/starting-out-with-c-from-
control-structures-through-objects-second-custom-edition-for-
penn-state-university-tony-gaddis/

Starting out with Python® Gaddis

https://textbookfull.com/product/starting-out-with-python-gaddis/
Starting Out With Visual Basic (8th Edition) Tony
Gaddis

https://textbookfull.com/product/starting-out-with-visual-
basic-8th-edition-tony-gaddis/

Objects : nothing out of the ordinary 1st Edition


Korman

https://textbookfull.com/product/objects-nothing-out-of-the-
ordinary-1st-edition-korman/

Big Java: Early Objects 7th Edition Cay S. Horstmann

https://textbookfull.com/product/big-java-early-objects-7th-
edition-cay-s-horstmann/

Beginning Entity Framework Core 2.0: Database Access


from .NET - Work directly with data through domain-
specific objects and methods 1st Edition Derek J.
Rouleau
https://textbookfull.com/product/beginning-entity-framework-
core-2-0-database-access-from-net-work-directly-with-data-
through-domain-specific-objects-and-methods-1st-edition-derek-j-
rouleau/

Making it in Real Estate Starting Out as a Developer


2nd Edition John Mcnellis

https://textbookfull.com/product/making-it-in-real-estate-
starting-out-as-a-developer-2nd-edition-john-mcnellis/
Starting Out with

Java
TM

From Control Structures


through Objects
This page intentionally left blank
Starting Out with

Java
TM

From Control Structures


through Objects
Sixth Edition

Tony Gaddis
Haywood Community College

Boston Columbus Indianapolis New York San Francisco Hoboken


Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montréal Toronto
Delhi Mexico City São Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo
Editor in Chief: Marcia Horton Senior Specialist, Program Planning and Support:
Acquisitions Editor: Matt Goldstein Maura Zaldivar-Garcia
Editorial Assistant: Kelsey Loanes Cover Designer: Joyce Wells
VP of Marketing: Christy Lesko Cover Image: Binh Thanh Bui/Shutterstock
Director of Field Marketing: Tim Galligan Manager, Rights Management: Rachel Youdelman
Product Marketing Manager: Bram van Kempen Associate Project Manager, Rights Management:
Field Marketing Manager: Demetrius Hall William J. Opaluch
Marketing Assistant: Jon Bryant Full-Service Project Management: Kailash Jadli,
Director of Product Management: Erin Gregg Aptara®, Inc.
Team Lead Product Management: Scott Disanno Composition: Aptara®, Inc.
Program Manager: Carole Snyder Printer/Bindery: Edwards Brothers
Production Project Manager: Camille Trentacoste Cover printer: Phoenix Color/Hagerstown
Procurement Manager: Mary Fischer

Copyright © 2016 by Pearson Education, Inc. or its affiliates. All Rights Reserved. Printed in the United States of America.
This publication is protected by copyright, and permission should be obtained from the publisher prior to any prohibited
reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical,
photocopying, recording, or otherwise. For information regarding permissions, request forms and the appropriate contacts
within the Pearson Education Global Rights & Permissions department, please visit www.pearsoned.com/permissions/.

Many of the designations by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those
designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed in
initial caps or all caps.

Unless otherwise indicated herein, any third-party trademarks that may appear in this work are the property of their
respective owners and any references to third-party trademarks, logos or other trade dress are for demonstrative or
descriptive purposes only. Such references are not intended to imply any sponsorship, endorsement, authorization, or
promotion of Pearson’s products by the owners of such marks, or any relationship between the owner and Pearson
Education, Inc. or its affiliates, authors, licensees or distributors.

Microsoft and/or its respective suppliers make no representations about the suitability of the information contained in the
documents and related graphics published as part of the services for any purpose. All such documents and related graphics
are provided “as is” without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties
and conditions with regard to this information, including all warranties and conditions of merchantability. Whether
express, implied or statutory, fitness for a particular purpose, title and non-infringement. In no event shall microsoft and/or
its respective suppliers be liable for any special, indirect or consequential damages or any damages whatsoever resulting
from loss of use, data or profits, whether in an action of contract. Negligence or other tortious action, arising out of or in
connection with the use or performance of information available from the services.

The documents and related graphics contained herein could include technical inaccuracies or typographical errors changes are
periodically added to the information herein. Microsoft and/or its respective suppliers may make improvements and/or
changes in the product(s) and/or the program(s) described herein at any time partial screen shots may be viewed in full within
the software version specified.
Microsoft® Windows®, and Microsoft Office® are registered trademarks of the Microsoft Corporation in the U.S.A. and
other countries. This book is not sponsored or endorsed by or affiliated with the Microsoft Corporation.

Library of Congress Cataloging-in-Publication Data

Gaddis, Tony, author.


Starting out with Java. From control structures through objects/Tony Gaddis,
Haywood Community College.—6th edition.
   pages cm
ISBN-13: 978-0-13-395705-1
ISBN-10: 0-13-395705-5
1. Java (Computer program language) 2. Data structures (Computer science)
3. Object-oriented programming (Computer science) I. Title.
QA76.73.J38G333 2016
005.13'3—dc23
2014049102

10 9 8 7 6 5 4 3 2 1

ISBN-13: 978-0-13-395705-1
ISBN-10:    0-13-395705-5
Contents in Brief

Preface    xxiii
Chapter 1 Introduction to Computers and Java 1
Chapter 2 Java Fundamentals 27
Chapter 3 Decision Structures 111
Chapter 4 Loops and Files 189
Chapter 5 Methods 269
Chapter 6 A First Look at Classes 319
Chapter 7 Arrays and the ArrayList Class 405
Chapter 8 A Second Look at Classes and Objects 495
Chapter 9 Text Processing and More about Wrapper Classes 559
Chapter 10 Inheritance 613
Chapter 11 Exceptions and Advanced File I/O 703
Chapter 12 A First Look at GUI Applications 761
Chapter 13 Advanced GUI Applications 849
Chapter 14 Applets and More 917
Chapter 15 Creating GUI Applications with JavaFX and Scene Builder 991
Chapter 16 Recursion 1047
Chapter 17 Databases 1075
Index    1171
Appendixes A–M Companion Website
Case Studies 1–7 Companion Website

v
This page intentionally left blank
Contents

Preface    xxiii

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
Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
What Is a Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
A History of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Java Applications and Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5 What Is a Program Made Of? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Language Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Lines and Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
The Compiler and the Java Virtual Machine . . . . . . . . . . . . . . . . . . . . . . . 12
Java Software Editions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Compiling and Running a Java Program . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.6 The Programming Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Software Engineering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
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
Displaying Multiple Items with the + Operator . . . . . . . . . . . . . . . . . . . . 40
Be Careful with Quotation Marks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
More about Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
viii Contents

Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Class Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.4 Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
The Integer Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Floating-Point Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
The boolean Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
The char Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Variable Assignment and Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Variables Hold Only One Value at a Time . . . . . . . . . . . . . . . . . . . . . . . . 53
2.5 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Integer Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Grouping with Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
The Math Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
2.6 Combined Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2.7 Conversion between Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . 65
Mixed Integer Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Other Mixed Mathematical Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 68
2.8 Creating Named Constants with final . . . . . . . . . . . . . . . . . . . . . . . . 69
2.9 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Objects Are Created from Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Primitive Type Variables and Class Type Variables . . . . . . . . . . . . . . . . . . 71
Creating a String Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
2.10 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
2.11 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
2.12 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
2.13 Reading Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Reading a Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Mixing Calls to nextLine with Calls to Other Scanner Methods . . . . . . . . 88
2.14 Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Displaying Message Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Displaying Input Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
An Example Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Converting String Input to Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
2.15 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Review Questions and Exercises   100
Programming Challenges   105

Chapter 3 Decision Structures   111


3.1 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Using Relational Operators to Form Conditions . . . . . . . . . . . . . . . . . . . 113
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Programming Style and the if Statement . . . . . . . . . . . . . . . . . . . . . . . 118
Be Careful with Semicolons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Contents ix

Having Multiple Conditionally Executed Statements . . . . . . . . . . . . . . . . 119


Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Comparing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
3.2 The if-else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
3.3 Nested if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
3.4 The if-else-if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
3.5 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
The Precedence of Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Checking Numeric Ranges with Logical Operators . . . . . . . . . . . . . . . . . 144
3.6 Comparing String Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Ignoring Case in String Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
3.7 More about Variable Declaration and Scope . . . . . . . . . . . . . . . . . . . 151
3.8 The Conditional Operator (Optional) . . . . . . . . . . . . . . . . . . . . . . . . 152
3.9 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
3.10 Displaying Formatted Output with System.out.printf
and String.format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Format Specifier Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Specifying a Minimum Field Width . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Formatting String Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
The String.format Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
3.11 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Review Questions and Exercises   179
Programming Challenges   184

Chapter 4 Loops and Files   189


4.1 The Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . 189
The Difference between Postfix and Prefix Modes . . . . . . . . . . . . . . . . . . 192
4.2 The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
The while Loop Is a Pretest Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Infinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Don’t Forget the Braces with a Block of Statements . . . . . . . . . . . . . . . . 197
Programming Style and the while Loop . . . . . . . . . . . . . . . . . . . . . . . . 198
4.3 Using the while Loop for Input Validation . . . . . . . . . . . . . . . . . . . . 200
4.4 The do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
4.5 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
The for Loop Is a Pretest Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Avoid Modifying the Control Variable in the Body
of the for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Other Forms of the Update Expression . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Declaring a Variable in the for Loop’s Initialization Expression . . . . . . . 211
Creating a User Controlled for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Using Multiple Statements in the Initialization
and Update Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
x Contents

4.6 Running Totals and Sentinel Values . . . . . . . . . . . . . . . . . . . . . . . . . . 216


Using a Sentinel Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
4.7 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
4.8 The break and continue Statements (Optional) . . . . . . . . . . . . . . . . 229
4.9 Deciding Which Loop to Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
4.10 Introduction to File Input and Output . . . . . . . . . . . . . . . . . . . . . . . . 230
Using the PrintWriter Class to Write Data to a File . . . . . . . . . . . . . . . 230
Appending Data to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Specifying the File Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Reading Data from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Reading Lines from a File with the nextLine Method . . . . . . . . . . . . . . . 238
Adding a throws Clause to the Method Header . . . . . . . . . . . . . . . . . . . 241
Checking for a File’s Existence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
4.11 Generating Random Numbers with the Random Class . . . . . . . . . . . . 249
4.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Review Questions and Exercises   256
Programming Challenges   262

Chapter 5 Methods   269


5.1 Introduction to Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
void Methods and Value-Returning Methods . . . . . . . . . . . . . . . . . . . . 270
Defining a void Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Calling a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Hierarchical Method Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Using Documentation Comments with Methods . . . . . . . . . . . . . . . . . . 278
5.2 Passing Arguments to a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Argument and Parameter Data Type Compatibility . . . . . . . . . . . . . . . . 281
Parameter Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Passing Multiple Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Arguments Are Passed by Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Passing Object References to a Method . . . . . . . . . . . . . . . . . . . . . . . . . 285
Using the @param Tag in Documentation Comments . . . . . . . . . . . . . . . 288
5.3 More about Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Local Variable Lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Initializing Local Variables with Parameter Values . . . . . . . . . . . . . . . . . 292
5.4 Returning a Value from a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Defining a Value-Returning Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Calling a Value-Returning Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Using the @return Tag in Documentation Comments . . . . . . . . . . . . . . 296
Returning a boolean Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Returning a Reference to an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
5.5 Problem Solving with Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Calling Methods That Throw Exceptions . . . . . . . . . . . . . . . . . . . . . . . . 306
5.6 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Review Questions and Exercises   307
Programming Challenges   312
Contents xi

Chapter 6 A First Look at Classes   319


6.1 Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Classes: Where Objects Come From . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Classes in the Java API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Primitive Variables vs. Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
6.2 Writing a Simple Class, Step by Step . . . . . . . . . . . . . . . . . . . . . . . . . 326
Accessor and Mutator Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
The Importance of Data Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Avoiding Stale Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Showing Access Specification in UML Diagrams . . . . . . . . . . . . . . . . . . . 341
Data Type and Parameter Notation in UML Diagrams . . . . . . . . . . . . . .341
Layout of Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
6.3 Instance Fields and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
6.4 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Showing Constructors in a UML Diagram . . . . . . . . . . . . . . . . . . . . . . . 350
Uninitialized Local Reference Variables . . . . . . . . . . . . . . . . . . . . . . . . . 350
The Default Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Writing Your Own No-Arg Constructor . . . . . . . . . . . . . . . . . . . . . . . . . 351
The String Class Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
6.5 Passing Objects as Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
6.6 Overloading Methods and Constructors . . . . . . . . . . . . . . . . . . . . . . 372
The BankAccount Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Overloaded Methods Make Classes More Useful . . . . . . . . . . . . . . . . . . 380
6.7 Scope of Instance Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Shadowing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
6.8 Packages and import Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Explicit and Wildcard import Statements . . . . . . . . . . . . . . . . . . . . . . . . 382
The java.lang Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Other API Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
6.9 Focus on Object-Oriented Design: Finding the Classes
and Their Responsibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Finding the Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Identifying a Class’s Responsibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
This Is Only the Beginning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
6.10 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Review Questions and Exercises   391
Programming Challenges   396

Chapter 7 Arrays and the ArrayList Class   405


7.1 Introduction to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Accessing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Inputting and Outputting Array Contents . . . . . . . . . . . . . . . . . . . . . . . 408
Java Performs Bounds Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Watch Out for Off-by-One Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Array Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Alternate Array Declaration Notation . . . . . . . . . . . . . . . . . . . . . . . . . . 414
xii Contents

7.2 Processing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415


Array Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
The Enhanced for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Letting the User Specify an Array’s Size . . . . . . . . . . . . . . . . . . . . . . . . . 419
Reassigning Array Reference Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Copying Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
7.3 Passing Arrays as Arguments to Methods . . . . . . . . . . . . . . . . . . . . . 424
7.4 Some Useful Array Algorithms and Operations . . . . . . . . . . . . . . . . . 428
Comparing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Summing the Values in a Numeric Array . . . . . . . . . . . . . . . . . . . . . . . . 429
Getting the Average of the Values in a Numeric Array . . . . . . . . . . . . . . 430
Finding the Highest and Lowest Values in a Numeric Array . . . . . . . . . . 430
The SalesData Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Partially Filled Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Working with Arrays and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
7.5 Returning Arrays from Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
7.6 String Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Calling String Methods from an Array Element . . . . . . . . . . . . . . . . . . . 445
7.7 Arrays of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
7.8 The Sequential Search Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
7.9 Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Initializing a Two-Dimensional Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
The length Field in a Two-Dimensional Array . . . . . . . . . . . . . . . . . . . . 457
Displaying All the Elements of a Two-Dimensional Array . . . . . . . . . . . . 459
Summing All the Elements of a Two-Dimensional Array . . . . . . . . . . . . . 459
Summing the Rows of a Two-Dimensional Array . . . . . . . . . . . . . . . . . . 460
Summing the Columns of a Two-Dimensional Array . . . . . . . . . . . . . . . 460
Passing Two-Dimensional Arrays to Methods . . . . . . . . . . . . . . . . . . . . .461
Ragged Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
7.10 Arrays with Three or More Dimensions . . . . . . . . . . . . . . . . . . . . . . . 464
7.11 The Selection Sort and the Binary Search Algorithms . . . . . . . . . . . . . 465
The Selection Sort Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
The Binary Search Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
7.12 Command-Line Arguments and Variable-Length Argument Lists . . . . 470
Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Variable-Length Argument Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
7.13 The ArrayList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Creating and Using an ArrayList Object . . . . . . . . . . . . . . . . . . . . . . . 475
Using the Enhanced for Loop with an ArrayList . . . . . . . . . . . . . . . . . 476
The ArrayList Class’s toString method . . . . . . . . . . . . . . . . . . . . . . . . 477
Removing an Item from an ArrayList . . . . . . . . . . . . . . . . . . . . . . . . . .478
Inserting an Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Replacing an Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Capacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Using the Diamond Operator for Type Inference (Java 7) . . . . . . . . . . . . 482
7.14 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Contents xiii

Review Questions and Exercises   483


Programming Challenges   488

Chapter 8 A Second Look at Classes and Objects   495


8.1 Static Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
A Quick Review of Instance Fields and Instance Methods . . . . . . . . . . . . 495
Static Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Static Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
8.2 Passing Objects as Arguments to Methods . . . . . . . . . . . . . . . . . . . . 502
8.3 Returning Objects from Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
8.4 The toString Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
8.5 Writing an equals Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
8.6 Methods That Copy Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Copy Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
8.7 Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Aggregation in UML Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Security Issues with Aggregate Classes . . . . . . . . . . . . . . . . . . . . . . . . . 525
Avoid Using null References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
8.8 The this Reference Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
Using this to Overcome Shadowing . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
Using this to Call an Overloaded Constructor
from Another Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
8.9 Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
Enumerated Types Are Specialized Classes . . . . . . . . . . . . . . . . . . . . . . . 534
Switching On an Enumerated Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
8.10 Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
The finalize Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
8.11 Focus on Object-Oriented Design: Class Collaboration . . . . . . . . . . . 544
Determining Class Collaborations with CRC Cards . . . . . . . . . . . . . . . . . 547
8.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Review Questions and Exercises   549
Programming Challenges   553

Chapter 9 Text Processing and More


about Wrapper Classes   559
9.1 Introduction to Wrapper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
9.2 Character Testing and Conversion with the Character Class . . . . . . . 560
Character Case Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
9.3 More String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Searching for Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Extracting Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Methods That Return a Modified String . . . . . . . . . . . . . . . . . . . . . . . . 579
The Static valueOf Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
xiv Contents

9.4 The StringBuilder Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582


The StringBuilder Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
Other StringBuilder Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
The toString Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
9.5 Tokenizing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
9.6 Wrapper Classes for the Numeric Data Types . . . . . . . . . . . . . . . . . . 597
The Static toString Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
The toBinaryString, toHexString, and toOctalString Methods . . . . . 598
The MIN_VALUE and MAX_VALUE Constants . . . . . . . . . . . . . . . . . . . . . . . 598
Autoboxing and Unboxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
9.7 Focus on Problem Solving: The TestScoreReader Class . . . . . . . . . . . 600
9.8 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
Review Questions and Exercises   605
Programming Challenges   608

Chapter 10 Inheritance   613


10.1 What Is Inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
Generalization and Specialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
Inheritance and the “Is a” Relationship . . . . . . . . . . . . . . . . . . . . . . . . . 614
Inheritance in UML Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
The Superclass’s Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Inheritance Does Not Work in Reverse . . . . . . . . . . . . . . . . . . . . . . . . . . 625
10.2 Calling the Superclass Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . 626
When the Superclass Has No Default
or No-Arg Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
Summary of Constructor Issues in Inheritance . . . . . . . . . . . . . . . . . . . . 633
10.3 Overriding Superclass Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
Overloading versus Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
Preventing a Method from Being Overridden . . . . . . . . . . . . . . . . . . . . . 642
10.4 Protected Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Package Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
10.5 Chains of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
Class Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
10.6 The Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
10.7 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
Polymorphism and Dynamic Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
The “Is-a” Relationship Does Not Work in Reverse . . . . . . . . . . . . . . . . . 660
The instanceof Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
10.8 Abstract Classes and Abstract Methods . . . . . . . . . . . . . . . . . . . . . . . 662
Abstract Classes in UML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
10.9 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
An Interface is a Contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
Fields in Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Implementing Multiple Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Interfaces in UML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Contents xv

Default Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676


Polymorphism and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678
10.10 Anonymous Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
10.11 Functional Interfaces and Lambda Expressions . . . . . . . . . . . . . . . . . 686
10.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Review Questions and Exercises   692
Programming Challenges   698

Chapter 11 Exceptions and Advanced File I/O   703


11.1 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703
Exception Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
Handling an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705
Retrieving the Default Error Message . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
Polymorphic References to Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 712
Using Multiple catch Clauses to Handle Multiple Exceptions . . . . . . . . . 712
The finally Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
The Stack Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
Handling Multiple Exceptions with One catch Clause (Java 7) . . . . . . . . 723
When an Exception Is Not Caught . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725
Checked and Unchecked Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . 726
11.2 Throwing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
Creating Your Own Exception Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 730
Using the @exception Tag in Documentation Comments . . . . . . . . . . . . 733
11.3 Advanced Topics: Binary Files, Random Access Files,
and Object Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
Binary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
Random Access Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741
Object Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746
Serializing Aggregate Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750
11.4 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
Review Questions and Exercises   751
Programming Challenges   757

Chapter 12 A First Look at GUI Applications   761


12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761
The JFC, AWT, and Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762
Event-Driven Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
The javax.swing and java.awt Packages . . . . . . . . . . . . . . . . . . . . . . . 764
12.2 Creating Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
Using Inheritance to Extend the JFrame Class . . . . . . . . . . . . . . . . . . . . 767
Equipping GUI Classes with a main Method . . . . . . . . . . . . . . . . . . . . . . 769
Adding Components to a Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771
Handling Events with Action Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . 777
xvi Contents

Writing an Event Listener for the KiloConverter Class . . . . . . . . . . . . . . 779


Background and Foreground Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784
The ActionEvent Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788
12.3 Layout Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
Adding a Layout Manager to a Container . . . . . . . . . . . . . . . . . . . . . . . 794
The FlowLayout Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794
The BorderLayout Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
The GridLayout Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
12.4 Radio Buttons and Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
Radio Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
12.5 Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821
12.6 Focus on Problem Solving: Extending Classes from JPanel . . . . . . . . 824
The Brandi’s Bagel House Application . . . . . . . . . . . . . . . . . . . . . . . . . . 824
The GreetingPanel Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
The BagelPanel Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
The ToppingPanel Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828
The CoffeePanel Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 832
12.7 Splash Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836
12.8 Using Console Output to Debug a GUI Application . . . . . . . . . . . . . . 837
12.9 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842
Review Questions and Exercises   842
Programming Challenges   845

Chapter 13 Advanced GUI Applications   849


13.1 The Swing and AWT Class Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . 849
13.2 Read-Only Text Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850
13.3 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852
Selection Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852
Responding to List Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853
Retrieving the Selected Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854
Placing a Border around a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858
Adding a Scroll Bar to a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858
Adding Items to an Existing JList Component . . . . . . . . . . . . . . . . . . . 863
Multiple Selection Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
13.4 Combo Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 868
Retrieving the Selected Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869
13.5 Displaying Images in Labels and Buttons . . . . . . . . . . . . . . . . . . . . . . 874
13.6 Mnemonics and Tool Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880
Mnemonics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880
Tool Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882
13.7 File Choosers and Color Choosers . . . . . . . . . . . . . . . . . . . . . . . . . . . 882
File Choosers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883
Color Choosers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885
Contents xvii

13.8 Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 886


13.9 More about Text Components: Text Areas and Fonts . . . . . . . . . . . . . 895
Text Areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895
Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898
13.10 Sliders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899
13.11 Look and Feel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 904
13.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906
Review Questions and Exercises   907
Programming Challenges   912

Chapter 14 Applets and More   917


14.1 Introduction to Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 917
14.2 A Brief Introduction to HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
Hypertext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
Markup Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
Document Structure Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
Text Formatting Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922
Creating Breaks in Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924
Inserting Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927
14.3 Creating Applets with Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928
Running an Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
Handling Events in an Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932
14.4 Using AWT for Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
14.5 Drawing Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
The XY Coordinate System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
Graphics Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
The repaint Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956
Drawing on Panels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957
14.6 Handling Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963
Handling Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963
14.7 Timer Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 973
14.8 Playing Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977
Using an AudioClip Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978
Playing Audio in an Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 981
14.9 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 982
Review Questions and Exercises   982
Programming Challenges   988

Chapter 15 Creating GUI Applications with JavaFX


and Scene Builder   991
15.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 991
Event-Driven Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993
15.2 Scene Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993
xviii Contents

15.3 Using Scene Builder to Create JavaFX Applications . . . . . . . . . . . . . . 995


Starting Scene Builder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 996
The Scene Builder Main Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997
15.4 Writing the Application Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009
The Main Application Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1010
The Controller Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1012
Using the Sample Controller Skeleton . . . . . . . . . . . . . . . . . . . . . . . . . 1017
Summary of Creating a JavaFX Application . . . . . . . . . . . . . . . . . . . . . 1018
15.5 RadioButtons and CheckBoxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1019
RadioButtons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1019
Determining in Code Whether a RadioButton Is Selected . . . . . . . . . . . 1021
Responding to RadioButton Events . . . . . . . . . . . . . . . . . . . . . . . . . . . 1024
CheckBoxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1027
Determining in Code Whether a CheckBox Is Selected . . . . . . . . . . . . . 1028
Responding to CheckBox Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1030
15.6 Displaying Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033
Displaying an Image with Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1034
15.7 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1038
Review Questions and Exercises   1038
Programming Challenges   1042

Chapter 16 Recursion   1047


16.1 Introduction to Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1047
16.2 Solving Problems with Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 1050
Direct and Indirect Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054
16.3 Examples of Recursive Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1055
Summing a Range of Array Elements with Recursion . . . . . . . . . . . . . . 1055
Drawing Concentric Circles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1056
The Fibonacci Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1058
Finding the Greatest Common Divisor . . . . . . . . . . . . . . . . . . . . . . . . . 1060
16.4 A Recursive Binary Search Method . . . . . . . . . . . . . . . . . . . . . . . . . . 1061
16.5 The Towers of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1064
16.6 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1069
Review Questions and Exercises   1069
Programming Challenges   1072

Chapter 17 Databases   1075


17.1 Introduction to Database Management Systems . . . . . . . . . . . . . . . 1075
JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1076
SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1077
Using a DBMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1077
Java DB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1078
Creating the CoffeeDB Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1078
Contents xix

Connecting to the CoffeeDB Database . . . . . . . . . . . . . . . . . . . . . . . . 1078


Connecting to a Password-Protected Database . . . . . . . . . . . . . . . . . . 1080
17.2 Tables, Rows, and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1081
Column Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083
Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1083
17.3 Introduction to the SQL SELECT Statement . . . . . . . . . . . . . . . . . . . 1084
Passing an SQL Statement to the DBMS . . . . . . . . . . . . . . . . . . . . . . . 1086
Specifying Search Criteria with the WHERE Clause . . . . . . . . . . . . . . . . . 1096
Sorting the Results of a SELECT Query . . . . . . . . . . . . . . . . . . . . . . . . . 1102
Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1103
17.4 Inserting Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1106
Inserting Rows with JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1108
17.5 Updating and Deleting Existing Rows . . . . . . . . . . . . . . . . . . . . . . . 1110
Updating Rows with JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1111
Deleting Rows with the DELETE Statement . . . . . . . . . . . . . . . . . . . . . . 1115
Deleting Rows with JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1115
17.6 Creating and Deleting Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1119
Removing a Table with the DROP TABLE Statement . . . . . . . . . . . . . . . 1122
17.7 Creating a New Database with JDBC . . . . . . . . . . . . . . . . . . . . . . . . 1122
17.8 Scrollable Result Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1124
17.9 Result Set Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1125
17.10 Displaying Query Results in a JTable . . . . . . . . . . . . . . . . . . . . . . . 1129
17.11 Relational Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1139
Joining Data from Multiple Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1142
An Order Entry System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1143
17.12 Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1161
Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1161
Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1162
17.13 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1163
Review Questions and Exercises   1163
Programming Challenges   1168

Index    1171
Companion Website:
Appendix A Working with Records and Random Access Files
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 More about JOptionPane Dialog Boxes
Appendix J Answers to Checkpoints
Appendix K Answers to Odd-Numbered Review Questions
xx Contents

Appendix L Getting Started with Alice


Appendix M Configuring JavaDB
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
Location of Videonotes in the Text
VideoNote

Chapter 1 Compiling and Running a Java Program, p. 14


Using an IDE, p. 15
Your First Java Program, p. 25

Chapter 2 Displaying Console Output, p. 33


Declaring Variables, p. 39
Simple Math Expressions, p. 55
The Miles-per-Gallon Problem, p. 106

Chapter 3 The if Statement, p. 111


The if-else Statement, p. 121
The if-else-if Statement, p. 132
The Time Calculator Problem, p. 185

Chapter 4 The while Loop, p. 193


The Pennies for Pay Problem, p. 263

Chapter 5 Passing Arguments to a Method, p. 279


Returning a Value from a Method, p. 293
The Retail Price Calculator Problem, p. 312

Chapter 6 Writing Classes and Creating Objects, p. 327


Initializing an Object with a Constructor, p. 348
The Personal Information Class Problem, p. 397

Chapter 7 Accessing Array Elements in a Loop, p. 409


Passing an Array to a Method, p. 424
The Charge Account Validation Problem, p. 489

Chapter 8 Returning Objects from Methods, p. 505


Aggregation, p. 517
The BankAccount, Class Copy Constructor Problem, p. 554

Chapter 9 The Sentence Capitalizer Problem, p. 608

Chapter 10 Inheritance, p. 613


Polymorphism, p. 657
The Employee and Productionworker Classes Problem, p. 698

Chapter 11 Handling Exceptions, p. 703


The Exception Project Problem, p. 759

(continued on the next page)


LOCATION OF VIDEONOTES IN THE TEXT (continued)
VideoNote
Chapter 12 Creating a Simple GUI Application, p. 764
Handling Events, p. 777
The Monthly Sales Tax Problem, p. 846

Chapter 13 The JList Component, p. 852


The JComboBox Component, p. 868
The Image Viewer Problem, p. 912

Chapter 14 Creating an Applet, p. 929


The House Applet Problem, p. 988

Chapter 15
Using Scene Builder to Create the Kilometer Converter GUI, p. 998
Learning More About the Main Application Class, p. 1010
Writing the Main Application Class For the Kilometer Converter GUI, p. 1011
Learning More About the Controller Class, p. 1013
Registering the Controller Class with the Application’s GUI, p. 1014
JavaFX RadioButtons, p. 1019
JavaFX CheckBoxes, p. 1027
The Retail Price Calculator Problem, p. 1042

Chapter 16 Reducing a Problem with Recursion, p. 1051


The Recursive Power Problem, p. 1073

Chapter 17 Displaying Query Results in a JTable, p. 1129


The Customer Inserter Problem, p. 1168
Preface

W elcome to Starting Out with Java: From Control Structures through Objects,
Sixth Edition. This book is intended for a one-semester or a two-quarter CS1
course. Although it is written for students with no prior programming background, even
experienced students will benefit from its depth of detail.

Control Structures First, Then Objects


This text first introduces the student to the fundamentals of data types, input and output,
control structures, methods, and objects created from standard library classes.
Next, the student learns to use arrays of primitive types and reference types. After this, the
student progresses through more advanced topics, such as inheritance, polymorphism, the
creation and management of packages, GUI applications, recursion, and database program-
ming. From early in the book, applications are documented with javadoc comments. As the
student progresses through the text, new javadoc tags are covered and demonstrated.
As with all the books in the Starting Out With . . . series, the hallmark of this text is its clear,
friendly, and easy-to-understand writing. In addition, it is rich in example programs that are
concise and practical.

Changes in This Edition


This book’s pedagogy, organization, and clear writing style remain the same as in the previous
edition. Many improvements have been made, which are summarized here:
· A New Chapter on JavaFX: New to this edition is Chapter 15 Creating GUI
Applications with JavaFX and Scene Builder. JavaFX is the next generation toolkit
for creating GUIs and graphical applications in Java, and is bundled with Java 8.
This new chapter introduces the student to the JavaFX library, and shows how to use
Scene Builder (a free download from Oracle) to visually design GUIs. The chapter is
written in such a way that it is independent from the existing chapters on Swing and
AWT. The instructor can choose to skip the Swing and AWT chapters and go straight
to JavaFX, or cover all of the GUI chapters.

xxiii
xxiv Preface

· String.format Is Used Instead of DecimalFormat: In previous editions, the DecimalFormat


class was used to format strings for GUI output. In this edition, the String.format
method is used instead. With String.format, the student can use the same format
specifiers and flags that were learned with the System.out.printf method.

· StringTokenizer Is No Longer Used: In previous editions, the StringTokenizer class


was introduced as a way to tokenize strings. In this edition, all string tokenizing is
done with the String.split method.

· Introduction of @Override annotation: Chapter 10 now introduces the use of


@Override annotation, and explains how it can prevent subtle errors.

· A New Section on Anonymous Inner Classes: Chapter 10 now has a new section that
introduces anonymous inner classes.

· The Introduction to Interfaces Has Been Improved: The introductory material on


interfaces in Chapter 10 has been revised for greater clarity.

· Default Methods: In this edition, Chapter 10 provides new material on default meth-
ods in interfaces, a new feature in Java 8.

· Functional Interfaces and Lambda Expressions: Java 8 introduces functional inter-


faces and lambda expressions, and in this edition, Chapter 10 has a new section on
these topics. The new material gives a detailed, stepped-out explanation of lambda
expressions, and discusses how they can be used to instantiate objects of anonymous
classes that implement functional interfaces.

· New Programming Problems: Several new motivational programming problems have


been added to many of the chapters.

Organization of the Text


The text teaches Java step-by-step. Each chapter covers a major set of topics and builds
knowledge as students progress through the book. Although the chapters can be easily
taught in their existing sequence, there is some flexibility. Figure P-1 shows chapter
­dependencies. Each box represents a chapter or a group of chapters. An arrow points from
a chapter to the chapter that must be previously covered.

Brief Overview of Each Chapter


Chapter 1: Introduction to Computers and Java. This chapter provides an introduc-
tion to the field of computer science and covers the fundamentals of hardware, software,
and programming languages. The elements of a program, such as key words, variables,
operators, and punctuation, are discussed by examining a simple program. An overview of
entering source code, compiling, and executing a program is presented. A brief history of
Java is also given.
Preface xxv

Figure P-1 Chapter dependencies

Chapters 1–6 (Cover in Order)


Java Fundamentals

Depend On
Chapter 7
Chapter 17
Arrays and the
Databases
ArrayList Class

*Some examples in
Chapter 17 use GUIs,
which are introduced
in Chapter 12.
Chapter 9 Chapter 8 Chapter 12
Chapter 16
Text Processing and A Second Look at A First Look at GUI
Recursion
Wrapper Classes Classes and Objects Applications

*Some examples in
Chapter 16 are applets,
which are introduced
Depends On in Chapter 14. Depends On

Chapter 10 Chapter 14
Depends On
Inheritance Applets and More

Depends On Depends On Depends On Depends On

Chapter 15 Chapter 11
Creating GUI Applications
with JavaFX and Scene Exceptions and
Builder Advanced File I/O Chapter 13
Advanced GUI
Applications

Chapter 2: Java Fundamentals. This chapter gets students started in Java by introduc-
ing data types, identifiers, variable declarations, constants, comments, program output, and
simple arithmetic operations. The conventions of programming style are also introduced.
Students learn to read console input with the Scanner class and with dialog boxes using
JOptionPane.
xxvi Preface

Chapter 3: Decision Structures. In this chapter students explore relational operators


and relational expressions and are shown how to control the flow of a program with the if,
if-else, and if-else-if statements. Nested if statements, logical operators, the conditional
operator, and the switch statement are also covered. The chapter discusses how to compare
String objects with the equals, compareTo, equalsIgnoreCase, and compareToIgnoreCase
methods. Formatting numeric output with the System.out.printf method and the
String.format method is discussed.

Chapter 4: Loops and Files. This chapter covers Java’s repetition control structures. The
while loop, do-while loop, and for loop are taught, along with common uses for these
devices. Counters, accumulators, running totals, sentinels, and other application-related
topics are discussed. Simple file operations for reading and writing text files are included.

Chapter 5: Methods. In this chapter students learn how to write void methods, value-
returning methods, and methods that do and do not accept arguments. The concept of
functional decomposition is discussed.

Chapter 6: A First Look at Classes. This chapter introduces students to designing


classes for the purpose of instantiating objects. Students learn about class fields and meth-
ods, and UML diagrams are introduced as a design tool. Then constructors and overloading
are discussed. A BankAccount class is presented as a case study, and a section on object-
oriented design is included. This section leads the students through the process of identify-
ing classes and their responsibilities within a problem domain. There is also a section that
briefly explains packages and the import statement.

Chapter 7: Arrays and the ArrayList Class. In this chapter students learn to create
and work with single and multi-dimensional arrays. Numerous array-processing tech-
niques are demonstrated, such as summing the elements in an array, finding the highest and
lowest values, and sequentially searching an array. Other topics, including ragged arrays
and variable-length arguments (varargs), are also discussed. The ArrayList class is intro-
duced, and Java’s generic types are briefly discussed and demonstrated.

Chapter 8: A Second Look at Classes and Objects. This chapter shows students how
to write classes with added capabilities. Static methods and fields, interaction between
objects, passing objects as arguments, and returning objects from methods are discussed.
Aggregation and the “has a” relationship is covered, as well as enumerated types. A section
on object-oriented design shows how to use CRC cards to determine the collaborations
among classes.

Chapter 9: Text Processing and More about Wrapper Classes. This chapter dis-
cusses the numeric and Character wrapper classes. Methods for converting numbers to
strings, testing the case of characters, and converting the case of characters are covered.
Autoboxing and unboxing are also discussed. More String class methods are covered,
including using the split method to tokenize strings. The chapter also covers the
StringBuilder and StringTokenizer classes.
Preface xxvii

Chapter 10: Inheritance. The study of classes continues in this chapter with the subjects
of inheritance and polymorphism. The topics covered include superclasses, subclasses, how
constructors work in inheritance, method overriding, polymorphism and dynamic binding,
protected and package access, class hierarchies, abstract classes, abstract methods, anony-
mous inner classes, interfaces, and lambda expressions.

Chapter 11: Exceptions and Advanced File I/O. In this chapter students learn to
develop enhanced error trapping techniques using exceptions. Handling exceptions is cov-
ered, as well as developing and throwing custom exceptions. The chapter discusses advanced
techniques for working with sequential access, random access, text, and binary files.

Chapter 12: A First Look at GUI Applications. This chapter presents the basics of
developing GUI applications with Swing. Fundamental Swing components and the basic
concepts of event-driven programming are covered.

Chapter 13: Advanced GUI Applications. This chapter continues the study of GUI
application development with Swing. More advanced components, menu systems, and
look-and-feel are covered.

Chapter 14: Applets and More. In this chapter students apply their knowledge of GUI
development to the creation of applets. In addition to using Swing applet classes, AWT
classes are discussed for portability. Drawing simple graphical shapes is discussed.

Chapter 15: Creating GUI Applications with JavaFX and Scene Builder. This
chapter introduces JavaFX, which is the next generation library for creating graphical
applications in Java. This chapter also shows how to use Scene Builder, a free screen designer
from Oracle, to visually design GUIs. This chapter is written in such a way that it is inde-
pendent from the existing chapters on Swing and AWT. You can choose to skip chapters 12,
13, and 14, and go straight to Chapter 15, or cover all of the GUI chapters.

Chapter 16: Recursion. This chapter presents recursion as a problem-solving technique.


Numerous examples of recursive methods are demonstrated.

Chapter 17: Databases. This chapter introduces the student to database programming.
The basic concepts of database management systems and SQL are first introduced. Then the
student learns to use JDBC to write database applications in Java. Relational data is cov-
ered, and numerous example programs are presented throughout the chapter.

Features of the Text


Concept Statements. Each major section of the text starts with a concept statement that
concisely summarizes the focus of the section.
xxviii Preface

Example Programs. The text has an abundant number of complete and partial example
programs, each designed to highlight the current topic. In most cases the programs are prac-
tical, real-world examples.

Program Output. Each example program is followed by a sample of its output, which
shows students how the program functions.

Checkpoints. Checkpoints, highlighted by the checkmark icon, appear at intervals through-


out each chapter. They are designed to check students’ knowledge soon after learning a new
topic. Answers for all Checkpoint questions are provided in Appendix K, which can be
downloaded from the book’s resource page at www.pearsonhighered.com/cs-resources.

Note: Notes appear at several places throughout the text. They are short explanations
of interesting or often misunderstood points relevant to the topic at hand.

Tip: Tips advise the student on the best techniques for approaching different program-
ming problems and appear regularly throughout the text.

Warning! Warnings caution students about certain Java features, programming tech-
niques, or practices that can lead to malfunctioning programs or lost data.

In the Spotlight. Many of the chapters provide an In the Spotlight


section that presents a programming problem, along with detailed, step-
by-step analysis showing the student how to solve it.

VideoNotes. A series of videos, developed specifically for this book, are available at www.
pearsonhighered.com/gaddis. Icons appear throughout the text alerting the student to ­videos
VideoNote
about specific topics.

Case Studies. Case studies that simulate real-world business applications are intro-
duced throughout the text and are provided on the book’s resource page at www.pearson-
highered.com/gaddis.

Common Errors to Avoid. Each chapter provides a list of common errors and explana-
tions of how to avoid them.

Review Questions and Exercises. Each chapter presents a thorough and diverse set of
review questions and exercises. They include Multiple Choice and True/False, Find the
Error, Algorithm Workbench, and Short Answer.
Random documents with unrelated
content Scribd suggests to you:
"They serve who stand and wait." It is easier to be busy than to be
quiet. When Peter was "young," he went whither he would; but
when he got "old," he went whither he would not. What a contrast
between the young, restless, ardent, energetic Peter, going whither
he would, and the old, matured, subdued, experienced Peter, going
whither he would not! What a mercy to have the will broken!—to be
able to say from the heart, "What Thou wilt—as Thou wilt—where
Thou wilt—when Thou wilt"—"not my will, but Thine, O Lord, be
done"!
"Follow Me!" Precious words! May they be engraved on our hearts,
beloved reader! Then shall we be steady in our course and effective
in our service. We shall not be distracted or unhinged by the
thoughts and opinions of men. It may happen that we shall get very
few to understand us or to sympathize with us—few to approve or
appreciate our work. It matters not. The Master knows all about it. If
a master tells one of his servants distinctly to go and do a certain
thing, or occupy a certain post, it is his business to go and do that
thing, or occupy that post, no matter what his fellow-servants may
think. They may tell him that he ought to be somewhere else, or to
do something else. A proper servant will heed them not; he knows
his master's mind, and has to do his master's work.
Would it were more thus with all the Lord's servants! Would that we
all knew more distinctly, and carried out more decidedly, the Master's
will respecting us! Peter had his path, and John had his. James had
his work, and Paul had his. So it was of old: the Gershonite had his
work, and the Merarite had his; and if the one had interfered with
the other, the work would not have been done. The tabernacle was
carried forward, or set up, by each man doing his own proper work.
Thus it is in this our day. God has varied workmen in His house and
in His vineyard; and the original terms of service are that the Holy
Spirit divideth to every one as He will. He has quarrymen, stone-
squarers, masons, and builders. Are all quarrymen? Surely not. But
each has his work to do, and the building is carried forward by each
one doing his own appointed work. Should a quarryman despise a
builder, or a builder look down with contempt upon a quarryman?
Assuredly not. The Master wants them both; and whenever the one
would interfere with the other (as, alas, we are apt to do), the
faithful correcting word falls on the ear, "What is that to thee? follow
thou Me."
JERICHO AND ACHOR
OR
PRIVILEGE AND RESPONSIBILITY
(Read Joshua vi., vii.)

The Christian reader will do well to turn, first of all, to the two
chapters named above, and give them a careful reading. They
furnish a very striking and impressive record of the double effect of
God's presence with His people. In chapter vi. we are taught that
the divine Presence ensured victory over the power of the enemy. In
chapter vii. we learn that the divine Presence demanded judgment
upon evil in the bosom of the congregation. The ruins of Jericho
demonstrate the one; the great heap of stones in the valley of Achor
attests the other.
Now, these two things must never be separated. We see them
vividly illustrated in every page of the history of God's people, both
in the Old and in the New Testament. The self-same Presence that
secures victory demands holiness. Let us never forget this. Yea, let
us keep it ever in the remembrance of our hearts. It has an
individual as well as a collective application. If we are to walk with
God, or, rather, if He is to walk with us, we must judge and put away
everything inconsistent with His holy presence. He cannot sanction
unjudged evil in His people. He can pardon, heal, restore, and bless;
but He is intolerant of evil. "Our God is a consuming fire." "The time
is come that judgment must begin at the house of God."
Should the thought of this discourage or depress any true-hearted
child of God, or servant of Christ? Certainly not. It should neither
discourage nor depress, but it should make us very watchful over
our hearts, very careful as to our ways, our habits of thought and
conversation. We have nothing to fear while God is with us, but He
cannot possibly sanction evil in His people; and every true lover of
holiness will heartily bless Him for this. Could we possibly desire it to
be otherwise? Would we wish the standard of holiness to be lowered
at all? God forbid. All those who love His name can give thanks at
the remembrance of His holiness, and rejoice in the truth that
holiness becometh His house forever? "Be ye holy, for I am holy." It
is not by any means on the pharisaic principle, wrapped up in the
words, "Stand by thyself; I am holier than thou." Thank God, it is
not this. It is not a question of what we are, but of what He is. Our
character and conduct are to be formed by the truth of what God is.
Marvelous grace! Precious privilege!
God must have His people like Himself. If they forget this, He will
surely remind them of it. If He, in infinite grace, links His name and
His glory with us, it behooves us to look well to our habits and ways,
lest we bring any reproach on that name. Is this legal bondage? Nay,
it is the holiest liberty. We may rest perfectly assured of this, that we
are never further removed from legality than when treading that
path of true holiness which becomes all those who bear the name of
Christ. "Having therefore these promises, dearly beloved, let us
cleanse ourselves from all filthiness of the flesh and spirit, perfecting
holiness in the fear of God."
This great truth holds good at all times. We see it in the ruins of
Jericho. We read it in the valley of Achor. What was it that caused
the frowning walls and towering bulwarks of Jericho to fall down at
the sound of rams' horns and the shout of the people? The presence
of Jehovah. And it mattered not if it was but the city of Jericho or
the whole land of Canaan, before that invincible Presence.
But what means the humiliating defeat before the insignificant city of
Ai? How comes it to pass that the hosts of Israel, so recently
triumphant at Jericho, have to flee ignominiously before a mere
handful of men at Ai? Ah, the answer tells a sorrowful tale! Here it
is; let us harken to it, and ponder it in the deepest depths of our
heart. Let us seek to profit by it. Let us be solemnly warned by it. It
has been written for our admonition. The Holy Ghost has taken the
pains to record it for our learning. Woe be to the one who turns a
deaf ear to the warning voice!
"But the children of Israel committed a trespass in the accursed
thing: for Achan, the son of Carmi, the son of Zabdi, the son of
Zerah, of the tribe of Judah, took of the accursed thing: and the
anger of the Lord was kindled against"—whom? Achan merely? or
his household, or his family, or his tribe? Nay, but "against the
children of Israel"! The whole assembly was involved in the evil. How
was this? The divine presence imparted a unity to the whole
assembly; it bound them all together in such a manner as to involve
all in the sin of the one. It was one assembly, and hence it was
impossible for any one to take independent ground. The sin of each
was the sin of all, because God was in their midst, and He could not
countenance unjudged evil. The whole congregation was involved,
and had to clear itself of the evil ere Jehovah could lead it on to
victory. Had He allowed them to triumph at Ai, it would have argued
that He was indifferent to the sin of His people, and that He could
give the sanction of His presence to "an accursed thing," which were
simply blasphemy against His holy name.
"And Joshua sent men from Jericho to Ai, which is beside Beth-aven,
on the east side of Bethel, and spake unto them, saying, Go up and
view the country. And the men went up and viewed Ai. And they
returned to Joshua, and said unto him, Let not all the people go up;
but let about two or three thousand men go up and smite Ai;"—
more easily said than done;—"and make not all the people to labor
thither; for they are but few"—yet quite too many for Israel with an
Achan in the camp. "So there went up thither of the people about
three thousand men; and they fled before the men of Ai. And the
men of Ai smote of them about thirty and six men: for they chased
them from before the gate even unto Shebarim, and smote them in
the going down: wherefore the hearts of the people melted, and
became as water.
"And Joshua rent his clothes, and fell to the earth upon his face
before the ark of the Lord until the eventide, he and the elders of
Israel, and put dust upon their heads."
Here was a strange and unlooked-for experience. "And Joshua said,
Alas, O Lord God, wherefore hast Thou at all brought this people
over Jordan, to deliver us into the hand of the Amorites, to destroy
us? would to God we had been content, and dwelt on the other side
Jordan! O Lord, what shall I say when Israel turneth their backs
before their enemies? For the Canaanites and all the inhabitants of
the land shall hear of it, and shall environ us round, and cut off our
name from the earth: and what wilt Thou do unto Thy great name?"
Joshua, that beloved and honored servant of God, did not see, did
not understand, that it was the very glory of that "great name"
which necessitated the defeat at Ai, just as it had achieved the
victory at Jericho. But there were other elements in that glory
besides power. There was holiness, and that holiness rendered it
impossible for Him to lend the sanction of His presence where there
was unjudged evil. Joshua should have concluded that there was
something wrong in the condition of the people. He ought to have
known that the hindrance was with Israel, and not with Jehovah.
The same grace that had given them victory at Jericho would have
given it at Ai, if things were right. But, alas, they were not right; and
hence defeat, and not victory, was the order of the day. How could
there be victory with an accursed thing in the camp? Impossible!
Israel must judge the evil, or Jehovah must judge Israel. To have
given them a victory at Ai would have been a reproach and a
dishonor to the One whose name was called upon them. The divine
presence absolutely demanded judgment upon the evil; and until
that was executed, further progress in the conquest of Canaan was
out of the question. "Be ye clean that bear the vessels of the Lord."
"Holiness becometh Thy house, O Lord, for ever."
"And the Lord said unto Joshua, Get thee up; wherefore liest thou
thus upon thy face? Israel hath sinned,"—not merely Achan,—"and
they have also transgressed My covenant which I commanded them:
for they have even taken of the accursed thing, and have also
stolen, and dissembled also, and they have put it even among their
own stuff. Therefore the children of Israel could not stand before
their enemies, but turned their backs before their enemies, because
they were accursed: neither will I be with you any more, except ye
destroy the accursed from among you."
This is peculiarly solemn. The whole congregation is held responsible
for the evil. "A little leaven leaveneth the whole lump." Unbelief may
inquire how all are involved in the sin of one; but the word of God
definitely settles the question—"Israel hath sinned"—"they have
taken"—"they have stolen"—"they have dissembled." The assembly
was one; one in privilege, one in responsibility. As such, the sin of
one was the sin of all, and all were called upon to clear themselves
thoroughly by putting away the accursed thing from among them.
There was not a single member of that large congregation who was
not affected by Achan's sin. This may seem strange to mere nature,
but such is the solemn and weighty truth of God. It was true in the
assembly of Israel of old, and assuredly it is not less true in the
Church of God now. No one could take independent ground in the
assembly of Israel; how much less can he take it in the Church of
God? There were over six hundred thousand people who, to speak
after the manner of men, were wholly ignorant of what Achan had
done; and yet God's word to Joshua was, "Israel hath sinned." All
were involved; all were affected; and all had to clear themselves ere
Jehovah could again lead them on to victory. The presence of God in
the midst of the Assembly formed the unity of all; and the presence
of the Holy Ghost in the Church of God, the body of Christ now on
the earth, binds all up in one divine, indissoluble unity. Hence, to talk
of independency is to deny the very foundation-truth of the Church
of God, and to prove beyond all question that we understand neither
its nature nor its unity as set forth on the page of inspiration.
And if evil creeps into an assembly, how is it to be met? Here it is:
"Up, sanctify the people, and say, Sanctify yourselves against to-
morrow: for thus saith the Lord God of Israel, There is an accursed
thing in the midst of thee, O Israel: thou canst not stand before
thine enemies, until ye take away the accursed thing from among
you." Were they one in privilege? Were they one in the enjoyment of
the glory and strength which the divine Presence secured? Were
they one in the splendid triumph at Jericho? Who would deny all
this? Who would wish to deny it? Why, then, seek to question their
oneness in responsibility—their oneness in respect to the evil in their
midst, and all its humbling consequences? Surely, if there was unity
in anything, there was unity in everything. If Jehovah was the God
of Israel, He was the God of all, the God of each; and this grand and
glorious fact was the solid basis both of their high privileges and
their holy responsibilities. How could evil exist in such an assembly,
and a single member be unaffected by it? How could there be an
accursed thing in their very midst, and a single member not be
defiled? Impossible. We may reason and argue about it until the
tongue cleaves to the roof of the mouth, but all the reasoning and
argument in the world cannot touch the truth of God, and that truth
declares that "a little leaven leaveneth the whole lump."
But how is the evil to be discovered? The presence of God reveals it.
The self-same power that had leveled the walls of Jericho, detected,
revealed and judged the sin of Achan. It was the double effect of the
same blessed Presence, and Israel was called to share in the one as
well as in the other. To attempt to separate the two is folly,
ignorance, or wickedness. It cannot be done, and ought not to be
attempted.

PART II.

We must ever remember, that, in the history of God's ways with His
people, privilege and responsibility are intimately bound up together.
To talk of privilege, or think of enjoying it, while neglecting the
responsibility, is a gross delusion. No true lover of holiness could
think for a moment of separating them; nay, he must ever delight in
strengthening and perpetuating the precious link.
Thus, for example, in Israel's case, who could estimate aright the
high privilege of having Jehovah dwelling in their midst? By day and
by night, there He was, to guide and guard, shield and shelter them;
to meet their every need, to give them bread from heaven, and
bring them forth water out of the rock. His presence was a
safeguard against every foe; no weapon formed against them could
prosper; not a dog might move his tongue against them; they were
at once invulnerable and invincible; with God in their midst they had
nothing whatever to fear. He charged Himself with all their wants,
whether great or small. He looked after their garments, that they
might not wax old; He looked after their feet, that they might not
swell; He covered them with the shield of His favor, so that no arrow
might touch them; He stood between them and every foe, and flung
back in the enemy's face every accusation.
Thus much as to the high privilege. But mark the corresponding and
connected responsibility. See how both are indissolubly bound up
together in the following weighty words: "For the Lord thy God
walketh in the midst of thy camp, to deliver thee, and to give up
thine enemies before thee: therefore shall thy camp be holy; that He
see no unclean thing in thee, and turn away from thee."
Precious privilege! Solemn responsibility! Who would dare to dissolve
the hallowed connection? Had Jehovah deigned to come down into
their midst, and walk with them, and tabernacle among them? Had
He, in infinite grace, condescended to be their traveling companion?
Was He there for the exigence of every hour? Yes, blessed be His
name. If so, then what did His presence demand? We have seen
something of what His presence secured; but what did it demand?
Holiness! Israel's whole conduct was to be regulated by the great
fact of the divine Presence in their midst. Not only their great public
national institutions, but their most private habits, were to be
brought under the controlling influence of Jehovah's presence with
them. He regulated what they were to eat, what they were to wear,
how they were to carry themselves in all the scenes, circumstances
and relationships of daily life. By night and by day, sleeping and
waking, sitting in the house or walking by the way, alone or in
company, He looked after them. Nothing was to be allowed in any
wise inconsistent with the holiness and purity which became the
presence of the Holy One of Israel.
Was all this irksome? Were the privileges irksome? Was it irksome to
be fed, clothed, guided, guarded and cared for in every possible
way? Was it irksome to repose beneath the overshadowing wings of
the God of Israel? Surely not. Why, then, should it be irksome to
keep their persons, their habits and their dwellings clean? Must not
every true heart, every upright mind, every tender conscience,
accept as thoroughly the responsibility which the divine Presence
necessarily involves as the privileges which it infallibly secures? Yea,
rather must we not rank the very responsibility itself among our
richest and rarest privileges? Unquestionably. Every true lover of
holiness will esteem it a signal mercy—a very high order of blessing
—to walk in company with One whose presence detects and
condemns every form of evil. "Thy testimonies are very sure;
holiness becometh Thy house, O Lord, for ever."
The foregoing train of thought will enable us in some measure to
understand the history of Achan, in Josh. vii.—a history solemn and
impressive in the highest degree—a history which utters in our
hearing, with deepest emphasis, words which our careless hearts are
only too ready to forget, "God is greatly to be feared in the assembly
of His saints, and to be had in reverence of all them that are about
Him." Had Achan remembered this, it would have taught him the
holy necessity of nipping in the very bud the covetousness of his
heart, and thus have spared the whole assembly the humiliating
defeat at Ai, and all the consequent sorrow and discipline. How
terrible to think of one man, for the sake of a little personal gain,
which at best could last but for a moment, plunging a whole
congregation into the deepest trouble! and, what was worse than all,
dishonoring and grieving that blessed One who had deigned, in His
infinite goodness, to take up His abode in their midst! How well it
would be if each one of us, when tempted to commit any secret sin,
would just pause and ask himself the question, "How can I do this
thing, and grieve the Holy Spirit of God who dwells in me, and bring
leaven into the assembly of God's people?" We ought to remember
that our private walk has a direct bearing upon all the members of
the body. We are either helping or hindering the blessing of all. We
are none of us independent atoms; we are members of a body
incorporated by the presence of the Holy Ghost; and if we are
walking in a loose, carnal, worldly, self-indulgent spirit, we are
grieving the Spirit, and injuring all the members. "But God hath
tempered the body together ... that there should be no schism in the
body; but that the members should have the same care one for
another. And whether one member suffer, all the members suffer
with it; or one member be honored, all the members rejoice with it"
(1 Cor. xii. 24-26).
It may seem hard, to some, to grasp this great practical truth—hard
to see how our private condition and conduct can affect our fellow-
members; but the simple and obvious fact is, we must either admit
this, or maintain the unscriptural and foolish notion that each
Christian is an independent person, having no connection with the
whole body of believers. If he be a member of a body, all the
members of which are bound together, and linked with the Head by
the personal indwelling of the Holy Ghost, then, verily, it follows that
his walk and ways affect all his fellow-members just as really as, if
any member of the human body suffers, all the other members feel
it. If there is anything wrong with the hand, the foot feels it. How is
this? Because the head feels it. The communication, in every
instance, is with the head first, and from the head to the members.
Now, though Achan was not a member of a body, but merely of a
congregation, yet we see how his private conduct affected the whole
assembly. This is all the more striking, inasmuch as the great truth
of the one body was not unfolded, and could not be until—
redemption being a grand, accomplished fact—the Head took His
seat on the throne of God, and sent down the Holy Ghost to form
the body, and link it, by His personal presence and indwelling, to the
Head in heaven. If the secret sin of Achan affected every member of
the congregation of Israel, how much more (may we not say?) doth
the secret sin of any member of the body of Christ affect all the
members thereof!
Let us never forget this weighty truth. May we keep it ever in the
remembrance of our hearts, that so we may see the urgent need of
a careful, tender, holy walk; that we may not dishonor our glorious
Head, grieve the blessed indwelling Spirit, or injure the feeblest
member of that body of which, by the sovereign grace of God and
the precious blood of Christ, we form a part.
But we must proceed with our subject, and in so doing call the
special attention of the reader to the way in which the sin of Achan
was traced home to him. It is all most solemn. He had little idea
whose eye was resting upon him when he was carrying on his secret
wickedness. He would, no doubt, think himself all right, and very
successful, when he had the money and the garment safely hidden
in his tent. Fatal, guilty, wretched treasure! Unhappy man! How
dreadful is the love of money! How terrible is the blinding power of
sin! It hardens the heart, deadens the conscience, darkens the
understanding, ruins the soul, and in the case before us brought
defeat and disaster upon the whole people of which he formed a
part.
"And the Lord said unto Joshua, Get thee up; wherefore liest thou
thus upon thy face?" There is a time for lying on the face, and there
is a time for standing on our feet; a time for devout prostration, and
a time for decided action. The instructed soul will know the time for
each. "Israel hath sinned, and they have also transgressed My
covenant which I commanded them; for they have even taken of the
accursed thing, and have also stolen, and dissembled also, and they
have put it even among their own stuff. Therefore the children of
Israel could not stand before their enemies, but turned their backs
before their enemies, because they were accursed: neither will I be
with you any more, except ye destroy the accursed from among you.
Up, sanctify the people, and say, Sanctify yourselves against to-
morrow: for thus saith the Lord God of Israel, There is an accursed
thing in the midst of thee, O Israel: thou canst not stand before
thine enemies, until ye take away the accursed thing from among
you."
How peculiarly solemn is all this! how very arresting! how soul-
subduing! God's people—those who bear His name, and profess to
hold His truth, who stand identified with Him in this world—must be
holy. He cannot lend the sanction of His presence to that which is
unholy or impure. Those who enjoy the high privilege of being
associated with God are solemnly responsible to keep themselves
unspotted from the world, else He must take down the rod of
discipline and do His strange work in their midst. "Be ye clean that
bear the vessels of the Lord."
"Thou canst not stand before thine enemies, until ye take away the
accursed thing from among you. In the morning therefore ye shall
be brought according to your tribes: and it shall be, that the tribe
which the Lord taketh shall come according to the families thereof;
and the family which the Lord shall take shall come by households;
and the household which the Lord shall take shall come man by
man."
Ah, this was coming to close quarters! The sinner might seek to
persuade himself that discovery was impossible; he might cherish
the fond hope of escaping amid the many thousands of Israel.
Miserable delusion! He might be sure his sin would find him out. The
self-same Presence that secured individual blessing, secured with
equal fidelity the detection of the most secret individual sin. Escape
was impossible. If Jehovah was in the midst of His people to lay
Jericho in ruins at their feet, He was there also to lay bare, in its
deepest roots, the sin of the congregation, and to bring forth the
sinner from his hiding-place to bear the penalty of his wickedness.
How searching are God's ways! First, the twelve tribes are
summoned, that the transgressor might be manifested. Then, one
tribe is fixed upon. Nearer still! the family is fixed upon! And yet
nearer! the very household is actually singled out; and, last of all,
"man by man!" Thus, out of six hundred thousand people, the all-
searching eye of Jehovah reads the sinner through and through, and
marks him off before the assembled thousands of Israel.
"And it shall be that he that is taken with the accursed thing shall be
burnt with fire, he and all that he hath: because he hath
transgressed the covenant of the Lord, and because he hath
wrought folly in Israel.
"So Joshua rose up early in the morning, and brought Israel by their
tribes; and the tribe of Judah was taken: and he brought the family
of Judah; and he took the family of the Zarhites: and he brought the
family of the Zarhites man by man; and Zabdi was taken: and he
brought his household man by man; and Achan, the son of Carmi,
the son of Zabdi, the son of Zerah, of the tribe of Judah, was taken."
"Our God is a consuming fire." He cannot tolerate evil in the ways of
His people. This accounts for the solemn scene before us. The
natural mind may reason about all this—it may marvel why the
taking of a little money and a garment from amid the spoils of a
doomed city should involve such awful consequences and entail such
a severe punishment. But the natural mind is incapable of
understanding the ways of God. And may we not ask the objector,
How could God sanction evil in His people? How could He go on with
it? What was to be done with it? If He was about to execute
judgment upon the seven nations of Canaan, could He possibly be
indifferent to sin in His people? Most assuredly not. His word is, "You
only have I known of all the families of the earth; therefore will I
punish you for your iniquities." The very fact of His taking them into
relationship with Himself was the ground of His dealing with them in
holy discipline.
It is the height of folly for man to reason about the severity of divine
judgment, or the apparent lack of proportion between the sin and
the punishment. All such reasoning is false and impious. What was it
that brought in all the misery, the sorrow, the desolation, the
sickness, pain, and death—all the untold horrors of the last six
thousand years? What was the source of it all? Just the one little act
—as man would call it—of eating a bit of fruit? But this little act was
that terrible thing called sin—yea, rebellion—against God! And what
was needed to atone for this? How was it to be met? What stands
over against it as the only adequate expression of the judgment of a
holy God?—What? The burning in the valley of Achor? Nay. The
everlasting burnings of hell? Nay; something far deeper and more
solemn still. What? The cross of the Son of God! The awful mystery
of the death of Christ!—that terrible cry, "My God, My God, why hast
Thou forsaken Me?" Let men remember this, and cease to reason.

PART III.

It is always well for the Christian to be able to give a calm and


decided answer to the objection which infidelity is sure to offer to
the actings of divine government. The answer is this: "Shall not the
Judge of all the earth do right?" If the creature is to be allowed to
judge the Creator, there is an end of all government in the vast
universe of God. Hence, when we hear men daring to pronounce
judgment upon the ways of God, and undertaking to decide what is
or what is not fit for God to do, this grand preliminary question
invariably suggests itself, "Who is to be judge?" Is man to judge
God? or is God to judge man? If the former, there is no God at all;
and if the latter, then man has to bow his head in reverent silence,
and own his utter ignorance and folly.
The fact is, if man could fathom the government of God, he would
no longer be man, but God. What contemptible folly, therefore, for a
poor, shallow, ignorant, short-sighted mortal to attempt to
pronounce an opinion upon the profound mysteries of divine
government! His opinion is not only utterly worthless, but, in the
judgment of every truly pious mind, positively impious and
blasphemous—a daring insult offered to the throne, to the nature
and to the character of God, for which he will, most assuredly, have
to answer before the judgment-seat of Christ, unless he repent and
find pardon through the blood of the cross.
The foregoing line of thought has suggested itself in connection with
the solemn scene in the valley of Achor. The unbelieving mind may
be disposed to start an objection on the ground of the apparent
severity of the judgment; to institute a comparison between the
offence and the punishment; to call in question the equity of Achan's
children being involved in their father's sin.
To all this we simply reply, "Are we competent to judge?" If any one
thinks he is, it is tantamount to saying that God is not fit to govern
the world, but should give place to man. This is the real root of the
whole matter. Infidelity wants to get rid of God altogether, and set
up man in His place. If God is to be God, then, most certainly, His
ways, the actings of His government, the mysteries of His
providence, His purposes, His counsels and His judgments must lie
far beyond the range of the greatest human or angelic mind. Neither
angel, man nor devil can comprehend Deity. Let men own this, and
hush into eternal silence their puny, ignorant and contemptible
reasonings. Let them take up the language of Job when his eyes
were opened: "Then Job answered the Lord, and said, I know that
Thou canst do everything, and that no thought can be withholden
from Thee. Who is he that hideth counsel without knowledge?
therefore have I uttered that I understood not; things too wonderful
for me, which I knew not. Hear, I beseech Thee, and I will speak: I
will demand of Thee, and declare Thou unto Me. I have heard of
Thee by the hearing of the ear; but now mine eye seeth Thee:
wherefore I abhor myself, and repent in dust and ashes." When the
soul gets into this attitude, there is an end of all infidel questions.
Till then there is little use in discussion.
Let us now turn for a few moments to contemplate the solemn
scene in the valley of Achor; and let us remember that "whatsoever
things were written aforetime, were written for our learning." May
we learn to watch with holy jealousy the incipient workings of evil in
our hearts. It is on these men ought to sit in judgment, and not on
the pure and perfect actings of divine government.
Joshua's address to Achan is solemn, weighty, and powerful: "My
son, give, I pray thee, glory to the Lord God of Israel, and make
confession unto Him; and tell me now what thou hast done; hide it
not from me."
Here is the all-important matter. "Give glory to Jehovah, God of
Israel." All hinges upon this. The Lord's glory is the one perfect
standard by which all is to be judged—the perfect gauge by which
everything is to be measured—the perfect touchstone by which all is
to be tried. The one great question for the people of God in all ages
and in all dispensations is this: What is suited to the glory of God? In
comparison with this, all other questions are less than secondary. It
is not a question of what is suitable to us, or what we can tolerate or
agree with. This is a very minor consideration indeed. What we have
ever to look to, and think of, and provide for, is the glory of God. We
have to ask ourselves the question, in reference to everything that
comes before us, "Will this comport with the glory of God?" If not,
let us, by His grace, fling it aside.
Well would it have been for Achan had he thought of this when his
eye rested on the cursed treasure! What misery it would have saved
him! What sorrow and trouble it would have saved his brethren! But,
alas, alas, people forget all this when lust dims the eye and vanity
and folly possess the heart! and onward they go until the heavy
judgment of a holy, sin-hating God overtakes them. And then,
forsooth, men presume to comment upon such judgment as
unworthy of a gracious and beneficent Being. Ignorant presumption!
They would fain have a god of their own imagination, one like
themselves, who can make light of sin and tolerate all sorts of evil.
The God of the Bible, the God of Christianity, the God of the Cross,
the God and Father of our Lord Jesus Christ, does not suit such
infidel reasoners. Their deep heart-utterance to Him is, "Depart from
us, for we desire not the knowledge of Thy ways."
"And Achan said, Indeed I have sinned against the Lord God of
Israel, and thus and thus have I done: when I saw among the spoils
a goodly Babylonish garment, and two hundred shekels of silver, and
a wedge of gold of fifty shekels weight, then I coveted them, and
took them; and, behold, they are hid in the earth in the midst of my
tent, and the silver under it."
Here the dark, defiling stream is traced up to its source in the heart
of this unhappy man. Oh, how little did he think whose eye was
resting on him during the entire progress of this melancholy and
disastrous affair! He thought of but one thing, namely, the
gratification of his covetousness. He saw, he coveted, he took, he
hid; and there, no doubt, he thought the matter would end. He
would have his treasure, and no one would be the wiser.
But, ah, the eye of Jehovah, the God of Israel, was upon him—that
holy eye, from which no secret thing is hidden, which penetrates the
depths of the human heart, and takes in at a glance all the hidden
springs of human action. Yes, God saw it all, and He would make
Israel see it, and Achan also. Hence the lamentable defeat at Ai, and
all that followed.
How perfectly solemn!—the whole assembly involved in shameful
defeat and disaster—Joshua and the elders of Israel, with rent
garments and dust upon their heads, prostrate on their faces from
morning till evening! And then, the divine challenge and rebuke! the
solemn muster of the hosts of Israel, tribe by tribe, family by family,
household by household, man by man!
And why all this? Just to trace the evil to its source, bring it out, and
have it judged in the sight of every creature. All created intelligence
must be made to see and confess that the throne of God can have
no fellowship with evil. The same power that had leveled the walls of
Jericho, and executed judgment upon its guilty inhabitants, was to
be manifested in detecting Achan's sin, and in evoking from the very
depths of his convicted heart the confession of his terrible guilt. He,
in common with all his brethren, had heard Jehovah's solemn
charge, "And ye, in any wise keep yourselves from the accursed
thing, lest ye make yourselves accursed, when ye take of the
accursed thing, and make"—not merely any one individual's tent, but
—"the camp of Israel a curse, and trouble it. But all the silver, and
gold, and vessels of brass and iron, are consecrated unto the Lord:
they shall come into the treasury of the Lord."
All this was plain enough. No one could mistake it. It only needed an
attentive ear and an obedient heart. It was as plain as the
commandment delivered to Adam and Eve amid the bowers of Eden.
But Achan, like Adam, transgressed the plain and positive command.
Instead of hiding it in his heart, that he might not sin against God,
he trampled it under his feet, that he might gratify his sinful desires.
He fixed his covetous gaze upon the accursed thing, in itself nothing
but a wretched pile of dust, but, through Satan's power and Achan's
erring heart, turned into an occasion of sin, shame, and sorrow.
O reader, how sad, how sorrowful, how terrible a thing it is to allow
the poor heart to go after the wretched things of this world! What
are they all worth? If we could have all the garments that were ever
made in Babylon; all the gold and silver that ever issued from the
mines of Peru, California, and Australia; all the pearls and diamonds
that ever glittered on the kings, princes and nobles of this world—
could they give us one hour's true happiness? Could they send a
single ray of heavenly light into the soul? Could they impart to us
one moment's pure, spiritual enjoyment? Not they. In themselves
they are but perishable dust, and when used of Satan a positive
curse, misery, and degradation. Not all the riches and material
comforts which this world could offer are worth one hour's holy
communion with our heavenly Father and our precious Saviour. Why
should we covet this world's wretched wealth? Our God will supply
all our need according to His riches in glory by Christ Jesus. Is not
this enough? Why should we put ourselves within the range of
Satan's power by setting our hearts upon the riches, honors, or
pleasures, of a world which is ruled by the archenemy of God and of
our souls? How well it would have been for Achan had he rested
content with what the God of Israel had given him! How happy he
might have been had he been satisfied with the furniture of his tent,
the smile of Jehovah, and the answer of a good conscience!
But he was not; and hence the appalling scene in the valley of Achor,
the record of which is enough to strike terror into the stoutest heart.
"So Joshua sent messengers, and they ran unto the tent; and,
behold, it was hid in his tent, and the silver under it. And they took
them out of the midst of the tent, and brought them unto Joshua,
and unto all the children of Israel, and laid them out before the Lord.
And Joshua, and all Israel with him, took Achan the son of Zerah,
and the silver, and the garment, and the wedge of gold, and his
sons, and his daughters, and his oxen, and his asses, and his sheep,
and his tent, and all that he had: and they brought them unto the
valley of Achor. And Joshua said, Why hast thou troubled us? the
Lord shall trouble thee this day. And all Israel stoned him with
stones, and burned them with fire, after they had stoned them with
stones. And they raised over him a great heap of stones unto this
day. So the Lord turned from the fierceness of His anger. Wherefore
the name of that place was called, The valley of Achor (that is,
trouble), unto this day" (Josh. vii. 19-26).
How deeply solemn is all this! What a warning note it sounds in our
ears! Let us not attempt, under the false influence of one-sided
notions of grace, to turn aside the holy edge of such a passage of
Scripture. Let us read with earnest attention the inscription on that
awful monument in the valley of Achor. What is it? "God is greatly to
be feared in the assembly of His saints, and to be had in reverence
of all them that are about Him." And again, "If any man defile the
temple of God, him will God destroy." And further, "Our God is a
consuming fire."
Weighty, solemn, searching words these!—much needed, surely, in
these days of flippant, easy-going profession, when the doctrines of
grace are so much on our lips, but the fruits of righteousness so little
seen in our lives. May we learn from them the urgent need of
watchfulness over our hearts, and over our private life, that evil may
be judged and nipped in the bud, so that it may not bring forth its
sad, shameful and sorrowful fruit in our practical career, to the gross
dishonor of the Lord and the grievous sorrow of those with whom
we are linked in the bonds of fellowship.

PART IV.

There is a very interesting allusion to "the valley of Achor" in Hos. ii.,


at which we may just glance in passing, though it does not connect
itself with the special line of truth which we have had before us in
this series of papers.
Jehovah, in speaking of Israel, by His prophet says: "Therefore,
behold, I will allure her, and bring her into the wilderness, and speak
comfortably unto her. And I will give her her vineyards from thence,
and the valley of Achor for a door of hope; and she shall sing there,
as in the days of her youth, and as in the day when she came up out
of the land of Egypt" (vers. 14, 15).
What touching grace shines in these words! "The valley of Achor"—
the place of "trouble"—the place of deep sorrow and shame—the
place of humiliation and judgment—the place where the fire of
Jehovah's righteous wrath consumed the sin of His people—there
shall be "a door of hope" for Israel by and by; there, too, she shall
sing as in the days of her youth. How wonderful to hear of songs of
praise in the valley of Achor! What glorious triumphs of grace! What
a bright and blessed future for Israel!
"It shall be at that day, saith the Lord, that thou shalt call me Ishi
(my husband), and shalt call me no more Baali (my lord). For I will
take away the names of Baalim out of her mouth, and they shall no
more be remembered by their name.... And I will betroth thee unto
Me for ever; yea, I will betroth thee unto Me in righteousness, and in
judgment, and in loving-kindness, and in mercies: I will even betroth
thee unto Me in faithfulness: and thou shalt know the Lord."
From this digression to "the valley of Achor" in the future, we now
return to our special theme; and in so doing we shall ask the reader
to turn with us, for a few moments, to the opening chapters of the
Acts. Here we find the same grand results of the presence of God in
the midst of His people as we have seen in the opening of the book
of Joshua; only in a much more glorious manner, as we might
expect.
On the day of Pentecost, God the Holy Ghost came down to form the
Assembly, and take up His abode therein. This great and glorious
fact was grounded on the accomplishment of the work of
atonement, as attested by the resurrection of our Lord Jesus Christ,
and His glorification at the right hand of God.
We cannot attempt to unfold this truth in all its bearings in this brief
article; we merely call the reader's attention to the two practical
points which have been before us—namely, the privilege and
responsibility connected with the Lord's presence in the midst of His
people. If He was there to bless,—as He most surely was,—He was
also, and quite as surely, there to judge. The two things go together,
and we must not attempt to separate them.
And first, then, we see the effect and blessings of the divine
Presence in the Assembly: "And all that believed were together, and
had all things common; and sold their possessions and goods, and
parted them to all, as every man had need." The blessed effect of
the realized presence of the Holy Ghost was to bind their hearts
together in a holy and loving fellowship; to cause them to let go
earthly things, and to lead them to merge their personal interests in
the common good.
Precious fruits! Would that we saw more of them! No doubt times
are changed; but God is not changed, and the effect of His realized
presence is not changed. True, we are not in Acts ii. Pentecostal
times are passed away; Christendom has lapsed in complete failure;
the professing Church has hopelessly fallen. All this is sadly true; but
Christ our Head abides, in all His living power and unchangeable
grace. "The foundation of God standeth sure"—as sure, as safe and
as solid to-day as it was on the day of Pentecost. No change here,
blessed be God; hence we may say, with all possible confidence, that
where His presence is realized, even though it be only by "two or
three" gathered to the name of Jesus, there the same lovely fruits
will be found. Hearts will be knit together; earthly things will be
surrendered; personal interests will be merged. It is not a question
of throwing our goods into a common heap, but of the grace which
once took that special form, and which at all times would lead us,
not merely to surrender our possessions, but ourselves, for the good
of others.
It is a very grave mistake indeed for any one to say, or to think, that
because we are not in Pentecostal times we cannot count on the
presence of God with us in the path of holy obedience to His will.
Such a thought should be judged as sheer unbelief. We are certainly
shorn of many of the Pentecostal gifts, but we are not bereft of the
Giver. The blessed Comforter abides with us; and it is our happy
privilege to be in a position in which we can enjoy His presence and
ministry.
The thing is to be in that position; not merely to say we are in it, to
boast of being in it, but really to be in it. We may well apply here the
pointed question of the blessed apostle, "What doth it profit, my
brethren, though a man say" he is on divine ground, if he be not
really there? Assuredly it profits nothing.
But let us not forget that although we are not in Acts ii., but in the
second epistle to Timothy; although we are not in the refreshing
scenes of Pentecost, but in the "perilous times" of "the last days,"
yet the Lord is with those "who call on Him out of a pure heart," and
His presence is all we want. Let us only trust Him, use Him, lean
upon Him. Let us see to it that we are in a position in which we can
count on His presence—a position of entire separation from all that
He judges to be "iniquity"—from the "dishonorable vessels" in "the
great house," and from all those who, having a form of godliness,
deny the power thereof.
These, we may rest assured, are the absolutely essential conditions
on which the divine Presence can be realized by any company of
Christians. We may come together, and form ourselves into an
assembly; we may profess to be on divine ground; we may call
ourselves the assembly of God; we may appropriate to ourselves all
those passages of Scripture which only apply to those who are really
gathered by the Holy Ghost in the name of Jesus; but if the essential
conditions are not there; if we are not "calling on the Lord out of a
pure heart"; if we are mixed up with "iniquity"; if we are associated
with "dishonorable vessels"; if we are walking hand in hand with
lifeless professors who deny in practice the power of godliness—
what then? Can we expect to realize the Lord's presence? As well
might Israel have expected it with Achan in the camp. It cannot be.
In order to reach divine results, there must be divine conditions. To
look for the former without the latter is vanity, folly, and wicked
presumption.
Let not the reader mistake our meaning. We are not now treating, or
even touching, the great question of the soul's salvation. This,
precious and important as it is to all whom it may concern, is not at
all our subject in this series of papers on "Jericho and Achor." We are
dealing with the solemn and weighty question of the privilege and
responsibility of those who profess to be the Lord's people, gathered
to His name; and we are specially anxious to impress upon the mind
of the reader that, notwithstanding the hopeless ruin of the
professing Church, its utter failure in its responsibility to Christ as His
witness and light-bearer in the world, yet it is the happy privilege of
"two or three" to be gathered in His name, apart from all the evil
and error around, owning our common sin and failure, feeling our
weakness, and looking to Him to be with us and bless us according
to the unchangeable love of His heart.
Now, to those thus gathered, there is no limit whatever to the
measure of blessing which our ever gracious and faithful Lord can
bestow. "He has the seven spirits of God, and the seven stars"—the
fulness of spiritual power, ministerial gift and authority for His
Church. Such is His style and title in addressing the church at Sardis,
which, we believe, prophetically sets before us the history of
Protestantism.
It is not said, as in the address to Ephesus, that He "holds the seven
stars in His right hand." There is a grave difference as to this; and it
is our bounden duty to recognize both the difference and the cause.
When the Church began, on the day of Pentecost, and during the
days of the apostles, Christ, the Head, not only possessed all
spiritual gift, power and authority for His Church, but was owned as
the actual administrator thereof. He held the stars in His right hand.
There was no such thing known or thought of as human authority in
the assembly of God. Christ was owned as Head and Lord. He had
received the gifts, and He dispensed them according to His
sovereign will.
Thus it should ever be. But, alas, man has intruded upon the
hallowed sphere of Christ's authority. He presumes to meddle in the
appointment of ministry in the Church of God. Without so much as a
single atom of divine authority, without any ability whatsoever to
impart the necessary gift for ministry, he nevertheless takes upon
himself the solemn responsibility of calling, appointing, or ordaining
to the ministry in the Church of God. As well might the writer of
these lines undertake to appoint a man as an admiral in Her
Majesty's fleet, or a general in her army, as for any man, or body of
men, to appoint a man to minister in the Church of God. It is a
daring usurpation of divine authority. None can impart ministerial
gift, and none can appoint to any branch of the ministry but Christ,
the Church's Head and Lord; and all who undertake to do so will
have to account to Him for so doing.
It may be that many who thus act, and many more who sanction or
are identified with such acting, are not aware of what they are
doing; and our God is gracious and merciful in bearing with our
feebleness and ignorance. All this is blessedly true; but as to the
principle of human authority in the Church of God, it is utterly false,
and should be rejected with holy decision by every one who loves,
reverences and adores the great Head of the Church and Lord of the
Assembly, who, blessed be His name, still has the seven Spirits of
God and the seven stars. He has them now just as positively as in
apostolic times; and all who take their true place, the place of self-
judgment and humiliation; all who truly own our common sin and
failure, our departure from first love, first principles; all who really, in
true humility of mind, look to Christ alone for all they want; all who,
in real earnestness of heart and godly sincerity, bow to His word and
confess His name—all such will assuredly prove the reality of His
presence; they will find Him amply sufficient for all their need. They
can count on Him for the supply of all ministerial gift, and for the
maintenance of all godly order in their public reunions.
True, they will feel—must feel—that they are not in the days of Acts
ii., but in the days of 2 Timothy. Yet Christ is sufficient for these, as
He was for those. The difficulties are great, but His resources are
infinite. It were folly to deny that there are difficulties; but it is sinful
unbelief to question the all-sufficiency of our ever-gracious and
faithful Lord. He has promised to be with His people right on to the
end. But He cannot sanction hollow pretension, or proud
assumption. He looks for reality, for truth in the inward parts. He will
have us in our right place, owning our true condition. There He can
meet us according to His infinite fulness, and according to the
eternal stability of that grace which reigns through righteousness
unto eternal life.
But oh, let us never forget that our God delights in uprightness of
heart and integrity of purpose. He will never fail a trusting heart; but
He must be trusted really. It will not do to speak of trusting Him
while in reality we are leaning on our own appliances and
arrangements. Here is precisely where we so sadly fail. We do not
leave room for Him to act in our midst. We do not leave the platform
clear for Him. Thus we are robbed, and that to an extent of which
we have little idea, of the blessed manifestation of His presence and
grace in our assemblies. His Spirit is quenched and hindered, and we
are left to feel our barrenness and poverty, when we might be
rejoicing in the fulness of His love and in the power of His ministry.
It is utterly impossible that He can ever fail those who, owning the
truth of the condition, earnestly look to Him. He cannot deny
Himself; and He can never say to His people that they have
reckoned too largely on Him.
It is not that we are to look for any special display of power in our
midst, anything that might attract public attention, or make a noise
in the world. There are no tongues, no gifts of healing, no miracles,
no extraordinary manifestations of angelic action on our behalf.
Neither are we to look for anything similar to the case of Ananias
and Sapphira—the sudden and awful execution of divine judgment,
striking terror into the hearts of all, both inside and outside the
assembly.
Such things are not to be looked for now. They would not comport
with the present condition of things in the Church of God. No doubt
our Lord Christ has all power in heaven and on earth, and He could
display that power now just as He did in Pentecostal times, if it so
pleased Him. But He does not so act, and we can readily understand
the reason. It is our place to walk softly, humbly, tenderly. We have
sinned, and failed, and departed from the holy authority of the word
of God. We must ever bear this in mind, and be content with a very
low and retired place. It would ill become us to seek a name or a
position in the earth. We cannot possibly be too little in our own
eyes.
But at the same time we can, if in our right place, and in a right
spirit, fully count on the presence of Jesus with us; and we may rest
assured that where He is—where His most gracious presence is felt
—there we may look for the most precious results, both in the way
of binding our hearts together in true brotherly love, in causing us to
sit loose to all earthly possessions and earthly ties, in leading us
forth in grace and kindness toward all men, and also in putting away
from among us all who would defile the assembly by unsound
doctrine or unholy morals.
P. S.—It is of the utmost importance for the Christian reader to bear
in mind that, whatever be the condition of the professing Church, it
is his privilege to enjoy as high communion and to tread as high a
path of individual devotedness as ever was known in the very
brightest days of the Church's history. We must never draw a plea
from the condition of things around us for lowering the standard of
individual holiness and devotedness. There is no excuse for
continuing a single hour in connection with anything that will not
stand the test of Holy Scripture.
True, we feel the condition of things—cannot but feel it: would we
felt it more! But it is one thing to feel it, and go through it with
Christ, and another thing to sink under it and go on with the evil, or
give up in despair.
May the Lord, in His infinite grace, produce in the hearts of all His
people a more profound and influential sense of their privileges and
responsibilities, both individually and collectively, that thus there may
be a truer and brighter testimony for His name, and a devoted band
of worshipers, workers and witnesses, gathered out to wait for His
coming!
What raised the wondrous thought?
Or who did it suggest?
"That we, the Church, to glory brought,
Should WITH the Son be blest."

O God, the thought was Thine!


(Thine only it could be,)
Fruit of the wisdom, love divine,
Peculiar unto Thee.

For, sure, no other mind,


For thoughts so bold, so free,
Greatness or strength, could ever find;
Thine only it could be.

The motives, too, Thine own,


The plan, the counsel, Thine!—
Made for Thy Son, bone of His bone
In glory bright to shine.

O God, with great delight


Thy wondrous thought we see,
Upon His throne, in glory bright,
The bride of Christ shall be.

Sealed with the Holy Ghost,


We triumph in that love,
Thy wondrous thought has made our boast,
"Glory WITH Christ above."
THE THREE APPEARINGS.
(Heb. ix. 24-28.)
"For Christ is not entered into the holy places made with hands,
which are the figures of the true; but into heaven itself, now to
appear in the presence of God for us: nor yet that He should offer
Himself often, as the high priest entereth into the holy place every
year with blood of others; for then must He often have suffered
since the foundation of the world: but now once in the end of the
ages hath He appeared to put away sin by the sacrifice of Himself.
And as it is appointed unto men once to die, but after this the
judgment: so Christ was once offered to bear the sins of many; and
unto them that look for Him shall He appear the second time apart
from sin unto salvation."[2]
The foregoing passage sets before us three great facts in the life of
our Lord Jesus Christ. It speaks of what we may venture to call three
distinct appearings, namely, an appearing in the past; an appearing
in the present; and an appearing in the future. He hath appeared in
this world to do a certain work; He doth appear in heaven to carry
on a certain ministry; and He shall appear in glory. The first is
Atonement; the second is Advocacy; the third is the Advent. And
first, then, let us dwell for a few moments on

THE ATONEMENT,

which is here presented in its two grand aspects, first, Godward; and
secondly, usward. The apostle declares that Christ hath appeared "to
put away sin;" and also "to bear the sins of many." This is a
distinction of the utmost importance, and one not sufficiently
understood or attended to, Christ has put away sin by the sacrifice
of Himself. He has glorified God in reference to the question of sin in
its very broadest aspect. This He has done altogether irrespective of
the question of persons or the forgiveness of the sins of individuals.
Even though every soul, from the days of Adam down to the very
last generation, were to reject the proffered mercy of God, yet would
it hold good that the atoning death of Christ had put away sin—had
destroyed the power of Satan—had perfectly glorified God, and laid
the deep and solid foundation on which all the divine counsels and
purposes can rest for ever.
It is to this fact that the Baptist refers in these memorable words,
"Behold the Lamb of God, which taketh away the sin of the world"
(John i. 29). The Lamb of God has wrought a work in virtue of which
every trace of sin shall be obliterated from the creation of God. He
has perfectly vindicated God in the very midst of a scene in which
He had been so grossly dishonored, in which His character had been
traduced and His majesty insulted. He came to do this at all cost,
even at the sacrifice of Himself. He sacrificed Himself in order to
maintain, in view of heaven, earth and hell, the glory of God. He has
wrought a work by the which God is infinitely more glorified than if
sin had not entered at all. God shall reap a richer harvest by far in
the fields of redemption than ever He could have reaped in the fields
of an unfallen creation.
It is well that the reader should deeply ponder this glorious aspect of
the atoning death of Christ. We are apt to think that the very highest
view we can take of the cross is that which involves the question of
our forgiveness and salvation. This is a grave mistake. That question
is divinely settled, as we shall seek to show; for the less is always
included in the greater. But let us remember that our side of the
atonement is the less, God's side of it the greater. It was infinitely
more important that God should be glorified than that we should be
saved. Both ends have been gained, blessed be God, and gained by
one and the same work, the precious atonement of Christ; but we
must never forget that the glory of God is of far greater moment
than the salvation of men; and further, that we never can have so
clear a sense of the latter as when we see it flowing from the former.
It is when we see that God has been perfectly and for ever glorified
in the death of Christ, that we can really enter into the divine
perfectness of our salvation. In point of fact, both are so intimately
bound up together that they cannot be separated; but still God's
part in the Cross of Christ must ever get its own proper
preeminence. The glory of God was ever uppermost in the devoted
heart of the Lord Jesus Christ. For this He lived, for this He died. He
came into this world for the express purpose of glorifying God, and
from this great and holy object He never swerved a moment from
the manger to the cross. True it is—blessedly true—that in carrying
out this object He has perfectly met our case; but the divine glory
ruled Him in life and in death.
Now it is on the ground of atonement, looked at in this its higher
aspect, that God has been dealing with the world in patient grace,
mercy and forbearance for well nigh six thousand years. He sends
His rain and His sunbeams upon the evil and upon the good, upon
the just and the unjust. It is in virtue of the atonement of Christ—
though despised and rejected—that the infidel and the atheist live,
and enjoy God's daily mercies; yea, the very breath that they spend
in opposing the revelation and denying the existence of God they
owe to Him in whom they live, move and have their being. We speak
not here, by any means, of the forgiveness of sins, or of the soul's
salvation. This is another question altogether, and to it we shall refer
presently. But, looking at man in reference to his life in this world,
and looking at the world in which he lives, it is the Cross which
forms the basis of God's merciful dealing with both the one and the
other.
Furthermore, it is on the ground of the atonement of Christ, in this
same aspect of it, that the evangelist can go forth "into all the world,
and preach glad tidings to every creature." He can declare the
blessed truth that God has been glorified as to sin—His claims
satisfied—His majesty vindicated—His law magnified—His attributes
harmonized. He can proclaim the precious message that God can
now be just and yet the justifier of any poor ungodly sinner that
believes in Jesus. There is no hindrance, no barrier of any kind
whatsoever. The preacher of the gospel is not to be cramped by any
dogmas of theology. He has to do with the large, loving heart of
God, which, in virtue of atonement, can flow forth to every creature
beneath the canopy of heaven. He can say to each and to all—and
say it without reserve—"Come!" Nay, more, he is bound to "beseech"
them to come. "We pray you in Christ's stead, be ye reconciled to
God." Such is the proper language of the evangelist, the herald of
the cross, the ambassador of Christ. He knows no less a range than
the wide, wide world; and he is called to drop his message into the
ear of every creature under heaven.
And why? Because "Christ hath put away sin by the sacrifice of
Himself." He has, by His most precious death, changed completely
the ground of God's dealings with man and with the world, so that,
instead of having to deal with them on the ground of sin, He can
deal on the ground of atonement.
Finally, it is in virtue of the atonement, in this broad and lofty aspect,
that every vestige of sin, and every trace of the serpent shall be
obliterated from the wide universe of God. Then shall be seen the
full force of that passage above referred to, "The Lamb of God,
which taketh away the sin of the world."
Thus much as to what we may call the primary aspect of the atoning
death of Christ—an aspect which cannot be too thoughtfully studied.
A clear understanding of this weighty point would tend to remove a
great deal of difficulty and misunderstanding in reference to the full
and free preaching of the gospel. Many of the Lord's honored
servants find themselves hindered in the presentation of the glad
tidings of salvation, simply because they do not see this wide aspect
of the atonement. They confine the death of Christ merely to its
bearing upon the sins of God's elect; and they therefore deem it
wrong to preach the gospel to all, or to invite, yea to beseech and
entreat, all to come.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like