0% found this document useful (0 votes)
9 views61 pages

Learning Java Through Games 1st Edition Stanchev Lubomir

The document promotes a collection of ebooks focused on learning Java programming through games, offering various titles for download at textbookfull.com. It highlights the book 'Learning Java Through Games' by Lubomir Stanchev, which teaches Java programming concepts using game development as a practical approach. The text covers both basic and advanced programming techniques while emphasizing good software practices.

Uploaded by

logrodeliu
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
0% found this document useful (0 votes)
9 views61 pages

Learning Java Through Games 1st Edition Stanchev Lubomir

The document promotes a collection of ebooks focused on learning Java programming through games, offering various titles for download at textbookfull.com. It highlights the book 'Learning Java Through Games' by Lubomir Stanchev, which teaches Java programming concepts using game development as a practical approach. The text covers both basic and advanced programming techniques while emphasizing good software practices.

Uploaded by

logrodeliu
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/ 61

Explore the full ebook collection and download it now at textbookfull.

com

Learning Java Through Games 1st Edition Stanchev


Lubomir

https://textbookfull.com/product/learning-java-through-
games-1st-edition-stanchev-lubomir/

OR CLICK HERE

DOWLOAD EBOOK

Browse and Get More Ebook Downloads Instantly at https://textbookfull.com


Click here to visit textbookfull.com and download textbook now
Your digital treasures (PDF, ePub, MOBI) await
Download instantly and pick your perfect format...

Read anywhere, anytime, on any device!

Learning Java With Games Chong-Wei Xu

https://textbookfull.com/product/learning-java-with-games-chong-wei-
xu/

textbookfull.com

Games To Teach or Games To Learn Unlocking the Power of


Digital Game Based Learning Through Performance 1st
Edition Yam San Chee
https://textbookfull.com/product/games-to-teach-or-games-to-learn-
unlocking-the-power-of-digital-game-based-learning-through-
performance-1st-edition-yam-san-chee/
textbookfull.com

Java Deep Learning Essentials 1st Edition Sugomori Yusuke

https://textbookfull.com/product/java-deep-learning-essentials-1st-
edition-sugomori-yusuke/

textbookfull.com

Pro Java 9 Games Development: Leveraging the JavaFX APIs


1st Edition Wallace Jackson (Auth.)

https://textbookfull.com/product/pro-java-9-games-development-
leveraging-the-javafx-apis-1st-edition-wallace-jackson-auth/

textbookfull.com
Polymorphism in Java: Methods and polymorphic algorithms
applied to computer games 1st Edition Privitera

https://textbookfull.com/product/polymorphism-in-java-methods-and-
polymorphic-algorithms-applied-to-computer-games-1st-edition-
privitera/
textbookfull.com

Learning Java Beginning programming with java for dummies


First Edition John Bach

https://textbookfull.com/product/learning-java-beginning-programming-
with-java-for-dummies-first-edition-john-bach/

textbookfull.com

Learning Java Functional Programming 1st Edition Reese


Richard M

https://textbookfull.com/product/learning-java-functional-
programming-1st-edition-reese-richard-m/

textbookfull.com

Learning Network Programming with Java 1st Edition Reese


Richard M

https://textbookfull.com/product/learning-network-programming-with-
java-1st-edition-reese-richard-m/

textbookfull.com

Learning Java An Introduction to Real World Programming


with Java Marc Loy

https://textbookfull.com/product/learning-java-an-introduction-to-
real-world-programming-with-java-marc-loy/

textbookfull.com
THROUGH GAMES
THROUGH GAMES

Learning Java Through Games teaches you how to use the different features
of the Java language as well as how to program. The book covers as much
material as possible from the latest Java standard while requiring no
previous programming experience.
Taking an application-motivated approach, the text presents an abundance
of games. You must read through the whole chapter to understand all the
features that are needed to implement the game. Most chapters start with
a description of a game and then introduce different Java constructs for
implementing the features of the game on need-to-use bases.
The first part of the book covers basic programming techniques, such as
conditional statements, loops, methods, arrays, and classes. The second part
focuses on more advanced topics, including class inheritance, recursions,
sorting algorithms, GUI programming, exception handling, files, and applets.
This text explains not only how to write code that works but also how to
follow good software practices. All sample programs in the text strive to
achieve low cohesion and high coupling—the hallmarks of well-designed
code. Many programs are refactored multiple times to achieve code that is
easy to understand, reuse, and maintain.

K20606

LUBOMIR STANCHEV
Programming Languages
K20606_FM.indd 2 8/30/13 4:13 PM
K20606_FM.indd 1 8/30/13 4:13 PM
K20606_FM.indd 2 8/30/13 4:13 PM
Boca Raton London New York

CRC Press is an imprint of the


Taylor & Francis Group, an informa business

K20606_FM.indd 3 8/30/13 4:13 PM


CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2014 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business

No claim to original U.S. Government works


Version Date: 20130715

International Standard Book Number-13: 978-1-4665-9332-9 (eBook - PDF)

This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been
made to publish reliable data and information, but the author and publisher cannot assume responsibility for the valid-
ity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright
holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this
form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may
rectify in any future reprint.

Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or uti-
lized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopy-
ing, microfilming, and recording, or in any information storage or retrieval system, without written permission from the
publishers.

For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://
www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923,
978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For
organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged.

Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for
identification and explanation without intent to infringe.
Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com
and the CRC Press Web site at
http://www.crcpress.com
To all my teachers and to my loving and supporting family.
Contents

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
List of Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
List of Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix

I Basic Principles 1
1 Computer Hardware and Software 3
1.1 Brief History of Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Hardware Components of a Computer . . . . . . . . . . . . . . . . . . . . 3
1.3 Binary Representation of Numbers . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Software Creation and Types of Software . . . . . . . . . . . . . . . . . . . 6
1.5 Type of Programming Languages . . . . . . . . . . . . . . . . . . . . . . . 7
1.6 Brief History of Computer Games . . . . . . . . . . . . . . . . . . . . . . . 8
1.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.8 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2 Data Types and Conditional Statements 11


2.1 Our First Java Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3 Random Numbers and the if Statement . . . . . . . . . . . . . . . . . . . 23
2.4 Combining Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.5 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.6 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.7 The Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.9 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.10 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.12 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

3 Loops 41
3.1 The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.2 The do-while Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.3 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.4 Nested for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.5 The Modulus Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.7 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.8 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

vii
viii Contents

3.10 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.11 Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

4 Methods and Formatted Output 61


4.1 Introduction to Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
4.2 Formatting Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
4.3 Code Refactoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
4.4 Documenting Methods Using JavaDoc . . . . . . . . . . . . . . . . . . . . 68
4.5 Sending Data between Methods . . . . . . . . . . . . . . . . . . . . . . . . 69
4.6 The Trading Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
4.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
4.8 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
4.9 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
4.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
4.11 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
4.12 Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

5 Introduction to Arrays 83
5.1 One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.1.1 Deep versus Shallow Array Copy . . . . . . . . . . . . . . . . . . . 93
5.1.2 Deep versus Shallow Array Comparison . . . . . . . . . . . . . . . . 95
5.2 The Trading Game Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . 96
5.3 Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
5.4 Variable Argument Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 102
5.5 Command Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
5.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
5.7 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
5.8 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
5.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
5.10 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
5.11 Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

6 Introduction to Classes 109


6.1 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
6.2 Class Interaction and Data Encapsulation . . . . . . . . . . . . . . . . . . 110
6.3 Default Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
6.4 The toString Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
6.5 Instance versus Static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
6.6 Non-empty Constructors and the Hidden Parameter this . . . . . . . . . 118
6.7 Array of Objects and Multi-Class Solutions . . . . . . . . . . . . . . . . . . 120
6.8 Multi-Class Solution to the Battleship Game . . . . . . . . . . . . . . . . . 123
6.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
6.10 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
6.11 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
6.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
6.13 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
6.14 Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Contents ix

7 The ArrayList Class and the enum Keyword 137


7.1 Introduction to the ArrayList Class . . . . . . . . . . . . . . . . . . . . . 137
7.2 Immutable Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
7.3 The StringBuffer Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
7.4 The Interface of an ArrayList . . . . . . . . . . . . . . . . . . . . . . . . . 142
7.5 Introducing the enum Construct . . . . . . . . . . . . . . . . . . . . . . . . 144
7.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
7.7 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
7.8 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
7.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
7.10 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
7.11 Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

II Advanced Programming Techniques 157


8 Classes Revisited 159
8.1 Class Containment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
8.2 Inheritance and the super Keyword . . . . . . . . . . . . . . . . . . . . . . 160
8.3 Multiple Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
8.4 Constructors of Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
8.5 Abstract Classes and Methods . . . . . . . . . . . . . . . . . . . . . . . . . 166
8.6 Auto-casting, Polymorphism, and Dynamic Binding . . . . . . . . . . . . . 167
8.7 Interfaces and the Comparable Interface . . . . . . . . . . . . . . . . . . . 172
8.8 Access Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
8.9 The final Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
8.10 Static Methods and Polymorphism . . . . . . . . . . . . . . . . . . . . . . 178
8.11 Explicit Type Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
8.12 Cloning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
8.13 Comparing Objects for Equality . . . . . . . . . . . . . . . . . . . . . . . . 184
8.14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
8.15 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
8.16 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
8.17 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
8.18 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
8.19 Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

9 Fun with Swing 193


9.1 Introduction to Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
9.2 Creating Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
9.3 Panels and Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
9.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
9.5 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
9.6 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
9.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
9.8 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
x Contents

10 Nested Classes and Event Handling 213


10.1 The Timer Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
10.2 Nested Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
10.2.1 Static Nested Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 215
10.2.2 Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
10.2.3 Local Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
10.3 Event Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
10.3.1 Key Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
10.3.2 Mouse Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
10.3.3 Menu Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
10.4 Multicasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
10.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
10.6 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
10.7 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
10.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
10.9 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
10.10 Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

11 The Breakout Game (Complete Version) 239


11.1 Overview of the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
11.2 Game Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
11.3 Moving the Ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
11.4 Adding the Paddle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
11.5 Drawing the Stickmen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
11.6 Adding the Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
11.7 Adding the Bricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
11.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
11.9 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
11.10 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
11.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
11.12 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
11.13 Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265

12 Layout Management and GUI Components 267


12.1 Creating Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
12.2 Flow Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
12.3 Border Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
12.4 Text Fields and Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
12.5 Grid Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
12.6 Creating Text Areas with Scroll Bars . . . . . . . . . . . . . . . . . . . . . 275
12.7 The Combo Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
12.8 Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
12.9 Radio Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
12.10 Document Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
12.11 Creating Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
12.12 Working with Password Fields . . . . . . . . . . . . . . . . . . . . . . . . . 285
12.13 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
12.14 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
12.15 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
12.16 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
12.17 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Contents xi

12.18 Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292

13 Exception Handling and Files 293


13.1 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
13.2 Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
13.2.1 The File Chooser Dialog . . . . . . . . . . . . . . . . . . . . . . . . 299
13.2.2 Reading from Text Files . . . . . . . . . . . . . . . . . . . . . . . . 300
13.2.3 Writing to Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . 303
13.3 Data Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
13.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
13.5 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
13.6 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
13.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
13.8 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
13.9 Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317

14 Recursion 319
14.1 Base Case and General Case . . . . . . . . . . . . . . . . . . . . . . . . . . 319
14.2 Dynamic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
14.3 Internal Details of a Recursive Call . . . . . . . . . . . . . . . . . . . . . . 323
14.4 Array Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
14.4.1 Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
14.4.2 Bubble Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
14.4.3 Selection Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
14.4.4 Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
14.4.5 Quick Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
14.4.6 Merge Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
14.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
14.6 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
14.7 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
14.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
14.9 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
14.10 Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340

15 Java Applets 343


15.1 HTML and the Java Applet Architecture . . . . . . . . . . . . . . . . . . . 343
15.2 Principles of Java Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
15.3 Creating Popup Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
15.4 The Tic-Tac-Toe Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
15.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
15.6 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
15.7 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
15.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
15.9 Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
15.10 Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

Index 361
Preface

Java is a programming language that was originally developed in 1995 by James Gosling
at Sun Microsystems, which later became part of Oracle Corporation. Since then, seven
major revisions of the language have been introduced and thousands of textbooks that
describe every nitty-gritty detail of the language have been published. At the same time,
many computer science departments of universities throughout the world have adopted
Java as their introductory programming language. The reason is that Java is a strongly
typed language that is easy to learn and apply. The language also helps identify erroneous
code early in the coding process. The popularity of Java as an introductory programming
language has led to the publication of thousands more textbooks that use the Java language
to introduce the reader to basic programming principles.
This textbook stands somewhere in the middle. It tries to cover as much material as
possible from the latest Java standard (Java SE 7). At the same time, it is a textbook that is
aimed at readers with no previous programming background. It not only teaches how to use
the different features of the language, but it also teaches the reader how to program. What
makes this textbook unique is its application-motivated approach. The textbook contains
a plethora of games. Almost all Java constructs are introduced as a necessity to implement
different game features. Most chapters start with a description of a game. Next, different
Java constructs that can be used to implement the features of the game are introduced
on need-to-use bases. The textbook reads similar to a mystery novel. The reader must
read through the whole chapter in order to understand all the features that are needed to
implement the game.
The second strength of the textbook is that, unlike most existing Java textbooks, it
spends a lot of time preaching good software development practices. Martin Fowler once
famously wrote, “Any fool can write code that a computer can understand. Good program-
mers write code that humans can understand.” This textbook tries to teach the reader not
only how to write code that works, but also how to follow good software practices. All
sample programs in the textbook strive to achieve low cohesion and high coupling, which
is the marksmanship of well-designed code. Many programs in the textbook are refactored
multiple times in order to achieve code that is easy to understand, reuse, and maintain.
The author of this textbook firmly believes that even novice programmers should be taught
good programming practices. The reason is that if one does not develop good programming
habits from the outset, then it is more difficult to develop them afterwards.
The textbook is split in two parts. Chapters 1–7 cover basic programming techniques,
such as conditional statements, loops, methods, arrays, and classes. Chapters 8–15 cover
more advanced topics, such as class inheritance, recursions, sorting algorithms, GUI pro-
gramming, exception handling, files, and applets. The textbook is designed to be either
used by a reader who wants to learn the language on their own or as part of a two-course
introduction to programming sequence. Most chapters build on each other and the best way
to read the textbook is from start to finish. A dependency diagram of the chapters is shown
in Figure 0.1.
Chapter 1 introduces basic computer concepts, such as main memory, hard disk, oper-
ating system, and binary numbers. It should be read by readers that have little computer

xiii
xiv Preface
1 8

2 9 13

3 10

4 11 12

5 15

14 6

FIGURE 0.1: Chapter sequence.

experience. Chapter 2 introduces variables and conditional statements. These are the build-
ing blocks of any program. Chapter 3 introduces loops. Loops allow us to execute the same
piece of code multiple times. Chapter 4 introduces methods. Methods allow the program-
mer to divide the code into smaller segments, which can shorten the program and eliminate
redundant code. Chapter 5 introduces arrays. These allow us to create numerous pieces
of data of the same type in a single statement. Chapter 6 introduces classes, which are
the pivot of any object-oriented programming language. Classes are introduced late in this
textbook because they are a more advanced concept that requires understanding of basic
programming principles. Chapter 7 combines two independent topics: the utility ArrayList
class and the Java enum keyword. An ArrayList is a more powerful version of an array that
provides more capabilities. Conversely, the enum keyword stands for enumerate and allows
the programmer to enumerate the values of a user-defined type.
The second part of the textbook contains advanced programming techniques. Chapter 8
revisits classes and introduces more advanced topics, such as inheritance and polymorphism.
Chapter 9 shows the basics of displaying text and drawings inside a window. Chapter 10
describes how events can be handled in Java. Concepts from Chapter 9 are used as examples.
Chapter 11 presents the Breakout game. The chapter shows how programming techniques
can be applied on a non-trivial project. If the reader is in a hurry, the chapter can be
skipped because it is not a prerequisite for any other chapter. Chapter 12 describes basic
graphical user interface (GUI) components, such as windows, panels, buttons, combo boxes,
and so on. Chapter 13 describes files and exception handling. It requires, as a prerequisite,
mainly the understanding of classes and class inheritance. Chapter 14 covers recursion and
requires, as a prerequisite, the understanding of methods and arrays. Chapter 15, which is
an optional chapter, shows the reader how to program Java Applets.
Book resources, including Java code from the examples in the textbook and slides, can be
found at http://softbase.ipfw.edu/∼lubo/LearningJava. Please e-mail typos or suggestions
for book improvement to [email protected].
About the Author

Lubomir Stanchev is an associate professor in the Department of Computer Science at


Indiana University – Purdue University Fort Wayne. He got his Ph.D. degree from the
David R. Cheriton School of Computer Science at the University of Waterloo in Canada.
He has taught introductory programming courses and software engineering courses about
fifteen times in the past eight years. He has published more than five journal articles and
more than twenty conference proceedings in the area of computer science. He has been
writing software code for over thirty years and he has worked as a software developer in
four different companies.

xv
List of Figures

0.1 Chapter sequence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv

1.1 A Colossus Mark 2 computer. Artwork created by United Kingdom Govern-


ment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 An Intel 80486DX2 CPU from above. Photographed by Andrew Dunn. The
picture is licensed under the Creative Commons Attribution-Share Alike 2.0
Generic license. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 DDRAM memory modules for desktop computers. Picture is from Wikipedia
commons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 The game Tennis for Two. The screen shot is taken from a video made by
the Brookhaven National Laboratory, a United States Government laboratory. 8
1.5 The game Spacewar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.6 The game Pong. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2.1 Creating a new Java project. . . . . . . . . . . . . . . . . . . . . . . . . . . 12


2.2 Setting the properties of a Java Application project. . . . . . . . . . . . . . 13
2.3 Creating a new Java class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4 Example program execution. . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.5 Allocating integer in main memory. . . . . . . . . . . . . . . . . . . . . . . . 19
2.6 The if-else construct. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.7 The if construct. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.8 ASCII code table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.9 The switch statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

3.1 The while construct. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42


3.2 The do-while construct. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.3 The for statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

4.1 The different parts of a method. . . . . . . . . . . . . . . . . . . . . . . . . 62


4.2 Passing parameters between methods. . . . . . . . . . . . . . . . . . . . . . 70

5.1 Example array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85


5.2 Passing an array to a method. . . . . . . . . . . . . . . . . . . . . . . . . . . 92
5.3 Example of shallow copy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
5.4 Example Battleship board. . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
5.5 Initial array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
5.6 Example of ragged array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

6.1 Interface of a class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111


6.2 Example of object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
6.3 The Die class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

7.1 Passing an immutable object to a method. . . . . . . . . . . . . . . . . . . . 139

xvii
xviii List of Figures

7.2 Passing a mutable object to a method. . . . . . . . . . . . . . . . . . . . . . 140


7.3 Heads of different coins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

8.1 Object composition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160


8.2 Class inheritance example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
8.3 Class inheritance and super object. . . . . . . . . . . . . . . . . . . . . . . . 163
8.4 Example of multiple inheritance. . . . . . . . . . . . . . . . . . . . . . . . . 164
8.5 Autocasting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
8.6 Inheritance hierarchy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
8.7 Cloning example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182

9.1 The Breakout game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195


9.2 Pixels in a screen. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
9.3 Coordinates of a ball. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
9.4 Picture for Exercise 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
9.5 Picture for Exercise 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

10.1 Example of inner class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217


10.2 Example of key listener. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
10.3 Example of multicasting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
10.4 Start of the Reversi game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

11.1 UML Diagram of the game Breakout. . . . . . . . . . . . . . . . . . . . . . 240


11.2 Explanation of the below method. . . . . . . . . . . . . . . . . . . . . . . . 259
11.3 Pieces of the Tetris game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265

12.1 The border layout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270


12.2 Degree converter program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
12.3 Calculator program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
12.4 A font combo box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
12.5 Example of a check box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
12.6 Example of radio buttons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
12.7 Example of a dialog box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
12.8 Example of a password field. . . . . . . . . . . . . . . . . . . . . . . . . . . 286

13.1 The file chooser dialog box. . . . . . . . . . . . . . . . . . . . . . . . . . . . 300


13.2 Moving the file cursor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302

14.1 Example of computing the fifth Fibonacci number recursively. . . . . . . . . 321


14.2 Example of computing the 5th Fibonacci number using dynamic programming. 322
14.3 Towers of Hanoi. Picture taken from Wikipedia Commons. . . . . . . . . . . 323
14.4 Example of a stack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
14.5 Example of computing 2! recursively. . . . . . . . . . . . . . . . . . . . . . . 325

15.1 Java Applet architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344


15.2 The Tic-Tac-Toe applet inside a web browser. . . . . . . . . . . . . . . . . . 347
15.3 The index of the squares in the Tic-Tac-Toe game. . . . . . . . . . . . . . . 350
List of Tables

1.1 Memory units. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5


1.2 Hexadecimal digits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

2.1 Java primitive types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17


2.2 Java comparison operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.3 Java operators on Boolean values. . . . . . . . . . . . . . . . . . . . . . . . 28
2.4 Comparison of String elements. . . . . . . . . . . . . . . . . . . . . . . . . 30

3.1 Increment/decrement Java shortcuts. . . . . . . . . . . . . . . . . . . . . . . 47

4.1 Examples using printf. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

5.1 Example two-dimensional array. . . . . . . . . . . . . . . . . . . . . . . . . . 100


5.2 Yahtzee game combinations. . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

6.1 Access matrix for static/instance for methods. . . . . . . . . . . . . . . . 113

8.1 Access privilege modifiers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176

9.1 Font masks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200

xix
Part I

Basic Principles
Chapter 1
Computer Hardware and Software

1.1 Brief History of Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3


1.2 Hardware Components of a Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Binary Representation of Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Software Creation and Types of Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5 Type of Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.6 Brief History of Computer Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.8 Important Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

This chapter introduces the basic components of a computer: its hardware and software. Af-
ter all, this is a computer programming textbook and we need to have a basic understanding
of how the computer works before we can start developing software for it. For completeness,
some historical information about computers and computer games is also included in the
chapter.

1.1 Brief History of Computers


Computers are electronic devices that can perform calculations. The first electronic
digital computer was built by physics professor John Atanasoff and his graduate student
Clifford Berry in 1937. The computer could not be programmed and could be used only to
solve linear equations. Six years later, in 1943, the first programmable electronic computer
was built by Tommy Flowers; see Figure 1.1.
As you can see in the picture, the first computers took a lot of space. Different compo-
nents, such as secondary storage and main memory, were in separate racks. Early computers
could easily fill a present-day classroom. Colloquial expressions such as: “I will go to check
on the memory” were common in those days. Even the term “bug”, which is commonly
used to describe a software error, derives from the days when actual rodents were roaming
around the different components of a computer. As time progressed, computers became
smaller and cheaper. The personal computer was introduced in the late 1970s and early
1980s by the likes of Hewlett Packard, Apple, and IBM.

1.2 Hardware Components of a Computer


The brain of a computer is the Central Processing Unit (CPU); see Figure 1.2 . It is a
device that can perform simple calculations. These include addition, deletion, subtraction,

3
4 Learning Java through Games

FIGURE 1.1: A Colossus Mark 2 computer. Artwork created by United Kingdom Gov-
ernment.

FIGURE 1.2: An Intel 80486DX2 CPU from above. Photographed by Andrew Dunn. The
picture is licensed under the Creative Commons Attribution-Share Alike 2.0 Generic license.

and multiplication. The speed of a CPU is measured in hertz, which is a measure of frequency
in cycles per second. In one cycle, the CPU can perform one instruction. Typical CPU
speeds of modern computers are around 2 GHz, which corresponds to 2 gigahertz, or 2
billion instructions per second. The actual data on which the operations are performed is
stored in main memory; see Figure 1.3. The main memory is where both the programs and
the data are stored.
The main memory contains both the application data and the commands that manip-
ulate the data. The commands from the main memory are loaded on the CPU. The CPU
then reads data from main memory, performs the operation that is requested, and saves the
result back into main memory. Therefore, all a computer can do is to move data from one
spot to another in main memory and perform arithmetic computations.
The main memory of a computer consists of a sequence of cells, where every cell can
take one of two states: 0 or 1. The different values are usually represented by different
voltage. For example, 0.8 volts or below may denote 0, while 2.2 volts or above can denote
the number 1. In computer science, this single cell is referred to as a bit. Table 1.1 shows
common units for measuring the size of memory (i.e., number of bits).
In modern computers, the main memory of a computer is typically between 1 and 16
GBs, while the hard disk can typically store anywhere between 256 GB and 1 TB. Moore’s
law is the observation that over the history of computing hardware, the number of transistors
Computer Hardware and Software 5

FIGURE 1.3: DDRAM memory modules for desktop computers. Picture is from
Wikipedia commons.

TABLE 1.1: Memory units.


(Unit) (Size) (Approximate size in bytes)
byte (B) 8 bits 1
kilobyte (KB) 1024 bytes 1,000
megabyte (MB) 1024 KBs 1,000,000
gigabyte (GB) 1024 MBs 1,000,000,000
terabyte (TB) 1024 GBs 1,000,000,000,000

on integrated circuits doubles approximately every two years. This means that with time,
both main memory and CPUs become cheaper.
Note that there is a significant difference between the hard disk and the main memory
of a computer. When the computer shuts down, everything that is written in main memory
disappears. This is why the main memory of a computer is referred to as volatile memory.
Alternatively, the data on the hard disk is persistent even after the computer shuts down
and there is no power. Usually, magnetic fields are used to store the data permanently. This
is why hard disks are referred to as persistent storage. Since the hard disk of a computer
contains physical components, such as moving heads and rotating platters, accessing data
from the hard disk is significantly slower than accessing data from main memory. Since the
hard disk contains moving components, Moore’s law does not apply to it. Main memory is
significantly more expensive than hard disk memory. For example, currently one can buy a
1 TB hard disk for around $100. Buying that much main memory costs more than 10 times
more. Note that the CPU cannot directly access data from the hard disk. The data needs
to be first brought into main memory before it can be accessed by the CPU.
Main memory is often referred to as Random Access Memory (RAM). This means that it
takes exactly the same time to access any cell of the main memory. Alternatively, accessing
different parts of the hard disk can take different time. For example, the sector (unit of
division of the hard disk) that is the closest to the reading head can be accessed the fastest.
Accessing a sector that is far away from the reading head will incur rotational delay. This
is the time that is needed for the reading head to reach the sector.
Input devices and output devices can be connected to a computer. Examples of input
devices include keyboard, joystick, mouse, and microphone. Examples of output devices
include monitor, speakers, and printer. An Internet connector can be considered to be both
an input and output device. The CPU communicates with devices though main memory
reads and writes. For example, a program can display an image on the screen by writing the
image to the video card of the display. Similarly, information from an Internet connector
can be received by reading the data from shared main memory.
6 Learning Java through Games

1.3 Binary Representation of Numbers


It is important to understand the fact that a computer can store only zeros and ones.
Consider the integer 423. It is equal to 4 · 102 + 2 · 101 + 3 · 100 . This is known as base 10
representation because every digit is between 0 and 9. Similarly, there is base 2 represen-
tation that uses only 2 digits: 0 and 1. This is also referred to as the binary representation
of the number. For example, the binary number 01010010 represents the decimal number
0 · 27 + 1 · 26 + 0 · 25 + 1 · 24 + 0 · 23 + 0 · 22 + 1 · 21 + 0 · 20 = 82.
There is also an easy algorithm for converting a decimal integer to a binary number.
Consider the decimal integer 134. The conversion algorithm keeps dividing the number by
2 and records the remainders.
134 /2 = 67 r e m a i n d e r 0
67 / 2 = 33 r e m a i n d e r 1
33 / 2 = 16 r e m a i n d e r 1
16 /2 = 8 r e m a i n d e r 0
8 / 2 = 4 remainder 0
4 / 2 = 2 remainder 0
2 / 2 = 1 remainder 0
add a 1
The binary number is created by starting with the number 1 and then taking the re-
mainders in reverse order. For example, the decimal number 134 is equal to the binary
number 10000110. To verify our work, we can try to convert the number back to a decimal
number. It will be equal to: 1 · 27 + 0 · 26 + 0 · 25 + 0 · 24 + 0 · 23 + 1 · 22 + 1 · 21 + 0 · 20 = 134.
Sometimes, data that is stored in computers is represented using hexadecimal numbers.
These are numbers in base 16. Table 1.2 shows the hexadecimal digits. The first 10 digits
are the same as the decimal digits. Then the letters A–F are used to represent the numbers
10–15. Consider the hexadecimal number 4F1A. Since a hexadecimal digit takes 4 bits to
represent, the number will be 16 bits or 2 bytes long. To convert it into a binary number,
we can just use Table 1.2 as a reference and convert every digit individually. In binary, the
number will be: 0100 1111 0001 1010. Since the hexadecimal representation of a number is
much shorter than the binary representation, it is often used to represent binary data, such
as main memory addresses.

1.4 Software Creation and Types of Software


Software is usually written by software programmers using a programming language, such
as Java or C. It is then converted into binary code that the CPU can understand. A compiler
will directly translate the program code into binary (or executable) code, which can be later
executed. For example, in Windows executable files have the extension .exe. Conversely,
an interpreter interprets the program. It translates every line of code into executable code,
which is then executed. A file that contains the executable code is not created. An interpreter
is usually slower than a compiler because each line of code needs to be translated into
executable code before it can be executed.
Java takes an approach that uses both a compiler and interpreter. Java code is compiled
into Java binary code, which is different than the binary code that the CPU can understand.
This binary code can be later executed by a Java Virtual Machine (JVM) interpreter. This
Computer Hardware and Software 7
TABLE 1.2: Hexadecimal digits.
(Hexadecimal digit) (Decimal number) (Binary number)
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111

is the reason why Java programs can run slower than programs that are directly compiled,
such as programs written in the C programming language. Alternatively, the Java Virtual
Machine may be physically coded as part of the CPU. For example, some cell phones have
CPUs that can execute Java binary code.
There are two general types of software: operating system software and application soft-
ware. Operating systems software provides the interface between the hardware of the com-
puter and the application software. For example, the operating system of a computer allows
programs to access input and output devices, such as the keyboard, the hard disk, the
mouse, and the monitor. Most modern operating systems also provide Graphical User In-
terface (GUI) to the system. A different responsibility of the operating system is to allow
programs to execute concurrently and share resources, such as the main memory and the
hard disk. The application software in a computer is made up of the programs that are run
by the end users. These include games, office software, and web browsers.

1.5 Type of Programming Languages


Computer software can be written using different programming languages. A program-
ming language of first generation is machine code. To put it differently, this is a sequence
of zeros and ones that the computer can understand. Such code can be represented, for
example, in hexadecimal notation. A typical example of a machine instruction is to read
data that is stored in a location of main memory. The problem with machine code is that
it is low level and it is difficult for humans to interpret. A second-generation programming
language is referred to as Assembly language. Now the instructions are written using En-
glish words (e.g., add, sub, move, etc.). There is a straightforward mapping between code
written in an Assembly language and machine code. The disadvantage of Assembly is that
it is a very low level language and writing Assembly code is difficult. A third-generation
programming language provides high-level access to the data. Examples of third-generation
programming languages include C, C++, and Java. An advantage of using a high-level
programming language is that the programmer does not need to deal with physical main
Random documents with unrelated
content Scribd suggests to you:
CHAPTER XV

I
SWEAR, I'd enjoy firing his barn!” Dick fumed, as the two friends
walked on through the beating sun. “I don't think I can stand
much more of it, Fred. I'm all gone inside. The lining of my
stomach has folded over.” They were passing the corner of a field
where, in the distance, they could see two men at work digging
ditches to drain the boggy land, and they paused again to rest under
the shade of a tree.
“I guess they will stop soon and go home to a square meal,” Dick
said, bitterly; and then his roving glance fixed itself on a spot in the
corner of the snake-fence near by.
“By George!” he exclaimed, exultantly, “we are in luck! Gee, what
a pick-up!”
“What is it, now?” Walton asked. But the boy was bounding away
toward the fence. “You wait and see—gee, what luck!”
Walton stood and watched him as he climbed over the fence,
dived into the thick underbrush, and reappeared with a covered tin
pail in his hands. As he came back he unfastened the lid and
laughed loud and long. “Full to the brim!” he chuckled. “Meat, bread,
pie, and a bottle of fresh milk. We can leg it along the road a piece
and sit down to it, or stow it away as we walk. My dinner-bell's rung,
old man.”
“Put it back, Dick! Go put it back!” Fred said, firmly, his eyes
averted.
The boy stared, a blended expression of surprise and keen
disappointment capturing his features.
“Do you really mean it, Fred?” he asked, his lip falling, the pail
hanging motionless at his side.
“Yes, it is not ours,” the other said. “Put it back before they see
you, and then I'll—I'll try to explain what I mean.”
The boy swore under his breath, and for a moment he stood
gloweringly sullen, but at the third command of his companion he
retreated to the fence and dropped the pail into its place. Then he
came back, his head hanging, his face still dark with disappointment.
“Huh!” he grunted, and started on without waiting to see if Fred
was ready to go. Walton followed, and presently caught up with him.
“I'm not a preacher, Dick,” he began, with a forced laugh, which
was intended as an opening wedge to the boy's displeasure, “I'm not
one bit better than you are. I've stolen a farmer's watermelons by
the light of the moon, and climbed his June apple-trees, and filled
my pocket with his prize fruit, and heartily enjoyed it; but somehow
I feel differently now. Dick. I'm older than you are, and reckless
living has got me down and stamped all hope out of me. I'm fighting
for my life. I'm swimming in a strange, swift stream, and my
strength is almost gone, but I have grasped at a straw; it may hold
me up, it may not; but I hope it will. That straw is the determination
to live right—absolutely right—from now on, no matter what it costs.
I've done great wrong, and I'm sick with the very thought of it. I
want to try to do what is right, and if I could influence you to feel as
I feel about these things, I'd like it mightily; it would strengthen me
in my course. Two can succeed better, even at a thing like that, than
one.”
“But I'm starving!” the boy whimpered. “The world wasn't made
for anybody to starve in. The birds up there in the trees don't starve,
and God gave them as good right to live as you or me. Huh! when
that beefy chump back there sows his wheat they watch him with
their keen eyes from their nests in the trees, and when his hulking
back is turned they chirp with glee and pounce down on his seed
and take it and flutter away with it in the sunshine.”
“Dick, you are a bloody anarchist!” Walton laughed gently as he
placed his hand affectionately on the boy's shoulder.
“I don't know whether I am or not,” Warren retorted, still ruffled.
“But the blamed bucket of grub may stay where it is. I wanted it for
your sake as much as mine, but I sha'n't ask you to sit down to
other men's dinner if you are going to ask the blessing over it. But
you are too dang particular. At least, I've got as much right to the
stuff as they have, for they can go home and get more, and I can't.”
“That is one way to look at it,” Walton said, quietly, “and I thought
as you do once, but I don't now.” After this they trudged along for
several minutes in silence. The boy did not raise his eyes from the
dusty ground, but he put his hand on Walton's arm, and there was a
catch in his young throat as he said:
“Fred, somehow you make me think of my mother, When she was
alive she was always wanting me to be good. She used to talk to me
when I was a little tiny fellow. It was always that one thing over and
over: 'My little boy is not going to be a bad man when he grows up,
is he?' That's what she said time after time, and in a thousand ways
she tried to impress it on me. She worried a lot about me just before
she died. You see, my father—well, he didn't care what became of
me, or her, either. He drank like a fish, and went with idle men about
the loafing-places—in fact, he was shot and killed in a bar-room. I've
tried pretty hard to have faith in what my mother used to say about
God's mercy and all that stuff, but, Fred, God never answered her
prayers to look after me. If I haven't had to go it blind, I don't want
a cent. Selling papers on the street at night till nearly morning,
sometimes sleeping in a stairway, outhouse, or stable. Then I was a
messenger boy, for a little better wages, in a dead boy's uniform,
and finally became a tramp telegraph operator. But, Fred, you are
true blue. I don't want a better pal. The way you yanked out that
watch and offered it to keep me out of jail when it was the last thing
you had in your pocket—well, you can count on me, that's all. I
won't try to stuff another man's grub down your throat, either.”
A man was coming toward them on horseback, and as he drew
near he reined in and leaned forward on the neck of his horse.
“Gentleman,” he began, as he pulled at his scraggy beard and kicked
his feet more firmly into his wooden stirrups, “I don't know whether
you fellows are interested in the like or not, but I'm riding round
here and yon trying to drum up hands to gather and crate and ship
my crop of early peaches. There is such a demand for labor of that
sort all through the peach section that we are powerful short on
help.”
The two pedestrians exchanged eager glances.
“Where is your place?” Fred asked.
“Why, it's a few miles to the right, over them hills,” the rider said.
“It's the Womack farm. That's my name. I've got a hundred acres of
dandy Elbertas, and they are ripening as fast as chickens in a
hatching-machine. They are a thing that has to be picked an' got off
in cold-storage cars at exactly the right minute or they ain't worth
the nails in the crates when they get to market. They say if all us
early fellows can manage to hit New York just right this year, we'll
get three dollars a crate, an' that will pay big, as times are now.”
“How far is it to your place?” Walton asked.
“Why, it's a little better than seven mile—on a beeline; but I
reckon by the nighest road it's a matter of ten or thereabouts. You
fellers look a little mite tired, but by stiff walking you could get there
by sundown. You can make good wages in a pinch like this if you will
buck down to it—I calculate three plunks a day for each of you.”
“And how long would the work last?” inquired Fred, as he and
Warren looked at each other, their pulses quickening, their eyes
beginning to glow.
“Well, I could hold you down for two weeks at least, for mine
don't all ripen at once; but after you was through on my land you
could go farther north and get more to do.”
“I think we'd better take you up,” Warren said. “I'd like that sort of
work.” He winked at his friend and rubbed his stomach. “I see myself
packing good, ripe, juicy peaches right now, but not in crates. The
truth is, farmer, we are mighty hungry, and that is a long walk. Now,
if you had fifty cents about you that you'd be willing to let go in an
advance, why we'll buy a snack at some farm-house, and go right on
to you.”
The horseman's shrewd face fell. He leaned forward and ran his
gnarled fingers through the mane of his horse, and avoided the pair
of anxious eyes fixed on his. “I don't want to be blunt and hurt your
feelings, fellers,” he said. “But we never come together before—we
are plumb strangers, I might say; and, well, to tell the truth, last
year I started out on this same business, and to my certain
knowledge not a man, woman, gal, boy, nor baby that I advanced
money to ever got to my place, while all the others who wasn't paid
was there bright and early.”
“But we are hungry and weak!” Dick Warren protested.
“Well, some o' them that I failed to get told the selfsame tale. One
said if I'd pay off the mortgage on his land, he'd bring his entire
family; but that wasn't business, and I refused. I'm making you
fellows a fair open-and-shut proposition. You hit my place before
dark to-night and tell my wife to give you a square meal—tell her
I've hired you to pick and pack, and that I said to stow you away
somewhere for the night. She will make room for you. Now, I hope
I'll see you there. That's as good as I can offer, as I look at it.”
“All right, we'll be there,” Walton promised. “And we will do the
best we can for your interests.”
“Very well, gentlemen, I'll expect to see you there when I get
back. So long.” And with his legs jogging the flanks of his mount, the
farmer rode away.
“We can make it, Dick,” Walton said, encouragingly. “Let's bend
down to it.”
“The thought of that meal is enough to keep me going,” the boy
replied. “What do you reckon she will give us? But stop! My mouth is
watering at such a rate that I believe I'll try not to think of it.”
It was long after sundown when the wayfarers reached the farm
in question. The house was a rambling, one-story, frame structure
which originally had been painted, afterward whitewashed, and rain
and storm beaten till not a trace of any sort of coating remained on
the bare, fuzzy, gray boards. At the gate, or bars, of the snake-
fence, in front, they paused, faint and exhausted, wondering if they
would be bitten by watch-dogs if they entered unannounced. On the
grass under the trees in the front yard a group of twenty or more
young women and young men were singing plantation melodies, and
here and there couples were sitting alone or strolling about, their
heads close together.
“They are peach-gatherers,” Walton surmised. “Come on; there
are no dogs that I can see.”
Crawling through the bars, they went to the house. There was no
light in the front part, but a yellow glow shone from a window
against the dark foliage of the trees in the rear, and thither the
wanderers directed their lagging steps. Looking in at the open door
of the kitchen, they saw the portly form of the farmer's wife at a
table washing dishes in the light of a smoking brass lamp which had
no chimney.
“Oh!” she exclaimed, as her kindly eyes fell on them. “Not more
pickers, surely?”
“That's what we are, and as good as you ever laid eyes on,” Dick
told her. “Mr. Womack said you'd give us something to eat. We
haven't had a bite since yesterday.”
“Well!” The woman drew her hands from the big dish-pan and
dried them on her apron as she looked them over doubtfully. “Pete
Womack goes crazy every year at picking-time. He's filled the house,
barn, and yard with hooting and singing gals and boys, and
furnished nobody to wait on 'em but me. The gals all say they are
too fagged out at night to lay their hands to cooking or dish-
washing, and yet, if you'll just listen and watch, you'll see that they
are all able to gallivant with the men about the yard. Six couples met
here for the first time last summer and got married. They say there's
some progress being made right now between three or four, an'
picking's just set in. I tell Pete he ought to start a marrying-agency
and take out a license to preach, so he can tie 'em on the spot and
collect two fees. Some of 'em are respectable and mean all right, but
Pete is so anxious to get his crop off on time that he's got women in
that bunch that—to look at 'em—Well, it ain't any of my business! I
ain't set up as a judge, and as the saying is, I won't throw no
stones. But you say you are hungry, and I don't see how I could give
you a thing hot at this time of night. My fires are out, and—”
“Hot!” Dick shouted. “Why, I've got such a big storage capacity
that I'd be afraid to take it hot. It might generate steam and
explode.”
The woman laughed. “Well, you must be hungry,” she said. “Come
on in the dining-room and I'll lay it out in a minute. There is plenty
of cold stuff. I cook a lot ahead. You have to feed pickers like kings
or they won't stay. It won't take long to heat the coffee. But I reckon
you want to wash and wipe. You'll find pans and water on the shelf
in the entry, and a clean towel on the roller. I'll be ready when you
are.”
“I'll see about that, old lady,” Dick challenged her, as he made a
dash for the near-by water-shelf.
Two minutes later the two wanderers sat down at a long,
improvised table, made of unplaned planks, in the dining-room. In
the light of a guttering home-made tallow dip the farmer's wife
spread before them the best meal that famished men ever feasted
on. They saw roast chicken with dressing, fried chicken with cream
gravy, country-smoked ham in a great platter of eggs; butter, hard
and cold, from the spring-house; great, snow-capped pound-cakes,
biscuits, apple-sauce, jellies, jams, cold buttermilk, and hot coffee.
“I don't know where I'm going to bunk you boys,” Mrs. Womack
said, in a motherly tone, as she stood behind their chairs, and, with
unsuppressed delight, watched them eat. “The women and gals
have got every bed in the house; and every spot on the floor, even
to the kitchen, has been staked off by the men.”
“What's the matter with the barn?” Dick mumbled, with his mouth
full. “I wouldn't want a better place this time of year than a sweet-
smelling bed of fresh hay or fodder.”
“There's plenty of room in the loft down there,” the woman
replied; “but somehow I hate to see nice-looking young men like you
put in a place like that.”
“It will do very well,” Fred assured her. “In fact, we would rather
like it.”
“Well, a little later, if you decide to stay, I may fix you a place in
the house,” the woman said; “but you got in too late to-night.”
“I'm dead tired and sleepy, Fred,” Dick said, when they had left
the table. “Let's turn in.”
Directed by Mrs. Womack, they went down to the barn, and from
the big cattle-room on the ground they climbed a ladder to the loft
above. A startled hen flew from her nest with a loud cackling as they
crawled through the hay and husks and leaves of corn to a square,
shutterless door, through which the hay was loaded to wagons
below. They threw off their coats and vests, and made pillows of
them; then took off their shoes, and lay down and stretched out
their tired limbs.
Through the doorway they saw the fathomless sky filled with
mysterious stars. The chirping of some chickens, as they jostled one
another on the roost below, came up to them; the champing of the
teeth of a horse, as he gnawed his wooden trough; the snarling of a
tree-frog; the far-off and dismal howling of a dog, and—they were
asleep.
CHAPTER XVI

I
T was not till early autumn that the two friends reached their
far-off destination. Fred's watch had been sold; they had saved
the greater part of their earnings from the various odd jobs at
which they had worked, and had made of their journey by rail. It
was Walton's idea that they must put their best foot to the front in
Gate City, and start out with a good appearance in their new home,
and so the most of their funds were promptly invested in new
clothing. Notwithstanding their spick-and-span appearance, however,
luck seemed against them, for every application they made for work
—Dick as a telegraph operator and Fred as an accountant—was
refused them.
The city was a bustling new place with prosperity and activity in its
very air. There were great railway-shops, factories of several kinds,
and various other enterprises. It was a typical Western “boom” town.
Its buildings were modern, its streets regular and well-paved. Men
and women, as they drove through the streets in their carriages,
thought nothing of it if a mounted horde of yelling cow-boys
galloped past with their revolvers playfully flourished, nor saw
anything unusual in the gangs of blanket-draped Indians who hung
about the bar-rooms, dance-halls, or gambling-houses. The new-
comers liked the place; Dick believed they would eventually secure
work, and Fred had the first sense of security which had come to
him since leaving Stafford. Here, under his new name, in this remote
place, he was sure he would meet with no familiar face, nor catch
any discordant echoes of the life he had left behind him, and which
he was trying to banish from his memory.
There was in the town a certain Stephen Whipple, a man about
sixty-five years of age, who had come from one of the Southern
States shortly after the Civil War. He had established himself, first, as
a small grocer, but, having acquired considerable wealth, he was
now the owner of the only wholesale grocery store in the place, an
establishment which was known for miles around.
He was an earnest member of the Presbyterian church of the
town, and its chief pride, owing to his influence in the community. It
had been his money which had built the church to which he
belonged, and it was said that he practically paid the salary of its
eloquent young preacher.
In his great red-brick, four-story business-house on the main
street Stephen Whipple had his private office. It was in the rear of
the counting-room and was of unusual size, and by many deemed a
curious place. Indeed, it was put to strange, unbusiness-like uses,
for it was here that the owner of the establishment personally
received all sorts of applications for aid. There were half a dozen
plain chairs in the bare, uncarpeted room, and the Rev. Luke
Matthews, who had the entrée to the office at any moment, often
found a motley gathering of supplicants on hand, each patiently
awaiting his turn to be beckoned to the seat close to the portly,
shaggy-browed merchant. There were individuals who called the old
man a deep-dyed hypocrite, for they held that no really self-
sacrificing toiler in the Lord's vineyard could have amassed the great
wealth old Whipple was known to possess. But this was disputed by
all the men in his employment, at least, for they were ready to attest
that Whipple had often held over important business matters till the
case of some suffering applicant could be investigated and relief
supplied. There were other uses to which this room was put. Old
Whipple, in order to render his pet church more attractive to the
public, selected and paid out of his own pocket the salaries of the
best choir in town. He was no expert musician, but he had them
meet in his office and practise on every Saturday afternoon, and he
was always present, seeing to it that refreshments were served and
the singers made comfortable.
It was one morning when Dick Warren and Fred Walton had been
in the town for a month, and had reached the lowest ebb of their
resources, that the minister dropped in to see the merchant. The
Rev. Luke Matthews was of unusual height, measuring six feet four,
very slender in build, and of markedly nervous temperament. He was
under thirty, unmarried, wore his black hair long enough to touch his
shoulders, and had the thin-lipped, unbearded face of an Edwin
Booth. It was said of him that he couldn't keep a coin in his pocket—
that it was promptly given to the first beggar he met.
“Well, brother, how are your bones?” was the halfjesting greeting
he gave the old man, as he bustled in, buttoning and unbuttoning
his long black coat and swinging his broad-brimmed hat at his side.
“Not holding court this morning?” He laughed as he looked over the
empty chairs.
“No; I sent the last prisoner up for life an hour ago,” the merchant
responded, jovially. “Set down, set down!”
The long-legged man with the poetic face complied. “Well,” he
said, “you'll have to be a judge in that sort of tribunal so long as you
inhabit this globe.” He smiled, showing two fine rows of white teeth.
“It looks like the Lord is pushing you on to unlimited prosperity, and
your work for humanity will increase instead of letting up. Say,
brother, I know the sort of thing you glory in, and I've had an
experience—the sort of experience that makes a fellow feel like
preaching is worth while. It was exactly the kind of thing you are
interested in yourself.”
“What have you run across now?” Whipple asked, as he leaned his
elbow on his desk and rested his florid face on his hand.
“The genuine thing, brother—a genuine reformation in a young
chap hardly out of his teens. He's been coming to my special
meetings for young men, and, as I'm a close observer, I was
attracted by his face. It interested me more than that of any boy's I
ever saw. Finally, I ventured to approach him. I never scare them off
if I can help it, but I singled him out from the rest last Thursday
evening and spoke to him. I saw that he was greatly moved, and I
invited him into my study, and we had a good long heart-to-heart
talk. Brother Whipple, I never felt the glory of God bearing down on
me in my life as I did while that boy was talking—while he was
telling me his past history. Crying like his heart would break, he
confessed to having been almost everything a boy could be—a thief,
a tramp, and an all-round, good-for-nothing idler, from his childhood
up to his sudden awakening to what was right.”
“Good, good!” Stephen Whipple ejaculated, his features working,
his kind old eyes twinkling.
“But now comes the climax to my experience,” the minister went
on. “You and I meet a converted person now and then, but we don't
often run across individuals in private life who are leading lives
which convert. The boy went on to tell me, brother, how he was
rescued from arrest by a young man who was a tramp like himself.
They began searching for work side by side. The boy told me how
his new friend—without ever saying a word that was preachy—
gradually won him from his ingrained tendencies and taught him the
difference between right and wrong. He gave me scores of touching
and inspiring incidents that had happened between them during
their wanderings here and there, trying to get work. Somehow I
became even more deeply interested in the fellow I hadn't met than
the one I had in tow, and so I asked the boy if he would introduce
me to his friend. He hesitated for a while, and then finally agreed to
take me to the room they had together. It was away over beyond
the railroads, in the slums of our 'tenderloin' district. It seemed to be
the only room whose price they could afford, and they were
unwilling to contract for what they could not pay. It was an awful
place, brother, up a narrow flight of shaky stairs, in the attic of a
negro shoemaker's house, in the worst part of 'Dive-town.' The man,
this Fred Spencer, when we came in, was seated at the little dingy
window reading a newspaper. He seemed very much surprised, and
flushed red as he stood up and shook hands. He was fine-looking—
strong and tall, well-clad and neat from his feet to his carefully
combed hair, but his great big sad eyes haunted me long after I left
him, and when he spoke his voice seemed to come from a proud
spirit that was crushed and broken. He began by saying that his
friend had spoken to him of my meetings, and that he was
exceedingly grateful for my interest and courtesy in calling. He tried
to apologize for the appearance of the room, and insisted on my
taking the only chair while he and his room-mate sat on the bed,
which, by the way, was unfit for a convict to sleep on. They used it
together, and yet it was barely wide enough for one. The straw in
the mattress was crumbling to powder and falling to the floor.”
“Poor chaps,” the merchant sighed, “and they have evidently seen
better days.”
“Spencer, the older one, has decidedly,” the minister answered.
“He is evidently Southern, for he has the soft accent of Virginia, I
should say, and the manner of the old aristocracy. I told him that I
had heard of his good influence over the boy, and he got redder
than ever, and tried to make light of what he had done, endeavored,
in fact, to convince me that the boy had only spoken as he had out
of personal friendship. Finally I offered my assistance toward finding
employment for them both, and Spencer showed real
embarrassment—as if he did not want to put me to any trouble in
the matter.”
“He's tried to find work here, then?” Stephen Whipple mused,
aloud.
“Yes, and been turned down on all sides. He has tried till he has
lost hope. He likes Gate City, but is afraid they will be driven to the
road again.”
“And to think that a fellow like that can't find work,” Whipple cried,
indignantly, “when the world is full of grafters and panhandlers!
Brother Matthews, I am interested in those fellows, especially the
oldest one. My list is full, as you know, but I can manage to find
places for the right sort. Couldn't you send him to me right away? I'll
be here to-night after closing time. There won't be anybody else
about, and me and him can talk undisturbed. I'd like to help a chap
like that. You have got me interested. The world is too full of bad
men who are prospering for his sort to go unrewarded.”
“Well, I'll send him, Brother Whipple. God bless you, old man, you
can always be counted on!”
That evening the merchant sat in the light of his green-shaded
gas-lamp at his desk waiting for the expected caller. The outer door
of the great building, which opened on the main street, was ajar,
and was plainly visible to the merchant from his seat. Now, as he
heard his visitor coming, he rose to his feet, pushed his desk-chair
back with his ponderous calves, and stood smiling cordially. As the
young man entered, politely removing his hat, Whipple grasped
Walton's hand and shook it warmly.
“I'm powerfully glad to know you, Mr. Spencer,” he said, “I am,
indeed. I'm told you are a newcomer to our brag town, and as I'm
one of the pioneers, so to speak, I take a personal pride in the
place, and I want to see everybody that drifts this way anchored
here for life. It certainly is the town for fresh young blood. Even old
men can make money here, and I know the young can. Set down,
set down! I'm glad you ran across my long-legged jumping-jack of a
preacher. He is a wheel-horse, I am here to state. If all the churches
in the world were led by men of his stamp, infidelity would die of the
dry rot or burn up with shame.
“I built Matthews' meeting-house, and if I hadn't found a man like
him to fill the pulpit I'd have turned the blamed thing into a
warehouse to store groceries in. But I found him, and he's doing
mighty well—mighty well! He isn't any of your ranting trance
religionists; he's practical, and, in one way, the funniest cuss you
ever laid eyes on. Me and him have big times in our way. He looks
after the souls of men while I sometimes help a little in patching up
their bodies. He tells me that you and a friend of yours haven't made
any business connection yet. My house is pretty well supplied, but
this is our best season of the year, and a good man always comes in
handy. You look like you've got a good head on them broad
shoulders, and I want to give you a start, so if you will show up here
in the morning with your friend, I'll put you to work in the office and
stow him away somewhere.”
“You are very, very kind, Mr. Whipple,” Fred said, a gratified flush
on his face; “but you have had no recommendation of me, and—”
“I don't want none,” the merchant said, firmly. “You see, I've
already heard about you. Long before me and you met you had cast
your bread on the water, and it has already come back. I've heard
about you. Anybody these days can bring a scrap of paper with
indorsements scribbled on it, but the best recommendation is the
sort that crawls along ahead of a fellow. Yes, I've heard about you,
and, to be plain, that's why I sent for you. Even if I didn't have no
opening right now, it would pay me to rub against men that—well,
that believe like you do and act like you have acted.”
“I suppose you mean”—Walton was quite embarrassed now—“I
suppose Mr. Matthews has been speaking of what my friend told him
of our ups-and-downs together; but really I couldn't let that sort of
thing stand as an indorsement of me, Mr. Whipple. Dick is young and
enthusiastic. It seems that he has never had a close friend before,
and he naturally exaggerates my—”
“Say, look here,” the merchant broke in, with a smile, “you really
don't know how funny that sounds. In this day and time, when a
man in my position has to set and listen to folks spout for the hour
about how good and worthy they are, why—well, to see a chap
actually denying the favorable things which have been said behind
his back is a downright curiosity. Why, the very fact that you are
talking this way shows plain enough what you are. Along with what
I've picked up about you and the—the general look of you, now that
you are at close range—why, if you was to lay down a whole batch
of written recommendations I'd chuck 'em in that stove. I'm a judge
of human faces and of men, and I know you mean well, and that is
all I ask.”
“It is very good of you, Mr. Whipple,” Walton said, his glance on
the floor. “I feel like we could get on together. I know I'd do my best
to please you.”
“Well, then, there is nothing more to be said,” old Whipple
answered. “Bring that boy in to-morrow morning, and we'll make
some sort o' a start.”
Fred sat silent. He took a deep breath and raised his eyes to the
genial face in the green light. “I must be frank and open with a man
as generous as you are, Mr. Whipple. If I am to work here we ought
to understand each other thoroughly. There are some things which
you must know about me, or I cannot consent to enter your
employment, for it would be deceiving you.”
“Oh, that's it!” Whipple said, awkwardly. “Still, you mustn't feel
that I am requiring any explanations of—of a private nature, for I am
not.”
“You ought to know more than you do know about me, at all
events,” Walton went on. “I'd feel better if nothing at all was hidden
from your knowledge. I haven't lived right, Mr. Whipple. I went
wrong—frightfully wrong. I got in debt—it is worse than that. I
misappropriated a considerable sum of money belonging to my
father. He is a stern, hard man, and demanded as much of me as he
would have done of a stranger. I left home to escape arrest. You
may think I ought to have submitted to the law. I simply couldn't, for
I felt that my father, when his passion cooled, would regret his step,
and, moreover, I felt that, with my freedom, I could apply myself
and eventually restore the loss.”
“Merciful Father!” Whipple exclaimed, fervently. “Lord have mercy!
To think of a man blessed with a son holding the law over his
repentant head and chasing him from spot to spot over God's green
earth! The child he brought into the world and saw cooing in the
cradle, a little, tiny sprout of his own flesh and blood, made in the
image of the Lord God of Hosts! My boy,” the old man leaned
forward, “shake hands with me. I've often wanted to help young
men in my stormy life, but, God knows, I never felt the desire as
strong as I do now. Just in this little talk I've been drawn more
closely to you than I ever was to a human being before. You are the
right sort, the genuine thing; if I was to turn you adrift, I'd never get
over it. I had a boy once, and I doted on him. He died when he was
a little toddling fellow, and since then I have never been consoled.
But his loss, and the memory of him, has warmed my heart to young
men wherever I meet them. You must come to me, my boy. I feel
sure we'll pull together. In fact, I'd want you at hand, for I'd grieve
to see you falter in your noble undertaking. God will bless your effort
as sure as the stars are shining up there in the heavens to-night.”
“I haven't told you quite all yet,” Walton added, in a low tone. “To
protect myself, I took another name. My real name is—”
“Stop! Don't tell me. That won't make one bit of difference to me,”
Whipple answered, with a sigh, as if he were thinking more of the
young man's former revelations than the one just made. “No doubt it
is best. You say you have determined to make good the loss, and if
bearing another name will help you out, then it can't be wrong. Go
ahead, I'll be your friend; I'll stick to you. I'm glad we came together
to-night. It makes me feel better. I've seen many sorts of human
struggles, but I never saw one that touched me down deep like
yours does. Wait, let me lock up, and I'll walk along a piece with
you.”
Outside, after he had closed the heavy door, the merchant put his
hand on the arm of his companion, and they moved on down the
street together. Suddenly they paused. Whipple swept his fat hand in
a slow gesture toward the skies.
“My boy,” he said, fervently, “this is a wonderful, wonderful old
world. Life seems hard and harsh at times, but when the soul is right
a man can conquer anything. I have my fight to make; you have
yours—stick to it, and may the Lord be with you! Goodnight.”. .
PART II
CHAPTER I

O
LD Stafford had changed wonderfully in the six years which
passed after Fred Walton's flight. The building of President
Galt's trunk-line to the sea had marked the turning-point in
the town's career. The older portion of the place remained quite as it
was, but new suburbs and new centres of commerce had sprung up
beyond the old incorporated limits. Where farms, fields, and
pastures had once been, now lay even, well-graded, and electric-
lighted streets. No small city in the South had a better freight-rate to
all points, and this had brought about the establishment of various
manufacturing enterprises which had greatly increased the
population. The clang and clatter of new growth was in the air;
speculation in building-sites was rife. The modest price of one day
was the jest of the next. Owning a great deal of the land along the
new railway, General Sylvester was now more wealthy than ever, and
the new interest in life had given him back his youth and health.
As for Kenneth Galt, he had scarcely spent a day in the town of his
birth since his hurried journey to New York to meet the capitalists
whose co-operation had made the road a certainty. His explanation
to Sylvester was that other points on the long line constantly
demanded his attention. His old home was still cared for by Mrs.
Wilson as housekeeper and John Dilk as gardener, and now and then
a false report had emanated from these proud and worshipful
menials that the distinguished owner was coming back to reside
there permanently. Indeed, he had promised General Sylvester to do
so time after time, only to make more delays and more excuses.
“He's coming this time sure,” the old soldier said to his nephew on
the veranda one day in the early part of the present summer. “I had
a letter from him this morning, in which he promised to come and
spend the hot weather here and take a good long rest. Mrs. Wilson
said, also, that he had written her about renovating his rooms, so I
reckon it is settled. And when he comes you will see that I was right
about my prophecy concerning him and Madge. He's a woman-hater,
they say—won't have a thing to do with society; and, quiet and
reserved as your sister is, the two will naturally drift together. I'll be
glad to have him back. That shady old place, with its early
associations, will fairly make him over. When I spent that week with
him in Savannah I naturally expected to find him at the top of the
social heap, but he went nowhere at all, and even seemed to shun
the men who extended courtesies to him. He's had too big a load on
him; his face shows wrinkles, and his hair is turning at the temples.”
“Yes, he is a strange chap,” Dearing answered. “I have been
thrown with him in Atlanta several times of late, and while he really
seemed glad to see me, and was cordial enough, in a way, I couldn't
exactly make him out. As usual, I found him moping over his favorite
books, and every bit as anxious, as of old, to prove that the grave
ends everything. That will ruin any man, Uncle Tom. When a fellow
actually gets to fighting the belief that we are more than sticks and
stones he can't rise very high in any spiritual sense. Why, Kenneth
has even reached the point of defending some of the lowest things
that men do. He and I were walking away out in the outskirts of the
city one night. He had asked me to go, because he wanted to avoid
some clubmen who were bent on having him preside at a banquet
given by the Chamber of Commerce. We were all alone, and it was
dark. He had asked me, I remember, if any news had come as to the
whereabouts of Fred Walton, and I had told him that nothing at all
had been heard except that his father had cut him off forever. To my
astonishment, Kenneth actually sighed. Then I distinctly heard him
muttering to himself: 'Poor fellow. Poor chap! He's been treated like
a dog!”' “Huh, the idea!” Sylvester broke in. “Well, that's like
Kenneth. He is always ready to take up for somebody or something
that no one else believes in.”
“Well, feeling as I did, and knowing what I do of the case,”
Dearing continued, warmly, “I couldn't hold my tongue. I didn't leave
a grain of sand for Fred Walton to stand on, and it made me hot for
Galt not to agree with me. He made some weak remark about men
obeying natural laws, and being cursed with uncontrollable passions,
and the like; but I flatter myself that I silenced him. I gave him a
picture of that beautiful girl's isolated life with her son and old
mother, wholly ostracized in the only community they had ever
known or loved. I saw, then, that I had touched his sympathies in
another direction.
“'You think,' he said, 'that Walton ought, even now, to go back
and marry her—at this late date?”
“I told him that I had grave doubts as to whether a woman who
had suffered as she had at a man's hands would ever want to see
her betrayer again, and he answered that he felt sure she wouldn't.
Then he asked about the boy. You know, he was always fond of
children—that is one redeeming quality he has, and it makes me
hope that he isn't so heartless as he would have us believe. He
listened attentively to all I said about Lionel, even asking me
questions as to how the child looked and how he amused himself.
When I told him that the little fellow was completely cut off from
other children, and that his association only with his mother and
grandmother had made him act and speak more like an older person
than a child, he seemed actually shocked.”
“'You don't mean to tell me,' he said, 'that the people of old
Stafford would turn against a helpless child because of any fault or
mistake of its parents!'
“I explained to him that it was mostly due to the pride of his
mother, and to the natural fear that such an intelligent boy, and one
so sensitive and observant as he is, might learn of his misfortune
and suffer from it. That conversation raised Kenneth Galt in my
estimation, Uncle Tom. I know now that he has true feeling and
sympathy for the unfortunate, and that his ambition is not all there
is to him.”
“I must confess that the child has greatly interested me,” the
General said. “From my window I can see him playing in that narrow
yard, always dressed neatly, and as strong and straight as an Indian
in his bearing. I have never seen him outside the fence. I have
stopped to speak to him once or twice in passing, and have been
actually charmed by his face and manner. I don't think I ever heard
of a case exactly like his. Of course, there have been thousands of
children born like that in straitlaced communities, but I never heard
of one being brought up in that prison-like way. It surely is wrong,
and it will make the truth all the harder to bear when it does come
out, as it must sooner or later. She is a wonderful woman—I started
to say girl, for she seems almost like a child to me with that sad,
young face, and wistful, artistic beauty. I have met her mother on
the street a few times, her old face thickly veiled, but I have not
seen Dora or the child away from the cottage.”
“As their family doctor,” said Dearing, “I urged Dora to go out
herself for exercise and to take the boy with her. At first she flatly
refused. I frightened her, however, by saying that the constant
confinement would injure Lionel's health. Since then she has taken
him with her in fine weather when she goes sketching in the woods
and swamp back of the cottage, but she is as shy as a fawn about it.
I venture to say that no one has ever met her on those excursions.
I've seen mother-love, Uncle Tom, in all its phases. I've met it at the
death-beds of scores of children, but the love between that
unfortunate mother and child is the prettiest thing on earth. No pair
of lovers were ever more constant and affectionate. Lionel is really a
sort of psychological oddity in his way. I have a theory that the
mother's morbid suffering was in some prenatal way stamped on her
offspring.' He is queerly supersensitive for one so young, and seems
constantly afraid that he won't be liked. He is rather fond of me—
perhaps it is because I'm the only visitor at the house; and when I
take him in my lap to hold him, I can see that he enjoys it as if it
were an unusual luxury. He closes his eyes sometimes and smiles,
and says he wants to go to sleep that way. Then he will ask me over
and over again if I love him. After being told that I do, he will detect
some slight change in my face or voice and cry out, 'Now, you don't
like me—do you?' I am not sentimental, Uncle Tom, but that little
chap's condition has worried me a lot. I pity him as I've never pitied
a human being before.”
“I have often wondered whether Madge has taken notice of him,”
General Sylvester remarked, reflectively. “A woman is hard to read
on the surface, and while Madge never mentions Fred Walton's
name any more than if he were dead, I've been afraid that the mere
sight of his child might keep the old memory alive. Do you know, my
son, a woman will condone exactly that failing in a man more quickly
than any other? I suppose they lay most of the blame on the woman
in the case. A high-strung creature like your sister wouldn't for a
moment consider herself a rival of a fallen woman, and it may be
that the explanation of her never having shown interest in other
men is that—”
“That she still cares for the rascal?” Dearing broke in, his face
darkening.
“Yes, and that she still clings to some sort of faith in his
constancy,” the General added. “You can't crush love in a woman's
heart so long as she believes she is loved by a man who is longing
for her and is kept away by adverse circumstances. You see, if our
dear girl attributes Walton's predicament to a simple act of low,
impulsive passion, and believes that he loved her, and her alone, in a
pure way, why—”
“I see, I see, and I am afraid you may be right,” Dearing said,
bitterly. “And instead of curing her, the scoundrel's absence is only
making the thing worse. Did you tell her about Kenneth's coming?”
“Yes, only an hour ago, and it seemed to me that she was rather
pleased. She remarked that she was glad John Dilk had kept up the
place so well, and that the flowers would gratify him. I really fancied
that she was more pleased by the news than she was willing to
show, for she changed the subject by offering to play for me.”
At this juncture a woman came round the house hurriedly, wiping
her red, bare arms, and trying to adjust the damp dress she wore. It
was Mrs. Chumley, the washerwoman. Her tawny hair was
disarranged, and her fat, freckled face flushed with an excitement
that was almost pleasurable.
“Oh, here you are, Doctor Wynn!” she panted. “I hain't been told
to come; in fact, them highfalutin' neighbors of mine never let a
body know anything they can get out of. But Mrs. Barry is having
another of her falling spells. She was on the side porch brushing
little Lionel's head when I heard her cry out to Dora for help, and
then she struck the floor of the kitchen with a thump you could have
heard up here if you'd been listening.”
“Well, I'll run down,” Dearing said to his uncle. “It may not be very
serious. She is subject to such attacks.”
CHAPTER II

H
URRYING down through the grounds, and vaulting over the
low boundary fence, Dearing approached the gate of the
Barry cottage just as Dora came out. Pretty as she had been
in girlhood, she was rarely beautiful as a fully developed woman.
And to-day, as ever, Dearing stood before her in absolute awe of her
rare, exquisite, and appealing personality.
“She's had another attack, Wynn!” Dora said, with a brave effort
to steady her faltering voice. “I really thought she was dying, and I
suppose I screamed. She looked so bad for a few moments! Her
face turned purple, and she lost consciousness. She came to herself
a moment ago, and is still awake. Will you see her?”
He went to the sick woman's room on tiptoe. Seated in a chair at
the head of the bed, and waving a palm-leaf fan to and fro, to keep
the flies from his grandmother's face, was Lionel, his great, serious
eyes, so like his mother's, filled with anxiety. He rose as Dearing
entered, and moved round to the other side of the bed, but he still
waved the fan and stood staring anxiously.
“I thought I was gone that time, Doctor Wynn,” Mrs. Barry said,
with a wan smile, as he took her hand to test her pulse.
“Well, you certainly are far from it now,” he laughed, reassuringly.
“I believe it would take a regiment of soldiers to put you out of
business. That was only a fainting spell brought on by too close
confinement to the house. You must get out more; that's all you
need. Now, take a good nap and you will be all right.” He nodded
and smiled reassuringly at Dora, who stood at the foot of the bed.
She followed him from the room, seeing that he wished to speak to
her.
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