You are on page 1of 41

(eBook PDF) Starting Out with Java:

Early Objects 5th Edition


Visit to download the full and correct content document:
https://ebooksecure.com/download/ebook-pdf-starting-out-with-java-early-objects-5th-
edition/
Contents in Brief

Preface xv

Chapter 1 Introduction to Computers and Java 1

Chapter 2 Java Fundamentals 31

Chapter 3 A First Look at Classes and Objects 129

Chapter 4 Decision Structures 193

Chapter 5 Loops and Files 279

Chapter 6 A Second Look at Classes and Objects 357

Chapter 7 Arrays and the ArrayList Class 449

Chapter 8 Text Processing and Wrapper Classes 547

Chapter 9 Inheritance 605

Chapter 10 Exceptions and Advanced File I/O 681

Chapter 11 GUI Applications—Part 1 739

Chapter 12 GUI Applications—Part 2 841

Chapter 13 Applets and More 909

Chapter 14 Creating GUI Applications with JavaFX 983

Chapter 15 Recursion 1039

Chapter 16 Databases 1067


Appendix A Getting Started with Alice 2 1163
Index 1189
Credits 1205
Appendixes B–M Available on the book’s online resource page
Case Studies 1–5 Available on the book’s online resource page vii
This page intentionally left blank
Contents

Preface xv

Chapter 1 Introduction to Computers and Java 1


1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Why Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Computer Systems: Hardware and Software . . . . . . . . . . . . . . . . . . . . . 2
1.4 Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5 What Is a Program Made of? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.6 The Programming Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.7 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Review Questions and Exercises 24
Programming Challenge 28

Chapter 2 Java Fundamentals 31


2.1 The Parts of a Java Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.2 The System.out.print and System.out.println Methods, and the
Java API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.3 Variables and Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.4 Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.5 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2.6 Combined Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
2.7 Conversion between Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . 70
2.8 Creating Named Constants with final . . . . . . . . . . . . . . . . . . . . . . . . 74
2.9 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
2.10 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
2.11 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
2.12 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
2.13 Reading Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
2.14 Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
2.15 The System.out.printf Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
2.16 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Review Questions and Exercises 118
Programming Challenges 123
ix
x Contents

Chapter 3 A First Look at Classes and Objects 129


3.1 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
3.2 More about Passing Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
3.3 Instance Fields and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
3.4 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
3.5 A BankAccount Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
3.6 Classes, Variables, and Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
3.7 Packages and import Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
3.8 Focus on Object-Oriented Design: Finding the Classes
and Their Responsibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
3.9 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Review Questions and Exercises 183
Programming Challenges 187

Chapter 4 Decision Structures 193


4.1 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
4.2 The if-else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
4.3 The Payroll Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
4.4 Nested if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
4.5 The if-else-if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
4.6 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
4.7 Comparing String Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
4.8 More about Variable Declaration and Scope . . . . . . . . . . . . . . . . . . . 235
4.9 The Conditional Operator (Optional) . . . . . . . . . . . . . . . . . . . . . . . . . 237
4.10 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
4.11 Formatting Numbers with the DecimalFormat Class . . . . . . . . . . . . . . 248
4.12 Focus on Problem Solving: The SalesCommission Class. . . . . . . . . . . . 254
4.13 Generating Random Numbers with the Random Class . . . . . . . . . . . . . 261
4.14 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Review Questions and Exercises 268
Programming Challenges 273

Chapter 5 Loops and Files 279


5.1 The Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . 279
5.2 The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
5.3 Using the while Loop for Input Validation . . . . . . . . . . . . . . . . . . . . . 290
5.4 The do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
5.5 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
5.6 Running Totals and Sentinel Values . . . . . . . . . . . . . . . . . . . . . . . . . . 307
5.7 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
5.8 The break and continue Statements . . . . . . . . . . . . . . . . . . . . . . . . . . 320
5.9 Deciding Which Loop to Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
5.10 Introduction to File Input and Output . . . . . . . . . . . . . . . . . . . . . . . . 321
5.11 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Contents xi

Review Questions and Exercises 342


Programming Challenges 348

Chapter 6 A Second Look at Classes and Objects 357


6.1 Static Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
6.2 Overloaded Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
6.3 Overloaded Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
6.4 Passing Objects as Arguments to Methods . . . . . . . . . . . . . . . . . . . . . 376
6.5 Returning Objects from Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
6.6 The toString Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
6.7 Writing an equals Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
6.8 Methods That Copy Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
6.9 Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
6.10 The this Reference Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
6.11 Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
6.12 Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
6.13 Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
6.14 Focus on Object-Oriented Design: Class Collaboration . . . . . . . . . . . 431
6.15 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Review Questions and Exercises 436
Programming Challenges 441

Chapter 7 Arrays and the ArrayList Class 449


7.1 Introduction to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
7.2 Processing Array Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
7.3 Passing Arrays as Arguments to Methods . . . . . . . . . . . . . . . . . . . . . . 472
7.4 Some Useful Array Algorithms and Operations . . . . . . . . . . . . . . . . . . 476
7.5 Returning Arrays from Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
7.6 String Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
7.7 Arrays of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
7.8 The Sequential Search Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
7.9 The Selection Sort and the Binary Search Algorithms . . . . . . . . . . . . . 501
7.10 Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
7.11 Arrays with Three or More Dimensions . . . . . . . . . . . . . . . . . . . . . . . 521
7.12 Command-Line Arguments and Variable-Length Argument Lists . . . . 522
7.13 The ArrayList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
7.14 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
Review Questions and Exercises 535
Programming Challenges 539

Chapter 8 Text Processing and Wrapper Classes 547


8.1 Introduction to Wrapper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
8.2 Character Testing and Conversion with the Character Class . . . . . . . 548
8.3 More about String Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
xii Contents

8.4 The StringBuilder Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569


8.5 Tokenizing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
8.6 Wrapper Classes for the Numeric Data Types . . . . . . . . . . . . . . . . . . . 587
8.7 Focus on Problem Solving: The TestScoreReader Class. . . . . . . . . . . . 591
8.8 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
Review Questions and Exercises 595
Programming Challenges 599

Chapter 9 Inheritance 605


9.1 What Is Inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
9.2 Calling the Superclass Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
9.3 Overriding Superclass Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
9.4 Protected Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
9.5 Classes That Inherit from Subclasses. . . . . . . . . . . . . . . . . . . . . . . . . . 639
9.6 The Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
9.7 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
9.8 Abstract Classes and Abstract Methods . . . . . . . . . . . . . . . . . . . . . . . 651
9.9 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
9.10 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
Review Questions and Exercises 669
Programming Challenges 675

Chapter 10 Exceptions and Advanced File I/O 681


10.1 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
10.2 Throwing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
10.3 Advanced Topics: Binary Files, Random Access Files, and Object
Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712
10.4 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728
Review Questions and Exercises 729
Programming Challenges 735

Chapter 11 GUI Applications—Part 1 739


11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
11.2 Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
11.3 Creating Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753
11.4 Layout Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 780
11.5 Radio Buttons and Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
11.6 Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
11.7 Focus on Problem Solving: Extending the JPanel Class . . . . . . . . . . . 812
11.8 Splash Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
11.9 Using Console Output to Debug a GUI Application . . . . . . . . . . . . . . 827
11.10 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831
Review Questions and Exercises 831
Programming Challenges 836
Contents xiii

Chapter 12 GUI Applications—Part 2 841


12.1 Read-Only Text Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841
12.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842
12.3 Combo Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859
12.4 Displaying Images in Labels and Buttons . . . . . . . . . . . . . . . . . . . . . . 865
12.5 Mnemonics and Tool Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 871
12.6 File Choosers and Color Choosers . . . . . . . . . . . . . . . . . . . . . . . . . . . 873
12.7 Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 877
12.8 More about Text Components: Text Areas and Fonts . . . . . . . . . . . . . 886
12.9 Sliders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 891
12.10 Look and Feel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895
12.11 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897
Review Questions and Exercises 898
Programming Challenges 903

Chapter 13 Applets and More 909


13.1 Introduction to Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 909
13.2 A Brief Introduction to HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911
13.3 Creating Applets with Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921
13.4 Using AWT for Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929
13.5 Drawing Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934
13.6 Handling Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954
13.7 Timer Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965
13.8 Playing Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969
13.9 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974
Review Questions and Exercises 974
Programming Challenges 980

Chapter 14 Creating GUI Applications with JavaFX 983


14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 983
14.2 Scene Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985
14.3 Using Scene Builder to Create JavaFX Applications . . . . . . . . . . . . . . . 987
14.4 Writing the Application Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1001
14.5 RadioButtons and CheckBoxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1011
14.6 Displaying Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025
14.7 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1030
Review Questions and Exercises 1030
Programming Challenges 1034

Chapter 15 Recursion 1039


15.1 Introduction to Recursion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1039
15.2 Solving Problems with Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 1042
15.3 Examples of Recursive Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1046
15.4 A Recursive Binary Search Method . . . . . . . . . . . . . . . . . . . . . . . . . . 1053
xiv Contents

15.5 The Towers of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1056


15.6 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1060
Review Questions and Exercises 1061
Programming Challenges 1064

Chapter 16 Databases 1067


16.1 Introduction to Database Management Systems . . . . . . . . . . . . . . . 1067
16.2 Tables, Rows, and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1073
16.3 Introduction to the SQL SELECT Statement . . . . . . . . . . . . . . . . . . . . 1077
16.4 Inserting Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1098
16.5 Updating and Deleting Existing Rows . . . . . . . . . . . . . . . . . . . . . . . 1101
16.6 Creating and Deleting Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1110
16.7 Creating a New Database with JDBC . . . . . . . . . . . . . . . . . . . . . . . . 1114
16.8 Scrollable Result Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1115
16.9 Result Set Meta Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1117
16.10 Displaying Query Results in a JTable . . . . . . . . . . . . . . . . . . . . . . . . 1120
16.11 Relational Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1131
16.12 Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152
16.13 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1154
Review Questions and Exercises 1155
Programming Challenges 1160

Appendix A Getting Started with Alice 2 1163


Index 1189
Credits 1205
Available on the book’s online resource page at
www.pearsonhighered.com/gaddis:
Appendix B The ASCII/Unicode Characters
Appendix C Operator Precedence and Associativity
Appendix D Java Key Words
Appendix E Installing the JDK and JDK Documentation
Appendix F Using the javadoc Utility
Appendix G More about the Math Class
Appendix H Packages
Appendix I Working with Records and Random-Access Files
Appendix J Installing Java DB
Appendix K The QuickSort Algorithm
Appendix L Answers to Checkpoints Questions
Appendix M Answers to Odd-Numbered Review Questions
Case Study 1 The Amortization Class
Case Study 2 The PinTester Class
Case Study 3 Parallel Arrays
Case Study 4 The SerialNumber Class
Case Study 5 A Simple Text Editor Application
Preface

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

Early Objects, Late Graphics


The approach taken by this text can be described as “early objects, late graphics.” The
student is introduced to object-oriented programming (OOP) early in the book. The funda-
mentals of control structures, classes, and the OOP paradigm are thoroughly covered before
moving on to graphics and more powerful applications of the Java language.
As with all the books in the Starting Out With series, the hallmark of this text is its clear,
friendly, and easy-to-understand writing. In addition, it is rich in example programs that are
concise and practical.

New to this edition:


● A New Chapter on JavaFX: New to this edition is Chapter 14: Creating GUI Applications
with JavaFX. JavaFX is the next-generation toolkit for creating GUIs and graphical appli-
cations in Java and is bundled with Java 7 and Java 8. This new chapter introduces the
student to the JavaFX library and shows how to use Scene Builder (a free download from
Oracle) to visually design GUIs. The chapter is written in such a way that it is indepen-
dent from the existing chapters on Swing and AWT. The instructor can choose to skip the
Swing and AWT chapters and go straight to JavaFX, or cover all of the GUI chapters.
● Rewritten Database Chapter: The database chapter, which is now Chapter 16, has
been rewritten with more examples and more detailed explanations of various data-
base operations.
● Coverage of System.out.printf Has Been Expanded: The section on System.out.printf
in Chapter 2 has been completely rewritten and expanded to include diagrams and cover-
age of additional format specifiers.
● System.out.printf Is Primarily Used For Formatting Console Output: In this edition,
System.out.printf is used as the primary method for formatting output in console pro-
grams. The DecimalFormat class is still introduced, but it is used to format numbers in
GUI applications.

xv
xvi Preface

● Discussion of Nested Loops Has Been Expanded: In Chapter 4 the section on nested loops
has been expanded to include an In the Spotlight section highlighting the use of nested
loops to print patterns.
● Usage of Random Numbers Has Been Expanded: In Chapter 4 the section on random
numbers has been expanded and now includes In the Spotlight sections demonstrating
how random numbers can be used to simulate the rolling of dice.
● New Motivational Example of Classes Has Been Added to Chapter 6: In Chapter 6,
a new motivational example of classes has been added. The example shows how a
variation of the game of Cho-Han can be simulated with classes that represent the
players, a dealer, and the dice.
● Multi-Catch Exception Handling: A discussion of multi-catch exception handling has
been added to Chapter 10.
● Equipping Swing GUI Applications with a Static main Method is Introduced Earlier: In
Chapter 11, GUI Applications—Part 1, the topic of equipping a GUI class with a static
main method has been moved to a point very early in the chapter.
● New Exercises and Programming Problems: New, motivational programming problems
have been added to many of the chapters.

Organization of the Text


The text teaches Java step-by-step. Each chapter covers a major set of topics and builds
knowledge as students progress through the book. Although the chapters can be easily
taught in their existing sequence, there is some flexibility. Figure P-1 shows chapter depen-
dencies. Each box represents a chapter or a group of chapters. A solid-line arrow points
from one chapter to the chapter that must be covered previously. A dotted-line arrow indi-
cates that only a section or minor portion of the chapter depends on another chapter.

Brief Overview of Each Chapter


Chapter 1: Introduction to Computers and Java. This chapter provides an
introduction to the field of computer science, and covers the fundamentals of hardware,
software, and programming languages. The elements of a program, such as key words,
variables, operators, and punctuation are discussed through the examination of a simple
program. An overview of entering source code, compiling it, and executing it is presented.
A brief history of Java is also given. The chapter concludes with a primer on OOP.

Chapter 2: Java Fundamentals. This chapter gets the student started in Java by
introducing data types, identifiers, variable declarations, constants, comments, program
output, and arithmetic operations. The conventions of programming style are also intro-
duced. The student learns to read console input with the Scanner class, or as an option,
through dialog boxes with JOptionPane.

Chapter 3: A First Look at Classes and Objects. This chapter introduces the stu-
dent to classes. Once the student learns about fields and methods, UML diagrams are
introduced as a design tool. The student learns to write simple void methods, as well
as simple methods that return a value. Arguments and parameters are also discussed.
Finally, the student learns how to write constructors, and the concept of the default con-
structor is discussed. A BankAccount class is presented as a case study, and a section on
Preface xvii

Figure P-1 Chapter Dependencies

Chapters 1 - 7 (Cover in Order)


Java Fundamentals

Depend On

Chapter 8
Chapter 9 Chapter 16 Chapter 15
Text Processing and
Inheritance Databases Recursion
Wrapper Classes
Some examples in
Chapter 16 use GUIs,
which are introduced
Depends On Depends On Depends On in Chapter 11.

Chapter 10 Chapter 14 Chapter 11 Some examples in


Creating GUI Chapter 15 are applets,
Exceptions and Applications with
GUI Applications,
which are introduced
Advanced File I/O JavaFX Part 1 in Chapter 13.

Depends On Depends On

Chapter 12
Chapter 13
GUI Applications,
Applets and More
Part 2

object-oriented design is included. This section leads the students through the process
of identifying classes and their responsibilities within a problem domain. There is also a
section that briefly explains packages and the import statement.

Chapter 4: Decision Structures. Here the student explores relational operators and
relational expressions and is shown how to control the flow of a program with the if,
if/else, and if/else if statements. The conditional operator and the switch statement
are also covered. This chapter also discusses how to compare String objects with the
equals, compareTo, equalsIgnoreCase, and compareToIgnoreCase methods. Formatting
numeric output with the DecimalFormat class is covered. An object-oriented case study
shows how lengthy algorithms can be decomposed into several methods.

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

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

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

Chapter 8: Text Processing and Wrapper Classes. This chapter discusses the
numeric and character wrapper classes. Methods for converting numbers to strings, test-
ing the case of characters, and converting the case of characters are covered. Autoboxing
and unboxing are also discussed. More String class methods are covered, including using
the split method to tokenize strings. The chapter also covers the StringBuilder and
StringTokenizer classes.

Chapter 9: Inheritance. The study of classes continues in this chapter with the sub-
jects of inheritance and polymorphism. The topics covered include superclass and subclass
constructors, method overriding, polymorphism and dynamic binding, protected and
package access, class hierarchies, abstract classes and methods, and interfaces.

Chapter 10: Exceptions and Advanced File I/O. In this chapter the student learns
to develop enhanced error trapping techniques using exceptions. Handling an exception is
covered, as well as developing and throwing custom exceptions. This chapter also discusses
advanced techniques for working with sequential access, random access, text, and binary files.

Chapter 11: GUI Applications—Part 1. This chapter presents the basics of develop-
ing graphical user interface (GUI) applications with Swing. Fundamental Swing compo-
nents and the basic concepts of event-driven programming are covered.

Chapter 12: GUI Applications—Part 2. This chapter continues the study of GUI
application development. More advanced components, as well as menu systems and
look-and-feel, are covered.

Chapter 13: Applets and More. Here the student applies his or her knowledge of
GUI development to the creation of applets. In addition to using Swing applet classes,
Abstract Windowing Toolkit classes are also discussed for portability. Drawing simple
graphical shapes is also discussed.

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

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


technique. Numerous examples of recursion are demonstrated.

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

Appendix A . Getting Started with Alice


Appendixes B–M and Case Studies 1-5 are available on the book’s online resource page at
www.pearsonhighered.com/gaddis.

Features of the Text


Concept Statements Each major section of the text starts with a concept statement.
This statement summarizes the ideas of the section.

Example Programs The text has an abundant number of complete example programs,
each designed to highlight the topic currently being studied. In most cases, these are prac-
tical, real-world examples. Source code for these programs is provided so that students
can run the programs themselves.

Program Output After each example program there is a sample of its screen output.
This immediately shows the student how the program should function.

Checkpoints
Checkpoints are questions placed throughout each chapter as a self-test study aid. Answers
for all Checkpoint questions are found in Appendix L (available for download) so students
can check how well they have learned a new topic. To download Appendix L, go to the
Gaddis resource page at www.pearsonhighered.com/gaddis.

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

WARNING! Warnings are notes that caution the student about certain Java features, pro-
gramming techniques, or practices that can lead to malfunctioning programs or lost data.

VidoeNotes. A series of online videos, developed specifically for this book, are available
for viewing at www.pearsonhighered.com/gaddis. Icons appear throughout the text alert-
ing the student to videos about specific topics.

Case Studies Case studies that simulate real-world applications appear in many chap-
ters throughout the text, with complete code provided for each. These case studies are
designed to highlight the major topics of the chapter in which they appear.

Review Questions and Exercises Each chapter presents a thorough and diverse set
of review questions and exercises. They include Multiple Choice and True/False, Find the
Error, Algorithm Workbench, and Short Answer.
xx Preface

Programming Challenges Each chapter offers a pool of programming challenges


designed to solidify students’ knowledge of topics at hand. In most cases the assignments
present real-world problems to be solved.

In the Spotlight. Many of the chapters provide an In the Spotlight section that presents a pro-
gramming problem, along with detailed, step-by-step analysis showing the student how to solve it.

Supplements
Companion Website
Many student resources are available for this book from the book’s Companion Website.
The following items are available at www.pearsonhighered.com/gaddis using the Access
Code bound into the front of the book:
● The source code for each example program in the book
● Access to the book’s companion VideoNotes
● Appendixes B–M (listed in the Table of Contents)
● A collection of five valuable Case Studies (listed in the Table of Contents)
● Links to download the Java™ Development Kit
● Links to download numerous programming environments, including jGRASP™,
Eclipse™, TextPad™, NetBeans™, JCreator, and DrJava

Online Practice and Assessment with MyProgrammingLab


MyProgrammingLab helps students fully grasp the logic, semantics, and syntax of pro-
gramming. Through practice exercises and immediate, personalized feedback, MyProgram-
mingLab improves the programming competence of beginning students who often struggle
with the basic concepts and paradigms of popular high-level programming languages.
A self-study and homework tool, a MyProgrammingLab course consists of hundreds of
small practice problems organized around the structure of this textbook. For students, the
system automatically detects errors in the logic and syntax of their code submissions and
offers targeted hints that enable students to figure out what went wrong—and why. For
instructors, a comprehensive gradebook tracks correct and incorrect answers and stores the
code inputted by students for review.
MyProgrammingLab is offered to users of this book in partnership with Turing’s Craft, the
makers of the CodeLab interactive programming exercise system. For a full demonstration,
to see feedback from instructors and students, or to get started using MyProgrammingLab
in your course, visit www.myprogramminglab.com.

Instructor Resources
The following supplements are available to qualified instructors only. Visit the Pearson
Education Instructor Resource Center (www.pearsonhighered.com/irc) for information on
how to access them:
● Answers to all Review Questions in the text
● Solutions for all Programming Challenges in the text
● PowerPoint presentation slides for every chapter
● Computerized test bank
Preface xxi

Acknowledgments
There have been many helping hands in the development and publication of this text. I
would like to thank the following faculty reviewers for their helpful suggestions and exper-
tise during the production of this text:

Rebecca Caldwell Kurt Kominek


Winston-Salem State University Northeast State Community College
Dan Dao Kevin Mess
Richland College College of Southern Nevada
Naser Heravi Lisa Olivieri
College of Southern Nevada Chestnut Hill College
Deborah Hughes Mark Swanson
Lyndon State College Southeast Technical
Nicole Jiao
South Texas College

Reviewers For Previous Editions


Ahmad Abuhejleh James Chegwidden
University of Wisconsin– Tarrant County College
River Falls
Kay Chen
Colin Archibald Bucks County Community College
Valencia CC
Brad Chilton
Ijaz Awani Tarleton State University
Savannah State University
Diane Christie
Dr. Charles W. Bane University of Wisconsin–Stout
Tarleton State University
Cara Cocking
Dwight Barnett Marquette University
Virginia Tech
Walter C. Daugherity
Asoke Bhattacharyya Texas A&M University
Saint Xavier University, Chicago
Michael Doherty
Marvin Bishop University of the Pacific
Manhattan College
Jeanne M. Douglas
Heather Booth University of Vermont
University Tennessee—Knoxville
Sander Eller
David Boyd California Polytechnic University—
Valdosta University Pomona
Julius Brandstatter Brooke Estabrook-Fishinghawk
Golden Gate University Mesa Community College
Kim Cannon Mike Fry
Greenville Tech Lebanon Valley College
xxii Preface

Georgia R. Grant Steve Newberry


College of San Mateo Tarleton State University
Chris Haynes Lynne O’Hanlon
Indiana University Los Angeles Pierce College
Ric Heishman Merrill Parker
Northern Virginia Community College Chattaonooga State Technical
Community College
Deedee Herrera
Dodge City Community College Bryson R. Payne
North Georgia College and State
Mary Hovik
University
Lehigh Carbon Community College
Rodney Pearson
Brian Howard
Mississippi State University
DePauw University
Peter John Polito
Norm Jacobson
Springfield College
University of California at Irvine
Charles Robert Putnam
Dr. Stephen Judd
California State University,
University of Pennsylvania
Northridge
Harry Lichtbach
Dr. Y. B. Reddy
Evergreen Valley College
Grambling State University
Michael A. Long
Carolyn Schauble
California State University, Chico
Colorado State University
Tim Margush
Bonnie Smith
University of Akron
Fresno City College
Blayne E. Mayfield
Daniel Spiegel
Oklahoma State University
Kutztown University
Scott McLeod
Caroline St. Clair
Riverside Community College
North Central College
Dean Mellas
Karen Stanton
Cerritos College
Los Medanos College
Georges Merx
Peter H.Van Der Goes
San Diego Mesa College
Rose State College
Martin Meyers
Tuan A Vo
California State University, Sacramento
Mt. San Antonio College
Pati Milligan
Xiaoying Wang
Baylor University
University of Mississippi
Godfrey Muganda
North Central College
Preface xxiii

Special thanks goes to Chris Rich for his assistance with the JavaFX chapter. I would like to
thank my family for all the patience, love, and support they have shown me throughout this
project. I would also like to thank everyone at Pearson Education for making the Starting
Out With series so successful. I am extremely fortunate to have Matt Goldstein as my edi-
tor. I am also fortunate to work with Yez Alyan and the computer science marketing team
at Pearson. They do a great job getting my books out to the academic community. I had
a great production team led by Scott Disanno, Kayla Smith-Tarbox, and Gregory Dulles.
Thanks to you all!

About the Author


Tony Gaddis is the principal author of the Starting Out With series of textbooks. Tony has
nearly twenty years experience teaching computer science courses at Haywood Community
College in North Carolina. He is a highly acclaimed instructor who was previously selected
as the North Carolina Community College Teacher of the Year and has received the Teach-
ing Excellence award from the National Institute for Staff and Organizational Develop-
ment. Besides Java™ books, the Starting Out series includes introductory books using the
C++ programming language, Microsoft® Visual Basic®, Microsoft® C#®, Python, Pro-
gramming Logic and Design, Alice, and App Inventor, all published by Pearson.
get with the programming
Through the power of practice and immediate personalized
feedback, MyProgrammingLab improves your performance.


MyProgrammingLab
Learn more at www.myprogramminglab.com
Another random document with
no related content on Scribd:
CHAPTER XIV
FURTHER RESEARCHES

Last week, we had a few days of such cool, bright, windless


weather, that it seemed as if a St. Luke’s summer had set in. Now,
however, the icy gales from the east are once more blowing round the
boma of Newala, and we had rain on Michaelmas Day, which was
somewhat early. This must have been a signal universally understood
by young and old; for I am no longer besieged by the hitherto
inevitable boys, and my old men, too, have ceased their visits.
Fortunately, I have been able to pump the old gentlemen so
effectually in the course of the last few weeks that I could leave at
once, quite happy in the possession of an enormous stock of notes,
were I not detained by the linguistic inquiries which I am now set on
making. It is quite impossible to give here even the merest indication
of the knowledge so far gained as to all these more or less strange
customs and usages. The details will have a place in official and other
documents to the preparation of which the leisure of many coming
terms will have to be sacrificed; here I can only indicate such
prominent points as are calculated to interest every civilized person.
Personal names among the natives offer an unlimited field for
research. Where Islam has already gained a footing, Arab names are
prevalent. The Makonde askari Saidi bin Musa keeps step with his
comrade Ali bin Pinga from Nyasa, and Hasani from Mkhutu
marches behind the Yao porter Hamisi. Among the interior tribes the
division into clans predominates as a principle of social
classification, and therefore, even in the case of converts to
Christianity, the baptismal name is followed by the clan name. Daudi
(David) Machina is the name of the native pastor at Chingulungulu,
and the presumptive successor to Matola I and Matola II calls
himself Claudio Matola. We shall have something more to say about
these clan names later on.
The meaning of the names is often equally interesting. My carriers
alone have already provided me with a good deal of amusement in
this respect, the appellations they go by being in most cases
exceedingly absurd. Pesa mbili (“Mr. Twopence”) is as familiar to us
as his friend Kofia tule, the tall man with the little flat cap, Kazi
Ulaya, the man who works for the European and Mambo sasa
—“Affairs of to-day.” Besides these, the following gentlemen are
running about among the two dozen who compose my faithful
retinue:—Mr. Blanket (Kinyamwezi bulangeti, corrupted from the
English word), Mr. Cigarette (no commentary needed), Kamba Ulaya
(European rope, i.e. hemp rope as distinguished from native cordage
of cocoa-nut fibre or palm-leaf twist), Mr. Mountain (Kilima) and
Messrs. Kompania and Kapella (Company and Band—from the
German Kapelle). The names Mashua (boat) and Meli (steamer,
from the English “mail”) have a nautical suggestion and Sita (Six) an
arithmetical one—and, to wind up with, we have Mpenda kula—(“He
who loves eating”).
The names used by the interior tribes are free from the noticeable
European touch found in these designations of the carriers, but here,
too, we come across amusing specimens. I notice at the same time
that these names are certainly not the first to grace their bearers. As
is so often the case with primitive peoples, and with the Japanese at
the present day, we find that every individual on being formally
admitted to the duties and responsibilities of adult life assumes a
new name. The natives hereabouts do not know or have forgotten the
original significance of this change, but we are not likely to be wrong
in supposing that the new name also means a new person, who
stands in quite a different relation to his kinsmen and his tribe from
his former one. Officially, every adult Yao, Makua, Makonde or
Matambwe has the right to offer himself as godfather, but I have the
impression that the majority of names one hears are really
nicknames, casually given by acquaintances.[53] It is well known that
the native has a very acute sense of the weak points and absurdities
of others.
Che Likoswe (“Mr. Rat”) will be remembered by his war-songs at
Chingulungulu, and with him may be classed Che Chipembere (“Mr.
Rhinoceros”). The latter is liable to fits of sudden rage, like the
pachyderm, hence his name. The name of the old beer-drinker,
Akundonde, is a reminiscence of his original kinship with the
Wandonde tribe. Che Kamenya is he who is victorious in fight; there
was joy at the birth of Machina; Makwenya gathers everything to
himself, but Che Mduulaga, on the other hand, thinks nothing of
himself,—he is modesty in person. In the same way, Mkotima is a
quiet man, Siliwindi is named after a song-bird so-called; and,
finally, Mkokora is he who carries away dirt in his hands.
These are some Yao men’s names. I will only mention the
following women’s names for this tribe:—Che Malaga means “She is
left alone”—all her relations have died. Che Chelayero, “She who has
a hard time.” Che Tulaye, “She who fares poorly,” and Che Waope,
“She is yours.”
The personal names of the other tribes have on the whole the same
character—Kunanyupu is an old Makua, who, according to his own
statement, has killed many gnus (nyupu) in his youth. Nantiaka is
the Don Juan who flits from one attraction to another. A similar train
of thought has suggested the name of Ntindinganya, the joker, who
contrives to saddle others with the blame of his own tricks.
Linyongonyo is the weakling; Nyopa the ambitious man who strives
to make himself feared by others; Madriga is the sad, melancholy
man; Dambwala the lazy one.
Among the women Alwenenge is “the one who knows her own
worth,”—her lord and master has, it is true, taken another wife, but
he will not remain with her, but return penitently to Alwenenge, as
she very well knows. Much less fortunate is Nantupuli; she wanders
about the world and finds nothing at all, neither a husband nor
anything else. Other unfortunates are Atupimiri and Achinaga—the
former has a husband who is always on his travels and only comes
home from time to time to “measure” (pima) his wife, i.e., to see how
she is behaving. Achinaga’s husband, on the other hand, is ill and
cannot work, so that she has to do everything by herself. There is also
a Pesa mbili among the Makonde women. The name implies that she
formerly stood high in the estimation of men, but now she has grown
old and is only worth two pice. Beauty has its market value even with
the negro.
A field of inquiry, extremely difficult to work, but which will
everywhere well repay cultivation, is that of the customs
accompanying the life of the individual from his cradle to his grave.
The native infant—which is not black, but at first as pink as our
own new-born babies—has come into the world in its mother’s hut.
The father is far from the spot, the women having sent him out of the
way in good time. The baby is carefully washed, and wrapped in a
piece of new bark-cloth. At the same time its ears are anointed with
oil, that it may hear well, and the ligature under the tongue loosened
with a razor, to ensure its learning to speak. Boys are everywhere
welcomed; but with regard to girls, the feeling varies in different
tribes, and, just as is the case among ourselves, in different families.
It is often stated in ethnographical works that primitive peoples
rejoice on purely interested grounds at the birth of girls, on account
of the price they will bring when married. Up to a certain point such
considerations may have weight here, too, but in general people are
glad of daughters if only because they can soon begin to help their
mother in her numerous outdoor and indoor tasks. Their marriage,
moreover, brings an additional faithful and unpaid worker into the
household. For this is the land of exogamy, where the young wife
does not go to her husband’s home, or enter his family, but, on the
contrary, the man leaves his father and mother and either moves
directly into the house of his wife’s parents, or builds his own close
beside it. In any case for some years, until his own family
circumstances necessitate a different arrangement, he devotes all his
powers to keeping up his mother-in-law’s establishment. He sees to
the planting of the crops and their ingathering, he breaks up new
ground, in short he renders every possible service, and anticipates
her every wish. I have often been ashamed when the conversation
turned on this and other features of native life, to remember the
tenor of those venerable jests of which our comic papers never
weary. Of course, a mere passing traveller like myself is no judge of
the more intimate side of family life, but Knudsen, who has lived in
the country long enough to become thoroughly familiar with the
people’s ways of thinking and acting, confirms the impression I had
arrived at, that, not only is the relation between mother and son-in-
law nothing short of ideal, but that the behaviour of young people to
their elders in general deserves to be called exemplary. We who
belong to the highest stage of culture, or, according to the view held
by most of us, the stage of culture, spend half our lives in educational
establishments of various kinds and grades, and the final result is
shown by statistics in the diminishing percentage of illiterates in our
population. But let all who have eyes to see and ears to hear observe
how little ethical sense and how much downright brutality make up
the daily life of these very representatives of culture. I am far from
wishing to say anything against our system of education and our
schools—I am a kind of schoolmaster myself—but it gives food for
serious reflection to see how worm-eaten, in spite of all the care
bestowed on it, is much of the fruit they produce, and how ethically
sound is the life we meet with among these barbarians. And this is
the outcome of a training extending over three or four months and
received from teachers who have passed through no school or
college.
The treatment of twins is different among
the various tribes in this part of the country.
The Wayao welcome them with unmixed joy,
while the Makonde look on their birth as a
terrible event, to be averted if possible by all
sorts of charms. But even here the parents are
not so cruel as to kill them if they do come
into the world; they are allowed to live and
treated in the same way as by the Wayao, i.e.,
their clothing (such as it is) is always alike. If
this were not done, it is believed that one of
them would certainly die.
For the first year the African infant remains
in close contact with its mother. When it is WOMAN CARRYING A
only a few days old, she takes it out for the BABY ON HER BACK.
FROM A DRAWING BY
first time, to be shown to the admiring PESA MBILI
neighbours. Like a little lump of misery it
squats in the large coloured cloth enfolding
the upper part of its mother’s person. It usually hangs on the
mother’s back, but she very often swings it round to one hip. When
the time comes for feeding the baby, it and the bag containing it are
brought round to the front. Nothing so impresses me with the idea of
poverty and squalor as this treatment of infants: no change of
clothing for mother or child—for there is no supply of extra garments
—no drying, no powdering, no napkins, no regular bath after the first
few days, no care of the mouth. On the contrary, every child has sore
places where the skin has been chafed, especially at the joints, and in
folds and depressions of the body; half-healed scabs, where nature is
getting the upper hand in spite of neglect; eyes nearly always bleared
and running in consequence of the perpetual attacks of flies, and,
finally, individual cases, here and there, of thrush-ulcers on such a
scale that fungoid growths actually protrude from mouth and
nostrils. It would be well if the Government and the Missions could
unite to put an end to this frightful state of things, not so much by
medical work, which is naturally limited to certain localities, as by
training the mothers, as extensively as possible, in the simplest rules
of hygiene and cleanliness.
I have been half-an-hour in a native village.
The men and boys were all assembled within
two minutes of my arrival; the women are
gathering more slowly; the little girls,
curiously enough, are altogether absent. Just
as with us, the women have at once gathered
into a closely-packed group. A shy silence
reigns at first, but no sooner have they had
time to get used to the sight of the white man,
THREE MAKUA than there is an outburst of talk in every key,
VEGETARIANS in spite of the hugest of peleles. At least half
these women are carrying babies, but this
term is tolerably comprehensive. Great boys
and girls of two, or even three years old, are sprawling on the slight
backs of delicate-looking mothers, or making violent attacks on the
maternal fount of nourishment. My camera apparently affords the
pretext for this last manœuvre; for, as if at a given signal, the whole
little black band is propelled forward into position at the very
moment when I press the bulb.
The later stages of childhood among the natives are passed in a
way not materially differing from our own youthful recollections. The
little boys band themselves together in troops and carry on their
games in the village and the bush; while the little girls begin at an
early age to help their mothers indoors and out. Wherever I have
been able to carry on my activity as a collector, I have been
particularly assiduous in getting together all toys and games in use in
the country. There is one point deserving of special notice in
connection with children’s games, and this is that almost from the
first day of its existence the child is present wherever anything is
going on. When the mother joins in the dance, the baby on her back
goes through every movement with her, and thus learns dancing, so
to speak, instinctively. By the time it can stand on its own little feet,
it joins in with the same certainty as that with which the partridge
chick just out of the egg goes to pick up its food. Whether native
children have outside these dances anything that can be called
concerted games, I cannot say, but so far I have seen nothing of the
sort,[54] unless we might count the great skill shown in clapping the
hands in unison, in which, with its pleasing rhythm and (one might
almost say) variety of tune, they are as much at home as their elders.
Otherwise every child seems to be dependent on itself, at least as far
as toys are concerned. For boys, bow and arrows are the sine qua non
in the first place. If I had been willing to buy all the toy bows offered
me, I should have had enough to load a small ship. Here in Africa the
weapon is as much of a survival as in most other countries. The fact
of its being confined to children shows that, as in Europe, it is no
longer seriously used in war, but only in play, or at most, in the
chase. We find, as might be expected, that the grown men are no
better archers than the boys, and vice versa. Where firearms have
once been introduced, more primitive weapons are no longer valued.

USE OF THE THROWING STICK


It is not easy to form an ethnographical collection in this country.
It is only in consequence of my very resolute attitude—which is far
more effectual than my bags of copper coin—that the people make up
their minds to bring anything at all, and then it is chiefly rubbish. In
order to obtain the more valuable class of articles, such as the more
important household implements, or the carved masks and other
works of art, I am frequently compelled to resort to a mild display of
force, by making the headman of the village morally responsible for
the production of the specimens. And yet every article is liberally
paid for. How peculiarly difficult it is to obtain toys, of all things,
people at home have no notion. I would suggest the following
explanation for this fact. If a Japanese ethnographer, for instance,
were to visit Germany in the autumn he would find it easy enough to
make a large collection of kites, but tops—to take one of our most
typical children’s toys—he would only be enabled to see and procure
if he definitely inquired for them. It is just the same here; everything
has its season, and toys above all. Having once grasped this truth, I
always made short work of the business, by delivering to the
assembled villagers a lecture on all the playthings of mankind,
winding up with, “If you have so and so—or so and so—be quick and
bring it here.” In many cases neither my own linguistic acquirements
nor the interpreter are sufficient, and gestures have to supply the
lack of words. I was quite startled at my success, one day at
Chingulungulu, when, on having gone through the vigorous
movement of slinging a stone, I saw Salim Matola, the all-
accomplished, return in a short time with two remarkable objects,
which, on his demonstration of the way in which they were used,
proved to be a veritable throwing-stick and a sling—an amentum. I
have rarely had such a feeling of complete success as at this moment.
Who would have thought to find the throwing-stick and the sling in
Eastern Africa, a region hitherto considered so barren as regards
ethnography? The former is an implement intended to serve no other
purpose than the lengthening of the lower arm in order to throw a
spear or a stone; it represents, therefore, in terms of physics, the
lengthened arm of a lever. Its principal region of distribution is
Australia; it also exists in some parts of the Western Pacific, among
the Hyperboreans, and in isolated parts of America. Hitherto it has
been assumed that the African had not arrived at this invention. The
sling in the same way serves to lengthen a lever, but the spear or
stone is in this case not thrown by means of a catch on the stick or
board, but by a string fastened to the root of the forefinger, while the
free end is coiled round the missile. If the warrior throws his arm
forward, the weapon leaves the hand by centrifugal force, uncoils
itself from the string and flies away with great initial velocity.

THROWING WITH THE SLING

Where such antiquities as these occur—I


reflected at the time—surely there are more
discoveries to be made. This expectation was
in fact fulfilled, though I had first to fight my
way through a superfluity of another species
of toy. One day, in the course of the lecture
already referred to, I happened to make the
gesture of whipping something over the
ground, and it was at once correctly
understood, for from that time forward the
SPINNING A TOP young people simply overwhelmed me with
tops. No less than four kinds are in use here.
One exactly corresponds to our European peg-top,[55] and is, like it,
driven with a whip, a second has a round or square piece of gourd
fixed on a short, stout wooden peg as axis of rotation;[56] a third is
similar to the last, but has a second disc under the first (which is
about the size of a five-shilling piece), in order to place the centre of
gravity higher up. Finally, we have a very complicated mechanism
whose action resembles that of our humming-top. The second and
third require no whip, but are spun with the thumb and middle
finger. The fourth, on the other hand, needs a “frame” to spin it. This
is represented by a piece of maize-cob perforated lengthwise,
through which the string wound round the top is quickly pulled back.
Like many other things, the art of spinning tops is not made easy for
native boys, the soft, sandy ground being ill-suited to this game; yet
the little fellows show great skill in it.

IKOMA DANCE AT THE GIRLS’ UNYAGO, ACHIKOMU


XYLOPHONE (MGOROMONDO)

With one exception, children have no musical instruments peculiar


to themselves. Whether they fiddle on the sese, the one-stringed
violin, or maltreat the ulimba, that instrument on which all Africans
strum—the box with wooden or iron keys fixed to its surface, and
struck with the finger-tips—or strike the mgoromondo, that
antediluvian xylophone in which the keys rest on a layer of straw, or
play on the lugombo, the musical bow with calabash resonator,
which is so widely distributed over East and South Africa—in every
case the instruments are only clumsy imitations of those used by
grown-up people. The only one whose use is confined to the young is
the natura—a friction-drum, made from a bottle-gourd or the fruit of
the baobab, cut across and covered in, like a drum, with the skin of
some small animal. A blade of grass passes through the middle of the
diaphragm, and thence down through the bottom of the shell. By
rubbing a wetted thumb and forefinger down the stalk, as the little
wretches are perpetually doing, a noise is produced so excruciating
that even my carriers—who are not precisely sufferers from nerves—
take to flight when they hear it. But young people are not only
capable of preserving ancient survivals in culture through thousands
of years, but also have the advantage of a greater receptivity for
novelties. I have in my collection two charming specimens of an
African telephone, consisting of two miniature drums, beautifully
carved and covered with the delicate skin of some small animal,
perforated in the middle to allow the passage of a thin string, which
is kept from slipping through by a knot on the inside of the skin. I
never thought, at first, of taking this thing seriously, but one day,
having a spare quarter of an hour, I put one of the drums into
Knudsen’s hand, and told him to walk away till the string—about a
hundred yards in length—was stretched tight. I held the membrane
to my ear, and heard quite clearly, “Good-day, Professor. Can you
hear me?” So the thing really acts, and all that remains for us to do is
to develop it and boldly link ourselves up with the coast and that
centre of civilization, Lindi![57] There can be no question of
independent invention in this case; the telephone is undoubtedly
borrowed—but the fact of the borrowing, and the way it is applied by
children are not without interest.
Such an important epoch in
native life as that represented
by the unyago, with all its joys
and woes, its games and
dances, cannot be without
influence on the habits of the
young people, even before it
arrives. Thus I have some ipivi
flutes obtained from little
fellows far too young to be
admitted to the mysteries.
Anyone who wishes to excel in
an art must begin his training
early, and the flute players of
the ndagala practise their
instrument for years
beforehand. Moreover, boys,
who had evidently not yet
passed through the unyago,
have more than once brought
me specimens of the kakale, PLAYING THE
the long sticks, painted black NATURA
and white in alternate rings
with a little trophy at the top,
NATURA
(FRICTION-
consisting of the shell of some fruit with a plume of
DRUM) feathers stuck in it. These two insignia of maturity,
therefore, are also found in the capacity of toys.
There is nothing surprising in this so far as the boys
are concerned, for the native has no secrets from them. At the
ceremonies I witnessed at Achikomu, as well as at Niuchi and
Mangupa, there was always a whole troop of little fellows, covered
with dirt and ashes, running about. Strangely enough, there were
never any half-grown girls to be seen on these occasions; everything
relating to the mysteries seems to be carefully kept secret from them.
It was only during my long residence at Newala, with its possibilities
of free intercourse between me and the different tribes, as well as
among natives of different ages, that I could see and photograph any
of these young things. They seem to be brought up much more within
the walls of the hut and its compound than we are accustomed to
suppose; and even in the hundreds of visits I have paid to native
homes, I have seldom been able to see the young daughters of the
house face to face. As a rule, I only caught sight of a slender little
figure retreating swiftly through the back door of the hut.
Under these circumstances, of
course, I cannot say how the little
native girl actually grows up, and
whether she enjoys anything even
faintly resembling the happy
childhood of our own loved ones
—but nothing leads us to suppose
that she does; though there is no
question that the native shares in
the universal instinct which
inspires all parents with affection
for their offspring; he feeds his
UNASIKIA?: “DO YOU HEAR?” children and protects them when
they need protection; he rejoices
when they thrive and mourns
over their illness and death. I can still see Matola, as he came to me
one day—his usual expression of gentle melancholy heightened to
one of deep grief and anxiety—carrying a little girl of some five or six
years. She was not even his own child, but a relative, for whom he
entreated my help. To my sincere regret, it was impossible for me to
do anything—the poor little thing was suffering from a malignant
gangrene, which had eaten away the whole front of one thigh, so that
the tendons were laid bare and the bones were beginning to bend. I
spoke very seriously to Matola, asking whether he were as much of a
mshenzi as his people, who were
perishing through their own
stupidity and apathy. He, the
headman, and a clever man at
that, knew very well, so I told
him, that there were German
doctors at Lindi, who could cure
even such cases as this, if the
patients were brought to them.
He ought therefore, to send the
child down at once, unless he
wished her to die, as all her elder NDIO: “YES”
brothers and sisters had done.
Matola gazed at me for some time, evidently
wavering between hope and doubt; but in the
end he followed my advice; and I have since
heard that the child is well on the way to
recovery. But it is astonishing and perplexing
that such an enlightened man as the chief of
Chingulungulu should have allowed the
disease to go on so long before taking any
serious steps to obtain assistance. What then
could be expected of a man from the bush,
who consulted me immediately after my
arrival, asking me for medicine for his sick
child?
“What is the matter with your child?”
“A wound on her foot.”
“But, my good man,” I said, “I can’t give you
medicine to take home,—you would not know
how to put it on. You must bring your child
here. Where do you live?”
“Mbali—a long way off—Bwana,” he
answers, lengthening the vowel to signify
inexpressible distance.
NATIVE TELEPHONE
“How far?”
“Well—about two hours.”
“Oh! you call that far, do you? you mshenzi! if you were going to a
beer-drink, twenty hours would be karibu sana. Off with you now,
and come back at eight to-morrow morning.” But neither at eight nor
at any later hour was there any sign of the noble father from the
Makonde bush. It was not till the fifteenth day after the preliminary
consultation that he appeared, bringing with him a little girl of five or
six. I did not at first remember him, but at once recalled his previous
visit when the child, overcoming her natural shyness, held out her
foot. Nothing was to be seen but a horrible mass of dirt and sand
cemented together with blood. I started at once on the cleansing
process, with the help of Stamburi, my trusty hospital orderly; and
when at last the foot was laid bare, we found that the whole ball was
eaten away to the bone—whether owing to jiggers, or through the
cumulative effect of various other circumstances, my medical
knowledge is insufficient to decide. When at last I glanced at the
father, I saw him staring like one hypnotized at a leg of antelope
intended for the next day’s dinner, which Knudsen had hung up just
over my table. Having recalled him to reality, I bade Moritz give him
the softest part from the skin of a recently killed wild pig, and told
him to make a shoe, or at least a sandal such as are certainly not
unknown in this country, as he must see for himself that the child
could not walk through the dirty sand with her freshly-bandaged
foot. He had his knife with him—let him get to work without delay!
We two practitioners devoted ourselves once more to the treatment
of the wound, which was in truth a terrible one; and in a little while
the bandage was put on as correctly as we knew how. A second look
at the father showed that he was still staring at the raw joint, as
intently as if he had really eaten his way into it. It is a good thing,
after all, in such cases, to have the kiboko within reach. In another
quarter of an hour the well-wrapped foot was protected by a very
serviceable pigskin slipper. But that is the last I ever saw or heard of
the gentleman, and he never so much as thanked me either for the
treatment or for the thrashing.
MAKONDE CHILDREN

Boys and girls, as a rule, reach the age of eight or nine, perhaps
ten, before any event of importance interrupts the even tenor of their
lives. Then the assembly of the men, which when the harvest is over,
meets daily in the baraza, decides where the unyago is to be
celebrated in the current year. Since all the adjacent districts have
now taken their turn in bearing the expense of the ceremony, it is a
point of honour that our village should invite them this time. The
resolution is soon carried into effect; the moon is already on the
wane, and the celebration must take place before the new moon. The
unyago presents exactly the same features in all the tribes of this
region. The men erect a circle—larger or smaller as circumstances
may require—of simple grass huts in an open space near the village.
In this space the opening and closing ceremonies are performed; the
huts are intended for the candidates to live and sleep in. Such an
arena, with all its appurtenances in excellent preservation, was the
circle of something over fifty yards’ diameter which I was enabled to
photograph when visiting the echiputu at Akuchikomu. The charred
remains of a similar lisakasa, as the system of huts is called in Yao,
were to be seen near the road on this side of Akundonde’s—the relics
of a former festival.

MASEWE DANCE OF THE MAKUAS IN THE BOMA AT


NEWALA

It is inherent in the nature of the whole institution that both boys


and girls should be passive throughout. They sit silent, inactive and
motionless in their huts while, on the first night of the festival, the
grown-ups feast and drink and enjoy themselves in the wild masewe
dance. Next day the boys, each one in charge of his instructor, are
conducted to the bush by the chief director. There they sleep one
night without any shelter whatever. For a short time, on the
following day, they may do as they please, but during the remainder
they have to set to work with their anamungwi (teachers) and build
the ndagala. As soon as this airy construction is finished, one after
another of the boys is laid on a very primitive couch of millet-straw,
and the jua michila performs the operation. For weeks the little
patients lie there in a row, unable to do anything to accelerate the
slow process of healing. Not till this is complete and the subsequent
moral and other instruction has begun do the wari, as the boys are
now called, acquire the right to take part in public life. In the high
spirits engendered by the pride of their new position, they indulge in
many a mad freak. Woe to the unhappy woman or girl who, ignorant
of the situation of the ndagala, strays into this region of the bush.
Like a troop of mischievous imps, the boys rush on her, tease her,
perhaps even tie her up and ill-use her. According to tribal custom,
they are quite within their rights in so doing, for their abode in the
bush is supposed to be utterly unknown to women. When he goes out
into the pori the boy is dead to his mother,—when he returns, he will
be a different person with a new name, and nothing to connect him
with his former relationship.
I have already tried to describe the course taken by the instruction
imparted in the ndagala. Old Akundonde and his councillor, in the
candour induced by their libations, were certainly trustworthy
informants in this respect. It is an irreparable misfortune that the
liquor supply coming to an end when it did (in such a surprisingly
short time) deprived me of the conclusion of the address to the wari,
but the fragment already given is quite sufficient to indicate the
character of the teaching.
The lupanda reaches its culminating point only with the closing
ceremony. The preparations on both sides are extensive: in the bush
the wari are being restored by their mentors by means of head-
shaving, baths, anointing with oil, and a supply of new cloth, to a
condition worthy of human beings. In the village, meanwhile, the
mothers, long before the time fixed, have been brewing large
quantities of beer and preparing still larger piles of food for the
festivity. When the great day at last arrives, the boys come back in
procession, in their clean new garments, with their faces, necks, and
freshly-shorn scalps all shining with oil, and carrying in their right
hands the kakale, the sticks headed with rattles which have already
been described. Men and women line the road in joyful expectation.
Ever louder and more piercingly, the “lu-lu-lu” of the women
vibrates across the arena, and yonder the drums strike up with their
inspiriting rhythm, while the hoarse throats of the men utter the first
notes of a ng’oma song. In short, everything is going on in the most
satisfactory and genuinely African way.
KAKALE PROCESSION ON THE LAST DAY OF THE UNYAGO

The Africans, being human, like ourselves, it is only to be expected


that all their works and ways are subject to as many changes and
inconsistencies as our own. I have devoted a disproportionate part of
the time (over a month) spent at Newala to the task of fixing the
typical course of all these ceremonies. This has been a most severe
labour, for if, in my wish to obtain unimpeachably accurate results, I
arranged to let my informants of each tribe come separately, I might
be sure that the two or three old men who made their appearance
would say little or nothing. The native intellect seems not to become
active till awakened and stimulated by sharp retort and rejoinder in a
numerous circle of men. I have thus been compelled to go back again
and again to my original method of assembling the whole senate of
“those who know,” some fifteen aged Yaos, Makua and Makonde in a
heterogeneous crowd round my feet. This was so far successful as to
produce a lively discussion every time, but it becomes very difficult
to distinguish between the elements belonging to different tribes. Yet
I venture to think that, with a great deal of luck, and some little skill,
I have succeeded so far as to get a general outline of these matters. I
feel quite easy in my mind at leaving to my successors the task of
filling up the gaps and correcting the inaccuracies which no doubt
exist.
Further, it must be remembered that my notes on the initiation
ceremonies of these three tribes would, if given in full, take up far too
much space to allow of their reproduction here. Two other points
must be borne in mind. What I saw with my own eyes of the unyago,
I have here related in full, with that local colouring of which actual
experience alone enables a writer to render the effect. But those
scenes at Achikomu, Niuchi and Mangupa are only tiny fractions of
the very extensive fasti represented by the girls’ unyago in reality;
while, as to the remainder, I can only repeat what I have heard from
my informants. Quotations, however, always produce an impression
of dryness and tedium, which is what I would seek to avoid at any
price. I therefore think it better to refer those interested in the details
of such things to the larger work in which it will be my duty,
according to agreement, to report to the Colonial Office on my doings
in Africa and their scientific results.
The last point belongs to another department. The negro is not in
the least sophisticated as regards the relation between the sexes.
Everything pertaining to it seems to him something quite natural,
about which his people are accustomed to speak quite freely among
themselves,—only in extreme cases showing a certain reticence
before members of the alien white race. Now the part played by sex
in the life of the African is very great, incomparably greater than with
us. It would be too much to say that all his thoughts and desires
revolve round this point, but a very large proportion thereof is
undoubtedly concerned with it. This is shown in the clearest way, not
only in the unyago itself, but in the representation which I
subsequently witnessed. In the present state of opinion resulting
from the popular system of education, such delicate matters can only
be treated in the most strictly scientific publications, being debarred
from reproduction in a book of any other character. This is necessary
—I must once more emphasize the fact,—not on account of the
subject itself, but out of consideration for the misguided feelings of
the public. It may be regrettable, but it is true.
Of all the tribes in the South of German East Africa, the Yaos seem
to be, not only the most progressive, but the most prosaic and
unimaginative, and in fact their initiation ceremonies are very
simple, compared with those of the Makonde and Makua. Those of
the latter have to a certain extent a dramatic character. The Makua
choose a branch of a particular shape, and forked several times,
which they plant in the midst of the open space where the festival is

You might also like