100% found this document useful (5 votes)
112 views

Instant Download Java TM Programming From Problem Analysis to Program Design 5th Edition D. S. Malik PDF All Chapters

From

Uploaded by

tazazofrodi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (5 votes)
112 views

Instant Download Java TM Programming From Problem Analysis to Program Design 5th Edition D. S. Malik PDF All Chapters

From

Uploaded by

tazazofrodi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

Download the full version of the ebook at

https://ebookultra.com

Java TM Programming From Problem Analysis to


Program Design 5th Edition D. S. Malik

https://ebookultra.com/download/java-tm-
programming-from-problem-analysis-to-program-
design-5th-edition-d-s-malik/

Explore and download more ebook at https://ebookultra.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

C programming from problem analysis to program design 4th


ed Edition D S Malik

https://ebookultra.com/download/c-programming-from-problem-analysis-
to-program-design-4th-ed-edition-d-s-malik/

ebookultra.com

The Java TM Programming Language 4th Edition Ken Arnold

https://ebookultra.com/download/the-java-tm-programming-language-4th-
edition-ken-arnold/

ebookultra.com

Java Programs to Accompany Programming Logic and Design


7th Edition Jo Ann Smith

https://ebookultra.com/download/java-programs-to-accompany-
programming-logic-and-design-7th-edition-jo-ann-smith/

ebookultra.com

Beginning Programming with Java for Dummies 5th Edition


Barry Burd

https://ebookultra.com/download/beginning-programming-with-java-for-
dummies-5th-edition-barry-burd/

ebookultra.com
Problem Solving and Program Design in C Third Edition Jeri
R. Hanly

https://ebookultra.com/download/problem-solving-and-program-design-in-
c-third-edition-jeri-r-hanly/

ebookultra.com

Java How to Program Early Objects 10th ed Edition Deitel

https://ebookultra.com/download/java-how-to-program-early-
objects-10th-ed-edition-deitel/

ebookultra.com

Java concepts 5th Edition Cay S. Horstmann

https://ebookultra.com/download/java-concepts-5th-edition-cay-s-
horstmann/

ebookultra.com

Java the UML way integrating object oriented design and


programming Else Lervik

https://ebookultra.com/download/java-the-uml-way-integrating-object-
oriented-design-and-programming-else-lervik/

ebookultra.com

Java TM Application Development on Linux R 1st Edition


Carl Albing

https://ebookultra.com/download/java-tm-application-development-on-
linux-r-1st-edition-carl-albing/

ebookultra.com
Java TM Programming From Problem Analysis to
Program Design 5th Edition D. S. Malik Digital Instant
Download
Author(s): D. S. Malik
ISBN(s): 9781111530532, 111153053X
Edition: 5
File Details: PDF, 3.43 MB
Year: 2011
Language: english
Licensed to: iChapters User
Licensed to: iChapters User

JAVA PROGRAMMING
FROM PROBLEM ANALYSIS TO PROGRAM DESIGN

FIFTH EDITION

D.S. MALIK

Australia ! Brazil ! Japan ! Korea ! Mexico ! Singapore ! Spain ! United Kingdom ! United States

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

This is an electronic version of the print textbook. Due to electronic rights


restrictions, some third party content may be suppressed. Editorial
review has deemed that any suppressed content does not materially
affect the overall learning experience. The publisher reserves the
right to remove content from this title at any time if subsequent
rights restrictions require it. For valuable information on pricing, previous
editions, changes to current editions, and alternate formats, please visit
www.cengage.com/highered to search by ISBN#, author, title, or keyword
for materials in your areas of interest.

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

!"#$%&$$&'(&)*+),,-*.*/00111** "2323$11133456475189
Licensed to: iChapters User

Java Programming: From Problem Analysis ª 2012 Course Technology, Cengage Learning
to Program Design, Fifth Edition
ALL RIGHTS RESERVED. No part of this work covered by the
D.S. Malik copyright herein may be reproduced, transmitted, stored or used
Executive Editor: Marie Lee in any form or by any means graphic, electronic, or mechanical,
including but not limited to photocopying, recording, scanning,
Acquisitions Editor: Brandi Shailer digitizing, taping, Web distribution, information networks, or
Senior Product Manager: Alyssa Pratt information storage and retrieval systems, except as permitted
Editorial Assistant: Jacqueline Lacaire under Section 107 or 108 of the 1976 United States Copyright Act,
without the prior written permission of the publisher.
Content Project Manager: Lisa Weidenfeld
Associate Marketing Manager: Shanna For product information and technology assistance, contact us at
Shelton Cengage Learning Customer & Sales Support, 1-800-354-9706
Art Director: Faith Brosnan For permission to use material from this text or product, submit
all requests online at www.cengage.com/permissions
Proofreader: Andrea Schein
Further permissions questions can be emailed to
Indexer: Alexandra Nickerson permissionrequest@cengage.com
Print Buyer: Julio Esperas
Cover Designer: Roycroft Design/
www.roycroftdesign.com Library of Congress Control Number: 2010940363
Cover Photo: ª photolibrary/Richard ISBN-13: 978-1-111-53053-2
Cummins ISBN-10: 1-111-53053-x
Compositor: Integra Course Technology
20 Channel Center Street
Boston, MA 02210
Some of the product names and company
USA
names used in this book have been used for
identification purposes only and may be
trademarks or registered trademarks of their Cengage Learning is a leading provider of customized
respective manufacturers and sellers. learning solutions with office locations around the globe,
including Singapore, the United Kingdom, Australia, Mexico,
Any fictional data related to persons or
Brazil and Japan. Locate your local office at:
companies or URLs used throughout this
www.cengage.com/global
book is intended for instructional purposes
only. At the time this book was printed, any Cengage Learning products are represented in Canada by
such data was fictional and not belonging to Nelson Education, Ltd.
any real persons or companies.
To learn more about Course Technology, visit
Course Technology, a part of Cengage www.cengage.com/coursetechnology
Learning, reserves the right to revise this
Purchase any of our products at your local college store
publication and make changes from time to
or at our preferred online store www.cengagebrain.com
time in its content without notice.
The programs in this book are for
instructional purposes only. They have been
tested with care, but are not guaranteed for
any particular intent beyond educational
purposes. The author and the publisher do
not offer any warranties or representations,
nor do they accept any liabilities with respect
to the programs.

Printed in the United States of America


1 2 3 4 5 6 7 16 15 14 13 12 11 10

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

TO

My Daughter

Shelly Malik

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

B RIEF C ONTENTS

PREFACE xix
1. An Overview of Computers and Programming Languages 1

2. Basic Elements of Java 25

3. Introduction to Objects and Input/Output 113

4. Control Structures I: Selection 177

5. Control Structures II: Repetition 249

6. Graphical User Interface (GUI) and Object-Oriented Design (OOD) 327

7. User-Defined Methods 383

8. User-Defined Classes and ADTs 465

9. Arrays 551

10. Inheritance and Polymorphism 639

11. Handling Exceptions and Events 723

12. Advanced GUIs and Graphics 783

13. Recursion 873

14. Searching and Sorting 907

APPENDIX A Java Reserved Words 939

APPENDIX B Operator Precedence 941

APPENDIX C Character Sets 945

APPENDIX D Additional Java Topics 949

APPENDIX E Answers to Odd-Numbered Exercises 997

INDEX 1023
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

TABLE OF C ONTENTS

Preface xix

AN OVERVIEW OF COMPUTERS AND


1 PROGRAMMING LANGUAGES 1
Introduction 2

An Overview of the History of Computers 2

Elements of a Computer System 4


Hardware 4
Software 6

Language of a Computer 6

Evolution of Programming Languages 8

Processing a Java Program 10

Internet, World Wide Web, Browser, and Java 13

Programming with the Problem


Analysis–Coding–Execution Cycle 13

Programming Methodologies 19
Structured Programming 19
Object-Oriented Programming 19

Quick Review 21

Exercises 23

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Table of Contents | vii

BASIC ELEMENTS OF JAVA 25


2 A Java Program 26

Basics of a Java Program 28


Comments 29
Special Symbols 30
Reserved Words (Keywords) 30
Identifiers 31

Data Types 32
Primitive Data Types 32

Arithmetic Operators and Operator Precedence 36


Order of Precedence 39

Expressions 40
Mixed Expressions 41

Type Conversion (Casting) 43

class String 45
Strings and the Operator + 46

Input 48
Allocating Memory with Named Constants and Variables 48
Putting Data into Variables 51
Declaring and Initializing Variables 55
Input (Read) Statement 56
Reading a Single Character 61

Increment and Decrement Operators 64

Output 66

Packages, Classes, Methods, and the import Statement 71

Creating a Java Application Program 72

Debugging: Understanding and Fixing Syntax Errors 77

Programming Style and Form 80


Syntax 80

Avoiding Bugs: Consistent, Proper Formatting and


Code Walk-Through 84

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

viii | Java Programming: From Problem Analysis to Program Design, Fifth Edition

More on Assignment Statements (Optional) 85

Quick Review 94

Exercises 97

Programming Exercises 106

INTRODUCTION TO OBJECTS AND INPUT/OUTPUT 113


3 Objects and Reference Variables 114

Using Predefined Classes and Methods in a Program 118


Dot Between Class (Object) Name and Class Member: A
Precaution 120

class String 121

Input/Output 129
Formatting Output with printf 129
Using Dialog Boxes for Input/Output 139
Formatting the Output Using the String Method format 146

File Input/Output 149


Storing (Writing) Output to a File 152

Debugging: Understanding Logic Errors and


Debugging with print or println Statements 163

Quick Review 165

Exercises 167

Programming Exercises 171

CONTROL STRUCTURES I: SELECTION 177


4 Control Structures 178

Relational Operators 180

Relational Operators and Primitive Data Types 181

Logical (Boolean) Operators and Logical Expressions 183

Order of Precedence 185


boolean Data Type and Logical (Boolean) Expressions 189

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Table of Contents | ix

Selection: if and if...else 190


One-Way Selection 190
Two-Way Selection 193
Compound (Block of) Statements 197
Multiple Selections: Nested if 198
Comparing if...else Statements with a Series
of if Statements 200
Short-Circuit Evaluation 201
Comparing Floating-Point Numbers for Equality:
A Precaution 202
Conditional Operator (? :) (Optional) 204

Avoiding Bugs by Avoiding Partially Understood


Concepts and Techniques 204

Program Style and Form (Revisited): Indentation 208

switch Structures 208

Avoiding Bugs by Avoiding Partially Understood


Concepts and Techniques (Revisited) 215

Comparing Strings 223


Strings, the Assignment Operator, and the Operator new 229

Quick Review 230

Exercises 232

Programming Exercises 241

CONTROL STRUCTURES II: REPETITION 249


5 Why Is Repetition Needed? 250

while Looping (Repetition) Structure 251


Designing while Loops 254
Counter-Controlled while Loops 255
Sentinel-Controlled while Loops 257
Flag-Controlled while Loops 263
EOF-Controlled while Loops 266
More on Expressions in while Statements 271

for Looping (Repetition) Structure 278

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

x | Java Programming: From Problem Analysis to Program Design, Fifth Edition

do...while Looping (Repetition) Structure 288


Choosing the Right Looping Structure 293

break and continue Statements 293

Avoiding Bugs by Avoiding Patches 295

Debugging Loops 298

Nested Control Structures 299

Quick Review 304

Exercises 306

Programming Exercises 319

GRAPHICAL USER INTERFACE (GUI) AND


6 OBJECT-ORIENTED DESIGN (OOD) 327
Graphical User Interface (GUI) Components 328
Creating a Window 332
JFrame 332
Getting Access to the Content Pane 338
JLabel 339
JTextField 343
JButton 347

Object-Oriented Design 363


A Simplified OOD Methodology 364

Implementing Classes and Operations 370


Primitive Data Types and the Wrapper Classes 370

Quick Review 377

Exercises 378

Programming Exercises 381

USER-DEFINED METHODS 383


7 Predefined Methods 384
Using Predefined Methods in a Program 388

User-Defined Methods 391


Value-Returning Methods 391
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Table of Contents | xi

return Statement 395


Final Program 398

Flow of Execution 404

Void Methods 407

Primitive Data Type Variables as Parameters 411

Reference Variables as Parameters 414


Parameters and Memory Allocation 414
Reference Variables of the String Type as Parameters:
A Precaution 414
The class StringBuffer 418

Primitive Type Wrapper Classes as Parameters 421

Scope of an Identifier Within a Class 422

Method Overloading: An Introduction 427

Debugging: Using Drivers and Stubs 440

Avoiding Bugs: One-Piece-at-a-Time Coding 442

Quick Review 442

Exercises 445

Programming Exercises 456

USER-DEFINED CLASSES AND ADTS 465


8 Classes and Objects 466
Constructors 471
Unified Modeling Language Class Diagrams 472
Variable Declaration and Object Instantiation 473
Accessing Class Members 475
Built-in Operations on Classes 476
Assignment Operator and Classes: A Precaution 476
Class Scope 478

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

xii | Java Programming: From Problem Analysis to Program Design, Fifth Edition

Methods and Classes 479


Definitions of the Constructors and Methods
of the class Clock 479

Classes and the Method toString 494

Copy Constructor 500

Static Members of a Class 501


static Variables (Data Members) of a Class 503

Finalizers 507

Accessor and Mutator Methods 507

Debugging—Designing and Documenting a Class 510

Reference this (Optional) 512


Cascaded Method Calls (Optional) 514

Inner Classes 517

Abstract Data Types 517

Quick Review 537

Exercises 538

Programming Exercises 547

ARRAYS 551
9 Why Do We Need Arrays? 552

Arrays 553
Alternate Ways to Declare an Array 555
Accessing Array Elements 555
Specifying Array Size during Program Execution 557
Array Initialization during Declaration 558
Arrays and the Instance Variable length 558
Processing One-Dimensional Arrays 559
Array Index Out of Bounds Exception 564
Declaring Arrays as Formal Parameters to Methods 564
Assignment Operator, Relational Operators, and Arrays:
A Precaution 565
Arrays as Parameters to Methods 567

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Table of Contents | xiii

Searching an Array for a Specific Item 572

Arrays of Objects 574


Arrays of string Objects 574
Arrays of Objects of Other Classes 576

Arrays and Variable Length Parameter List (Optional) 581

Two-Dimensional Arrays 589


Accessing Array Elements 591
Two-Dimensional Array Initialization during Declaration 594
Processing Two-Dimensional Arrays 595
Passing Two-Dimensional Arrays as Parameters to Methods 599

Multidimensional Arrays 603

class Vector (Optional) 616


Primitive Data Types and the class Vector 620
Vector Objects and the foreach Loop 620

Quick Review 621

Exercises 623

Programming Exercises 634

INHERITANCE AND POLYMORPHISM 639


10 Inheritance 640
Using Methods of the Superclass in a Subclass 642
Constructors of the Superclass and Subclass 648
Protected Members of a Class 657
Protected Access vs Package Access 660

class Object 661

Java Stream Classes 663

Polymorphism 664
Operator instanceof 670

Abstract Methods and Classes 674

Interfaces 681

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

xiv | Java Programming: From Problem Analysis to Program Design, Fifth Edition

Polymorphism Via Interfaces 682

Composition (Aggregation) 684

Quick Review 709

Exercises 712

Programming Exercises 719

HANDLING EXCEPTIONS AND EVENTS 723


11 Handling Exceptions Within a Program 724
Java’s Mechanism of Exception Handling 727
try/catch/finally Block 728

Java Exception Hierarchy 733

Java’s Exception Classes 736

Checked and Unchecked Exceptions 741

More Examples of Exception Handling 743


class Exception and the Operator instanceof 746

Rethrowing and Throwing an Exception 749

Method printStackTrace 753

Exception-Handling Techniques 755


Terminate the Program 755
Fix the Error and Continue 756
Log the Error and Continue 757

Creating Your Own Exception Classes 758

Event Handling 760

Quick Review 775

Exercises 777

Programming Exercises 781

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Table of Contents | xv

ADVANCED GUIS AND GRAPHICS 783


12 Applets 787

class Font 791

class Color 794

class Graphics 800


Converting an Application Program to an Applet 808

Additional GUI Components 811


JTextArea 811
JCheckBox 816
JRadioButton 823
JComboBox 828
JList 833

Layout Managers 839


FlowLayout 840
BorderLayout 843

Menus 844

Key and Mouse Events 847


Key Events 848
Mouse Events 850

Quick Review 865

Exercises 866

Programming Exercises 868

RECURSION 873
13
Recursive Definitions 874
Direct and Indirect Recursion 876
Infinite Recursion 877
Designing Recursive Methods 877

Problem Solving Using Recursion 878


Tower of Hanoi: Analysis 887

Recursion or Iteration? 888

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

xvi | Java Programming: From Problem Analysis to Program Design, Fifth Edition

Quick Review 896

Exercises 897

Programming Exercises 901

SEARCHING AND SORTING 907


14
List Processing 908
Searching 908
Selection Sort 909
Insertion Sort 913
Binary Search 917

Quick Review 934

Exercises 934

Programming Exercises 936

APPENDIX A: JAVA RESERVED WORDS 939

APPENDIX B: OPERATOR PRECEDENCE 941

APPENDIX C: CHARACTER SETS 945


ASCII (American Standard Code for Information
Interchange), the First 128 Characters of the
Unicode Character Set 945
EBCDIC (Extended Binary Coded Decimal
Interchange Code) 946

APPENDIX D: ADDITIONAL JAVA TOPICS 949


Binary (Base 2) Representation of a Nonnegative Integer 949
Converting a Base 10 Number to a Binary Number (Base 2) 949
Converting a Binary Number (Base 2) to Base 10 951
Converting a Binary Number (Base 2) to Octol (Base 8)
and Hexdecimal (Base 16) 952

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Table of Contents | xvii

Executing Java Programs Using the Command-Line


Statements 954
Setting the Path in Windows 7.0 (Professional) 954
Executing Java Programs 959
Java Style Documentation 964

Creating Your Own Packages 966


Multiple File Programs 969

Formatting the Output of Decimal Numbers Using


the class DecimalFormat 969

Packages and User-Defined Classes 972


Primitive Type Classes 972
Class: IntClass 972
Class: LongClass 976
Class: CharClass 977
Class: FloatClass 977
Class: DoubleClass 978
Class: BooleanClass 979
Using Primitive Type Classes in a Program 980

Enumeration Types 981

APPENDIX E: ANSWERS TO ODD-NUMBERED


EXERCISES 997
Chapter 1 997

Chapter 2 998

Chapter 3 1001

Chapter 4 1002

Chapter 5 1004

Chapter 6 1007

Chapter 7 1008

Chapter 8 1010

Chapter 9 1014

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

xviii | Java Programming: From Problem Analysis to Program Design, Fifth Edition

Chapter 10 1016

Chapter 11 1018

Chapter 12 1019

Chapter 13 1020

Chapter 14 1020

INDEX 1023

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

P REFACE TO THE F IFTH


E DITION

Welcome to Java Programming: From Problem Analysis to Program Design, Fifth Edition.
Designed for a first Computer Science (CS1) Java course, this text will provide a breath of
fresh air to you and your students. The CS1 course serves as the cornerstone of the Computer
Science curriculum. My primary goal is to motivate and excite all programming students,
regardless of their level. Motivation breeds excitement for learning. Motivation and excite-
ment are critical factors that lead to the success of the programming student. This text is the
culmination and development of my classroom notes throughout more than fifty semesters of
teaching successful programming.
Warning: This text can be expected to create a serious reduction in the demand for program-
ming help during your office hours. Other side effects include significantly diminished student
dependency on others while learning to program.

The primary focus in writing this text is on student learning. Therefore, in addition to clear
explanations, we address the key issues that otherwise impede student learning. For example, a
common question that arises naturally during an early programming assignment is: ‘‘How
many variables and what kinds are needed in this program?’’ We illustrate this important and
crucial step by helping students learn why variables are needed and how data in a variable is
manipulated. Next students learn that the analysis of the problem will spill the number and
types of the variables. Once students grasp this key concept, control structures (selection and
loops) become easier to learn. The second major impediment in learning programming is
parameter passing. We pay special attention to this topic. First students learn how to use
predefined methods and how actual and formal parameters relate. Next students learn about
user-defined methods. They see visual diagrams that help them learn how methods are called
and how formal parameters affect actual parameters. Once students have a clear understanding
of these two key concepts, they readily assimilate advanced topics.
The topics are introduced at a pace that is conducive to learning. The writing style is friendly,
engaging, and straightforward. It parallels the learning style of the contemporary CS1 student.
Before introducing a key concept, the student learns why the concept is needed, and then sees
examples illustrating the concept. Special attention is paid to topics that are essential in
mastering the Java programming language and in acquiring a foundation for further study
of computer science.

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

xx | Java Programming: From Problem Analysis to Program Design, Fifth Edition

Other important topics include debugging techniques and techniques for avoiding programming
bugs. When a beginner compiles his/her first program and sees that the number of errors exceeds the
length of this first program, he/she becomes frustrated by the plethora of errors, only some of which
can be interpreted. To ease this frustration and help students learn to produce correct programs,
debugging and bug avoidance techniques are presented systematically throughout the text.

Changes in the Fifth Edition


The main changes are:
• In the fifth edition, new debugging sections have been added and some of the old
ones have been rewritten. These sections are indicated with a debugging icon.
• The fifth edition contains more than 125 new exercises, 27 new programming
exercises, and numerous new examples spread throughout the book.
• In Chapters 6 and 12 the GUI figures have been captured and replaced in Windows 7
Professional environment.
• Appendix D contains screen images illustrating how to compile and execute a Java
program using the command-line statements as well as how to set the path in
Windows 7 Professional environment.
These changes were implemented based on comments from the text reviewers of the fifth
edition. The source code and the programming exercises are developed and tested using Java
6.0 and the version of Java 7.0 available at the time this book was being typeset.

Approach
Once conceived as a Web programming language, Java slowly but surely found its way into
classrooms where it now serves as a first programming language in computer science curricula
(CS1). Java is a combination of traditional style programming—programming with a non-
graphical user interface—and modern style programming with a graphical user interface
(GUI). This book introduces you to both styles of programming. After giving a brief
description of each chapter, we discuss how to read this book.
Chapter 1 briefly reviews the history of computers and programming languages. The reader
can quickly skim and become familiar with some of the hardware and software components of
the computer. This chapter also gives an example of a Java program and describes how a Java
program is processed. The two basic problem-solving techniques, structured programming
and object-oriented design, are also presented.
After completing Chapter 2, students become familiar with the basics of Java and are ready to
write programs that are complicated enough to do some computations. The debugging section
in this chapter illustrates how to interpret and correct syntax errors.
The three terms that you will encounter throughout the book are—primitive type variables,
reference variables, and objects. Chapter 3 makes clear distinctions between these terms and
sets the tone for the rest of the book. An object is a fundamental entity in an object-oriented

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Preface to the Fifth Edition | xxi

programming language. This chapter further explains how an object works. The class
String is one of the most important classes in Java. This chapter introduces this class and
explains how various methods of this class can be used to manipulate strings. Because input/
output is fundamental to any programming language, it is introduced early, and is covered in
detail in Chapter 3. The debugging section in this chapter illustrates how to find and correct
logical errors.
Chapters 4 and 5 introduce control structures used to alter the sequential flow of execution.
The debugging sections in these chapters discuss and illustrate logical errors associated with
selection and looping structures.
Java is equipped with powerful yet easy-to-use graphical user interface (GUI) components
to create user-friendly graphical programs. Chapter 6 introduces various GUI components
and gives examples of how to use these components in Java application programs. Because
Java is an object-oriented programming language, the second part of Chapter 6 discusses
and gives examples of how to solve various problems using object-oriented design
methodology.
Chapter 7 discusses user-defined methods. Parameter passing is a fundamental concept in any
programming language. Several examples, including visual diagrams, help readers understand
this concept. It is recommended that readers with no prior programming background spend
extra time on this concept. The debugging section in this chapter discuss how to debug a
program using stubs and drivers.
Chapter 8 discusses user-defined classes. In Java, a class is an important and widely used
element. It is used to create Java programs, group related operations, and it allows users to
create their own data types. This chapter uses extensive visual diagrams to illustrate how
objects of classes manipulate data.
Chapter 9 describes arrays. This chapter also introduces variable length formal parameter lists.
In addition, this chapter introduces foreach loops and explains how this loop can be used to
process the elements of an array. This chapter also discusses the sequential searching algorithm
and the class Vector.
Inheritance is an important principle of object-oriented design. It encourages code reuse.
Chapter 10 discusses inheritance and gives various examples to illustrate how classes are
derived from existing classes. In addition, this chapter also discusses polymorphism, abstract
classes, inner classes, and composition.
An occurrence of an undesirable situation that can be detected during program execution is
called an exception. For example, division by zero is an exception. Java provides extensive
support for handing exceptions. Chapter 11 shows how to handle exceptions in a program.
Chapter 11 also discusses event handling, which was introduced in Chapter 6. Chapter 12
picks up the discussion of GUI components started in Chapter 6. This chapter introduces
additional GUI components and discusses how to create applets.
Chapter 13 introduces recursion. Several examples illustrate how recursive methods
execute.

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

xxii | Java Programming: From Problem Analysis to Program Design, Fifth Edition

Chapter 14 discusses a binary search algorithm as well as bubble sort, selection sort, insertion
sort, and quick sort algorithms. Additional content covering the sorting algorithms bubble sort
and quick sort is provided online at www.cengagebrain.com.
Appendix A lists the reserved words in Java. Appendix B shows the precedence and
associativity of the Java operators. Appendix C lists the ASCII (American Standard Code
for Information Interchange) portion of the Unicode character set as well as the EBCDIC
(Extended Binary Code Decimal Interchange) character set.
Appendix D contains additional topics in Java. The topics covered are converting a base 10
number to binary (base 2) number and vice versa, converting a number from base 2 to base 8 (base
16) and vice versa, how to compile and execute a Java program using command line statements,
how to create Java style documentation of the user-defined classes, how to create packages, how to
use user-defined classes in a Java program, and enum type. Appendix E gives answers to the odd-
numbered exercises in the text. Those odd-numbered exercises with very long solutions will not
be in the text, but will be provided to students online at www.cengagebrain.com.

How To Use This Book


Java is a complex and very powerful language. In addition to traditional (non-GUI) program-
ming, Java provides extensive support for creating programs that use a graphical user interface
(GUI). Chapter 3 introduces graphical input and output dialog boxes. Chapter 6 introduces
the most commonly used GUI components such as labels, buttons, and text fields. More
extensive coverage of GUI components is provided in Chapter 12.
This book can be used in two ways. One way is an integrated approach in which readers learn
how to write both non-GUI and GUI programs as they learn basic programming concepts
and skills. The other approach focuses on illustrating fundamental programming concepts
with non-GUI programming first, and later incorporating GUI components. The recom-
mended chapter sequence for each of these approaches is as follows:
• Integrated approach: Study all chapters in sequence.
• Non-GUI first, then GUI: Study Chapters 1–5 in sequence. Then study Chapters
7–11 and Chapters 13 and 14. This approach initially skips Chapters 6 and 12, the
primary GUI chapters. After studying Chapters 1–5, 7–11, 13, and 14, the reader can
come back to study Chapters 6 and 12, the GUI chapters. Also note that Chapter 14
can be studied after Chapter 9.
If you choose the second approach, it should also be noted that the Programming Examples in
Chapters 8 and 10 are developed first without any GUI components, and then the programs
are extended to incorporate GUI components. Also, if Chapter 6 is skipped, the reader can skip
the event handling part of Chapter 11. Chapter 13 (recursion) contains two Programming
Examples: one creates a non-GUI application program, while the other creates a program that
uses GUI. If you skip Chapters 6 and 12, you can skip the GUI part of the Programming
Examples in Chapters 8, 10, 11, and 13. Once you have studied Chapter 6 and 12, you can
study the GUI part of the Programming Examples of Chapters 8, 10, 11, and 13.

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Preface to the Fifth Edition | xxiii

Figure 1 shows a chapter dependency diagram for this book. Solid arrows indicate that the
chapter at the beginning of the arrow is required before studying the chapter at the end of the
arrow. A dotted arrow indicates that the chapter at the beginning of the arrow is not essential
to studying the chapter at the end of the dotted arrow.

Chapter 1

Chapter 2

Chapter 3

Chapter 4

Chapter 5

Chapter 6 Chapter 7

Chapter 8 Chapter 9

Chapter 10 Chapter 14

Chapter 11

Chapter 12

Chapter 13

A dotted arrow means that the chapter is not essential to studying the following chapter.

FIGURE 1 Chapter dependency diagram

All source code and solutions have been written, compiled, and quality assurance tested
with Java 6.0 and the version of Java 7.0 available at the time this book was being typeset.

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

FEATURES OF THE BOOK

Four-color
interior design
shows
accurate code
and related
comments.

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

The debugging
sections show
how to find
and correct
syntax and
semantic
(logical)
errors.

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

More than
250 visual
diagrams,
both
extensive and
exhaustive,
illustrate difficult
concepts.

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Numbered Examples
illustrate the key
concepts with their
relevant code. The
programming code in
these examples is
followed by a Sample
Run. An explanation
then follows that
describes what each
line in the code does.

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Notes highlight
important facts
about the concepts
introduced in the
chapter.

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Programming
Examples are
complete programs
featured in each
chapter. These
examples include the
accurate, concrete
stages of Input,
Output, Problem
Analysis and Algorithm
Design, and a
Complete Program
Listing.

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Exercises further
reinforce learning
and ensure that
students have, in
fact, mastered the
material.

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Programming
Exercises challenge
students to write
Java programs with
a specified
outcome.

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

S UPPLEMENTAL
R ESOURCES

The following supplemental materials are available when this book is used in a classroom setting.
Most instructor teaching tools, outlined below, are available with this book on a single
CD-ROM, and are also available for instructor access at login.cengage.com.

Electronic Instructor’s Manual


The Instructor’s Manual that accompanies this textbook includes:
• Additional instructional material to assist in class preparation, including suggestions
for lecture topics.
• Solutions to all the end-of-chapter materials, including the Programming Exercises.

ExamView!
This textbook is accompanied by ExamView, a powerful testing software package that allows
instructors to create and administer printed, computer (LAN-based), and Internet exams.
ExamView includes hundreds of questions that correspond to the topics covered in this text,
enabling students to generate detailed study guides that include page references for further
review. These computer-based and Internet testing components allow students to take exams
at their computers, and save the instructor time because each exam is graded automatically.

PowerPoint Presentations
Microsoft PowerPoint slides are available for each chapter. These slides are provided as a teaching
aid for classroom presentations, either to make available to students on the network for chapter
review, or to be printed for classroom distribution. Instructors can add their own slides for
additional topics that they introduce to the class.

Distance Learning
Course Technology is proud to present online courses in WebCT and Blackboard to provide
the most complete and dynamic learning experience possible. For more information on how

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

xxxiv | Java Programming: From Problem Analysis to Program Design, Fifth Edition

to bring distance learning to your course, contact your local Course Technology sales
representative.

Source Code
The source code is available for students at www.cengagebrain.com. At the cengagebrain.com home
page, search for the ISBN of your title (from the back cover of your book) using the search
box at the top of the page. This will take you to the product page where these resources can
be found. The source code is also available on the Instructor Resources CD-ROM. The
input files needed to run some of the programs are also included with the source code.

Additional Student Files


The Additional Student Files referenced throughout the text are available on the Instructor
Resources CD. Students can download these files directly at www.cengagebrain.com. At the
cengagebrain.com home page, search for the ISBN of your title using the search box at the top
of the page. This will take you to the product page where these resources can be found. Click
the Access Now link below the book cover to find all study tools and additional files available
directly to students. Additional Student Files appear on the left navigation and provide access
to additional Java programs, selected solutions, and more.

Solution Files
The solution files for all programming exercises are available for instructor download at
http://login.cengage.com and are also available on the Instructor Resources CD-ROM. The
input files needed to run some of the programming exercises are also included with the
solution files.

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

A CKNOWLEDGMENTS

There are many people I must thank who, in one way or another, contributed to the success
of this book. First, I would like to thank those who e-mailed numerous comments that helped
to improve on the fourth edition. I am thankful to Professors S.C. Cheng and Randall Crist
for constantly supporting this project.
I owe a great deal to the following reviewers, who patiently read each page of every chapter
of the current version and made critical comments that helped to improve the book:
Nadimpalli Mahadev, Fitchburg State College and Baoqiang Yan, Missouri Western State
University. Additionally, I would like to thank Brian Candido, Springfield Technical Com-
munity College, for his review of the proposal package. The reviewers will recognize that
their suggestions have not been overlooked and, in fact, made this a better book.
Next, I express thanks to Brandi Shailer, Acquisitions Editor, for recognizing the importance
and uniqueness of this project. All this would not have been possible without the careful
planning of Senior Product Manager Alyssa Pratt. I extend my sincere thanks to Alyssa, as well
as to Content Project Manager, Lisa Weidenfeld. I also thank Sreejith Govindan of Integra
Software Services for assisting us in keeping the project on schedule. I would like to thank
Chris Scriver and Serge Palladino of the MQA department of Course Technology for patiently
and carefully proofreading the text, testing the code, and discovering typos and errors.
I am thankful to my parents for their blessings.
Finally, I am thankful to the support of my wife Sadhana, and especially my daughter Shelly,
to whom this book is dedicated. They cheered me up whenever I was overwhelmed during
the writing of this book.
We welcome any comments concerning the text. Comments may be forwarded to the
following e-mail address: malik@creighton.edu.
D.S. Malik

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

APPENDIX A
J AVA R ESERVED
W ORDS

The following table lists Java reserved words in alphabetical order.


abstract else interface switch
assert enum long synchronized
boolean extends native this
break false new throw
byte final null throws
case finally package transient
catch float private true
char for protected try
class goto public void
const if return volatile
continue implements short while
default import static
do instanceof strictfp
double int super

The reserved words const and goto are not currently in use.

939
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

APPENDIX B
O PERATOR
P RECEDENCE

The following table shows the precedence of operators in Java from highest to lowest,
and their associativity.

Operator Description Precedence Level Associativity

. Object member access 1 Left to right


[] Array subscripting 1 Left to right
(parameters) Method call 1 Left to right
++ Postincrement 1 Left to right
-- Postdecrement 1 Left to right

++ Preincrement 2 Right to left


-- Predecrement 2 Right to left
+ Unary plus 2 Right to left
- Unary minus 2 Right to left
! Logical not 2 Right to left
! Bitwise not 2 Right to left

new Object instantiation 3 Right to left


(type) Type conversion 3 Right to left

* Multiplication 4 Left to right


/ Division 4 Left to right
% Remainder (modulus) 4 Left to right

941
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

942 | Appendix B: Operator Precedence

Operator Description Precedence Level Associativity

+ Addition 5 Left to right


- Subtraction 5 Left to right
+ String concatenation 5 Left to right
<< Left shift 6 Left to right
Right shift with sign
>> 6 Left to right
extension
Right shift with zero
>>> 6 Left to right
extension

< Less than 7 Left to right


<¼ Less than or equal to 7 Left to right
> Greater than 7 Left to right
>¼ Greater than or equal to 7 Left to right
instanceof Type comparison 7 Left to right

¼¼ Equal to 8 Left to right


!¼ Not equal to 8 Left to right

& Bitwise AND 9 Left to right


& Logical AND 9 Left to right

^ Bitwise XOR 10 Left to right


^ Logical XOR 10 Left to right

| Bitwise OR 11 Left to right


| Logical OR 11 Left to right

&& Logical AND 12 Left to right

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Appendix B: Operator Precedence | 943

Operator Description Precedence Level Associativity

|| Logical OR 13 Left to right

?: Conditional operator 14 Right to left

¼ Assignment 15 Right to left


Compound Operators
Addition, then
+¼ 15 Right to left
assignment
String concatenation,
+¼ 15 Right to left
then assignment
Subtraction, then
-¼ 15 Right to left
assignment
Multiplication, then
*¼ 15 Right to left
assignment
Division, then
/¼ 15 Right to left
assignment
Remainder, then
%¼ 15 Right to left
assignment
Bitwise left shift, then
<<¼ 15 Right to left
assignment
Bitwise right shift, then
>>¼ 15 Right to left
assignment
Bitwise unsigned-right
>>>¼ 15 Right to left
shift, then assignment
Bitwise AND, then
&¼ 15 Right to left
assignment
Logical AND, then
&¼ 15 Right to left
assignment
Bitwise OR, then
|¼ 15 Right to left
assignment
Logical OR, then
|¼ 15 Right to left
assignment
Bitwise XOR, then
^¼ 15 Right to left
assignment
Logical XOR, then
^¼ 15 Right to left
assignment

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Licensed to: iChapters User

APPENDIX C
C HARACTER S ETS

This appendix lists and describes the character sets for ASCII (American Standard Code
for Information Interchange), which also comprises the first 128 characters of the Uni-
code character set, and EBCDIC (Extended Binary Coded Decimal Interchange Code).

ASCII (American Standard Code for Information Interchange),


the First 128 Characters of the Unicode Character Set
The following table shows the first 128 characters of the Unicode (ASCII) character set.

ASCII

0 1 2 3 4 5 6 7 8 9
0 nul soh stx etx eot enq ack bel bs ht
1 lf vt ff cr so si dle dc1 dc2 dc3
2 dc4 nak syn etb can em sub esc fs gs
3 rs us b ! " # $ % & '
4 ( ) * + , - . / 0 1
5 2 3 4 5 6 7 8 9 : ;
6 < ¼ > ? @ A B C D E
7 F G H I J K L M N O
8 P Q R S T U V W X Y
9 Z [ \ ] ^ _ ` a b c
10 d e f g h i j k l m
11 n o p q r s t u v w
12 x y z { | } " del

945
Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Exploring the Variety of Random
Documents with Different Content
“Oh, but you must have a jam sandwich,” cried Morris with the
pseudo-heartiness characteristic of such occasions.
“Well—if you won’t all think me fearfully, fearfully greedy——”
Minnie hesitated and looked wildly round her, but as no one
appeared in the least aghast at the prospect of her depredations
among the jam sandwiches, she deprecatingly took the smallest one,
murmuring, “Thank you muchly—this is fearful gluttony—‘just one
more crust,’ as the boy said on the burning deck.”
The spasmodic conversation died away.
Presently Hazel said:
“I’ve found the place where we got that white heather last year,
mother. There are some more roots there, if you want to take them
home for the rock garden.”
“Come on and let’s dig then,” said Bertha vigorously, rising as she
spoke.
Morris shot Hazel a glance of gratitude.
He longed to be alone with Rosamund, even while thinking that he
was dreading the pain of bidding her good-bye.
He looked at Miss Blandflower, but Hazel Tregaskis was quicker than
he.
“I shan’t find the way without you,” she declared lightly.
“Come on, Minnie,” shouted Mrs. Tregaskis, already well on ahead.
“There’s no rest for the wicked,” said Minnie mechanically, and went.
Rosamund’s first words were not at all what Morris had expected.
She looked at him sombrely, and remarked almost violently:
“Do you know what’s the matter with Frances? Is Cousin Bertie really
frightened about her?”
“No, not seriously, I don’t think,” he answered, instinctively anxious
to soothe her. “She only said that if Frances wasn’t quite well again
next week she wouldn’t go to Scotland, but would send you and
Hazel alone.”
“I shan’t go if Francie is ill.”
He looked at her, astounded.
“But, Rosamund, what’s the matter? She isn’t ill. Mrs. Tregaskis
herself said that a temperature didn’t mean anything at all with
Frances.”
“Oh, you don’t understand,” she burst out angrily. “Nobody
understands in the least what Frances is to me. Cousin Bertie has
never understood, and never will. You heard what she said just
now.”
He had forgotten.
“That I’m not to go near Frances till to-night. She always treats me
like a child.”
She looked very like one indeed, as she spoke, flushed and
indignant.
“Perhaps Frances was going to sleep, and doesn’t want to be
disturbed.”
“As though I should disturb her! Why, I’ve looked after her ever
since she was a little girl—until we came to live here. Now,” said
Rosamund bitterly, “I’m told to mind my own business and let
Frances mind hers.”
“Never mind,” consoled Morris. “Don’t let’s talk about it. I want to tell
you something, Rosamund.”
Her angry face softened a little, but she seemed unable to dismiss
the subject.
“Nobody has ever understood about Frances and me—ever. I feel
more as though she were my child than my sister.”
Morris was becoming heartily tired of the discussion, and showed
distinct traces of that fatigue in his tone, as he replied perfunctorily:
“Of course I understand—but, really, she’s only three years younger
than you are, isn’t she?”
“Cousin Bertie is always harping on that, and telling Frances not to
be domineered over!”
“Rosamund!” cried Morris, “you really talk as though Mrs. Tregaskis
was always being unkind to you. I can’t understand you. Why, she
simply adores you both—just as though she were your mother.”
He was totally unable to understand why Rosamund, at this, turned
the fury of her eyes full upon him.
“You don’t understand, any more than anyone else.”
“Don’t understand what?” almost shouted Morris. “I don’t
understand you, when you talk like that.”
Nor did he. She seemed to him altogether unbalanced, and as
different as possible from the stately, wonderful Rosamund whom he
had met in the orchard at Porthlew.
“Why do you speak as though Mrs. Tregaskis was unkind, or
unsympathetic?” he asked more gently. “She is devoted to you. You
can’t think how proud she is of you, Rosamund.”
“I’m not her daughter.”
“She feels as though you were. She told me so herself.”
“I wish you hadn’t let her talk to you about me at all,” said
Rosamund unhappily.
“I don’t think you’d say that if you knew how nice and understanding
she was. I—I wish I could explain better.”
Morris felt the impotence of his lame and stammering words before
the deep hostility, which he recognized, although he was at a loss to
account for it, in Rosamund’s silence.
“I haven’t ever told anyone,” she said at last, stammering a little,
“but I’ve always resented being told that Cousin Bertha has done
everything for us and is so fond of us. Of course it’s quite true in a
way, but she’s never made me happy—or Francie either.”
If Morris thought that the fault lay more on Rosamund’s side than on
her guardian’s, he would not say so, but his too expressive face
betrayed him to Rosamund’s quick perceptions.
“You think I’m ungrateful—but I do recognize all the material things
she’s done for us.”
Morris thought her explanation very ungracious, and then chid
himself half-heartedly for criticizing his goddess.
“She’s done more than material things, hasn’t she?” he reminded her
gently. “It’s not as though Porthlew had been an alien atmosphere.
She cares about all the things that matter—books and music and
friendship and other things too. That’s what makes her so wonderful,
I think—that she should have that side to her, as well as the splendid
practical capable side that everyone can see and admire.”
Rosamund looked at him, with a face that seemed to have grown
weary.
“Yes, of course,” she said slowly.
Morris felt, unreasonably, as though he had been weighed and found
wanting, in the balance of that baffled, tired gaze of hers. He
reflected with bewilderment that although she had looked at him like
a child when she had spoken defiantly and angrily of her guardian,
she now looked very much older, and more unhappy.
“What is it, Rosamund?” he asked, half involuntarily, and conscious
of the futility of the question.
“Oh, I don’t know,” she said drearily.
It was the discontented child again.
Morris remained silent, plucking at the tough strands of heather all
round him.
He felt injured.
He had come out on the moor prepared to sacrifice himself, to bid
Rosamund a long farewell, and to take away with him only the
memory of that bitter-sweet parting hour. Surely the intuition of love
should have met him more than halfway. But Rosamund, with
childish perversity, had harped upon the string of her own
grievances, grievances which Morris could not but feel to be for the
most part imaginary ones. She was not thinking about him at all,
and all his wealth of love and self-sacrifice had gone unheeded.
Morris began to feel angry, and, worse still, as though he were being
made a fool of in his own eyes.
It did not calm him to reflect that he would probably appear in
exactly the same light to the penetrating gaze of Bertha Tregaskis.
She was even now advancing slowly towards them, stooping every
now and then to prod at some little root or plant and pull it up into
her capacious basket.
Morris got up abruptly.
“Rosamund, do you know that I’m going away?”
She looked almost as much startled as he could have wished.
“When, Morris? Where?”
“At once,” he said gloomily. “I don’t know where—or care.”
He had meant to ask her if she would “wait for him” in the time-
honoured phrase, but he had not reckoned on having to cram the
whole parting scene, as it were, into the last three minutes of his
interview.
Rosamund also looked at Bertha’s advancing form and spoke rapidly.
“I didn’t know you meant to go away, Morris.”
Was her voice trembling a little?
“I didn’t!” he cried passionately.
Bertha hailed them with a prolonged “coo-ee” that might have been
regarded as superfluous in view of the fact that only some rapidly
diminishing hundred yards now lay between them.
“I didn’t,” repeated Morris earnestly, and was unable to resist
adding, “but—it’s the only way.”

He also made use of that excellent phrase, for which he was


beholden to Mrs. Tregaskis, in conversation with his mother that
evening.
It was more than wasted upon her.
“I don’t know what you mean by ‘the only way,’” she returned with a
sudden irritating assumption of common sense, her lack of which
she habitually dwelt upon with pensive complacency.
“If you want to go yachting, Morris, well and good; but don’t talk in
an affected melodramatic style, as though you were making some
great sacrifice in going, please. It doesn’t ring true, and you know
how I hate little insincerities.”
Nina’s assault was perhaps not utterly unprovoked. A certain jutting
forward of her son’s jaw, a tendency to monosyllabic replies
preceded by the slight start of one roused from a profound reverie,
had conveyed to Nina all too accurately that Morris was enacting, in
his own opinion, the rôle of jeune premier in a drama of self-
sacrifice.
“I’ve already told you that you can start on this yachting trip
whenever you please, so why talk as though it were some
tremendous decision which you had just come to?” she demanded
irritably.
Morris smiled with a superior expression.
“You don’t understand, mother,” he told her, with a touch of
compassion.
Few remarks were more calculated to rouse her annoyance.
“My dear boy, it’s perfectly childish to talk like that. How can there
be anything about you which I, your mother, can’t understand? It
makes one realize how very very young you are, when you talk like
that.”
But even allusions to his youth could not disturb Morris’s exalted
mood.
He was unable to resist giving his mother a hint of the heights to
which he had attained.
“I was up at Porthlew this afternoon,” he said in a meaning tone.
“So I supposed. You always come back in this silly, self-satisfied
frame of mind when you’ve been with those girls, who naturally play
up to your vanity. If that’s the effect of the Grantham girl’s influence,
Morris, the less you see of her the better, for your own sake.”
The fatal word “influence,” combined with the preposterous
implication that Nina had slightingly forgotten Miss Grantham’s very
name, roused Morris to anger at last.
“Rosamund Grantham and I have said good-bye, mother. It was the
only way. Some day I shall come back to her, and find her waiting,”
said Morris, considerably worked up by the pathos of his own
eloquence, and momentarily forgetful that he had received no such
pledge. “But you make it impossible that I should tell you anything
of what I am going through, when you speak as you did just now.”
He walked with sorrowful dignity to the door, confident that his
mother would not allow him to leave the room without giving him
further opportunities for rhetoric.
Nina, in effect, finding herself driven to her last resort, with a
readiness born of much experience, began gently to cry.
“Darling, you know I didn’t mean it if I spoke impatiently. I only
want to sympathize with you and comfort you.”
He turned slowly towards her.
She was deeply relieved that the affaire Rosamund should have been
successfully tided over. Morris was far from being as heartbroken at
the idea of parting from his love as he had been before their final
interview, and the evening passed amid a harmonious rendering of a
strong man’s grief and his mother’s tender sympathy.
Preparations for his journey absorbed Morris for the next twenty-four
hours, during which he and his mother enjoyed the sense of perfect
companionship which was always theirs on the rare occasions when
their respective mental tableaux vivants of one another happened to
coincide, and then he was off.
“Good-bye, my darling boy. Enjoy yourself.”
“Thank you, mother dear. Write to me and”—his voice took on the
slightly deeper note consecrated to the strong-man-in-grief attitude
—“tell me any news of her.”
“Yes, dearest, of course,” tenderly replied Nina, but she refrained
from telling him the only piece of news which transpired during the
next few days: that Frances was not well enough for Mrs. Tregaskis
to leave her, and that Rosamund had refused to accompany Hazel to
Scotland, but remained with her guardian at Porthlew.
“It is tiresome of her,” said Bertha, in a tone more nearly resembling
annoyance than she often used.
“Frances isn’t seriously ill at all, and if she were Rosamund would be
the worst possible person for her. She goes about looking like a
tragedy-queen, as though Frances were at death’s door.”
“Why on earth did you let her stay?” said Nina with more derision
than sympathy in her voice.
“She asked Frederick. You know how tiresome and contradictory he
can be, and of course he knew perfectly well that I didn’t want
Rosamund fussing and fretting on my hands, but he said she could
do as she liked. He always takes up an absurd attitude of having no
authority over those two, as you know.”
“I know. So Hazel has gone alone?”
“I’ve had to send my maid with her, though I should have done that
in any case. I don’t approve of young girls travelling about all over
the country by themselves.”
“Lucky for you that you have girls who can be chaperoned! Look at
poor little me—I can’t run after Morris, let alone send a maid with
him, and have to sit here with a trembling heart, wondering all the
time how things are going with him.”
“That’s always the way with a son, my dear, or a husband either,”
said Bertha, determinedly emphasizing the fact that she, although
not the mother of a son, also possessed a male appendage.
“It’s our part just to sit at home and work and wait, while they have
all the fun,” Nina sighed. “A woman’s life is one long self-sacrifice,”
she murmured.
“It is, when one has to mend and make and nurse, and all the rest
of it,” cordially agreed Bertha, with one fleeting glance at Nina’s
exquisite, empty hands, folded in her lap.
The glance was not lost upon Mrs. Severing, who presently said
reflectively that Mr. Bartlett would no doubt call upon her shortly
with some of his interminable business questions, and she must ask
dearest Bertie to forgive her. It was not her way to put off a matter
of business.
“Unpractical, dreamy creature that I am,” said Nina with a sad,
sweet smile, “I have had too many years’ hard training in looking
after this big estate, ever to be unbusinesslike. Mr. Bartlett always
amuses me so much when he will say that I should make a better
agent than he does.”
“I don’t wonder!” exclaimed Bertha, the dryness of her tone making
it abundantly evident that her emphatic assent was directed towards
Nina’s amusement, and not towards Mr. Bartlett’s opinion of his
employer’s abilities. “No, no, dear. You must stick to your charming
songs. They’re your work in the world,” smiled Bertha tolerantly.
“Dear Bertie! How sweet of you to say so. I’m always afraid of being
just some silly, trivial flowery thing—not of any real use in the
world.”
“The world needs its little speedwell flowers just as much as its
sturdy oak-trees,” laughed Bertha tenderly.
“Yes, dear,” said Nina deftly. “There is room for Mary as well as for
Martha. It always comforts me to remember that.”
Comfort, however, was not the predominant expression on the face
of Mrs. Tregaskis as she heard her friend’s favourite Scriptural
parallel once more enunciated.
“If you’re really waiting for Mr. Bartlett, darling, I mustn’t keep you,”
she said rather hastily. “Anyhow, I must get back to my invalid. She’s
much better to-day, and only fretting at the idea of my having
missed the Scotch visits. Of course one had been rather longing for
a breath of Scottish air, this weather, but I dare say I shall manage
without. It’s an economy, at all events.”
She gave her cheery, plucky laugh.
“How is Morris enjoying Norway? Has he got over his love-lornity?”
Nina laughed a little.
“I think he has. I’ve had a very cheery letter from him, raving about
the fiords and things.”
Bertha looked slightly puzzled.
“The——? Oh, you mean the fjords! Yes, of course they must be
perfectly gorgeous at this time of year,” she remarked thoughtfully,
with the air of a connoisseuse.
“They are just the same at any time of year, dear,” sweetly returned
Nina. “Geoffrey and I went there for a fortnight once—it seems oh
so long ago! It somehow made one think of those far-away days
when everything was couleur de rose——”
There were few topics that Bertha enjoyed less than the
retrospective couleur de rose of her friend’s married life, and she
hastily dragged the conversation back into the living present.
“I’m so very glad about Morris. Give the boy my love when you
write. I wish Rosamund was half as sensible as he is. She goes
mooning about the place as though she’d lost her dearest friend.”
Bertha gave a slightly apologetic laugh at her own acerbity, and
Nina, whose regard for Rosamund always waxed in proportion as her
friend’s waned, murmured with the air of a compassionate angel:
“Poor child! One remembers the heartaches of one’s own youth. The
thoughts of youth are long, long thoughts, Bertie!”
“Well, Morris appears to have curtailed his successfully enough, at all
events,” crisply returned Bertha. “I always said there was stuff in the
boy, Nina, although you’ve spoilt him so outrageously.”
Nina laughed, and kissed Mrs. Tregaskis affectionately as they said
good-bye.
It always pleased her to be told that she spoilt Morris. She had
consistently over-indulged him as a little boy, and did so still in all
matters where his personal pleasures were concerned, provided that
they did not interfere with her wishes. The accusation of spoiling
seemed to add colour to her frequently-voiced conviction that youth
was very hard, and that a mother’s sacrifices often went unheeded.
“I’m afraid I have spoilt him,” she sighed in response to Bertha’s
words. “But after all, Morris has been my only thought for so many,
many years....”
Bertha told herself that really poor Nina was sometimes positively
maudlin, and firmly created a diversion by demanding the loan of
Nina’s seldom-used garden scissors.
“At all events,” she told herself, as she walked briskly away, “I
managed to forestall an allusion, for once, to poor Geoffrey. And
now for my little tragedy-queen!”
But Rosamund, though not undeserving of her guardian’s epithet,
gave less trouble than Bertha had anticipated. With characteristic
want of balance, she was absorbed in one thought only: that of her
sister. As long as Frances remained ill, Rosamund gave little thought
to Morris Severing. Perhaps the measure of her undeveloped lack of
proportion might have been probed by that fact. The memory of a
spoilt illusion might come to vex and grieve the youthfulness of her
spirit later, but that would only be when the nearer, and to her
infinitely more real, solicitude had ceased to be.
And Rosamund, her outlook being honest, knew, and was to know
more clearly yet, that her first love had brought her no nearer to
that reality which lies at the back of all wisdom, and which for her
was still typified by her love for Frances.
VIII
“ROSAMUND!” wrote Hazel from the North. “The most
marvellous things in all the world are happening. I am in
love—with a man who wears an eyeglass—(you know how
I’ve always hated an eyeglass) and he is in love with me.
He is Sir Guy Marleswood, and he’s thirty-four, and quite
six foot, and I don’t think I should mind if he were five
foot nothing. I know I shouldn’t. I’ve known him a
fortnight, and yet we both feel as though we’d known
each other all our lives, and yet it’s new and wonderful all
the time. It’s indescribable. There’s one thing—which I
have to keep reminding myself of, but which will assume
enormous proportions as soon as one begins to do
anything—I mean, write to mother, or wear an
engagement ring. (He’s given me a most beautiful one, a
ruby marquise, only I won’t wear it.) He’s been married
before, and he had to divorce his wife five years ago. I
knew it before we met, because the girls here had been
talking about him, and said that was why their mother had
not asked him to stay in the house, but he came to the
dance, and he is staying at the Ludleys’, a mile away.
That’s where we met, and I’ve seen him nearly every day
since—and the days when I don’t see him are just hell—
only knowing that Heaven may open again at any minute.
“Rosamund dear, I know now that I was a fool ever to let
boys make love to me or propose in the sort of half-and-
half way that a boy does—asking one to wait for him
because he may have enough to marry on in fifteen years’
time, and meanwhile exchange photographs and write
every Sunday afternoon. You know the sort of thing—that
does to tell other girls about, and sentimentalize over
when a waltz that you used to dance with him is being
played. But when it’s the real thing—when a man tells you
that he cares for you and asks you to be his wife—it’s
absolutely and utterly different. Guy asked me the fifth
time he saw me. He told me about his wife first. The odd
thing is, that I don’t mind. Of course I shouldn’t mind
about the moral part of it, anyhow—I mean whom God
hath joined together and all that—but I don’t seem even
to mind about his having once loved her and married her.
They only cared for one another a very little while, and it’s
all past and over. The present is ours—and more glorious
and wonderful than any words can ever say. As for the
future—he says he is going to marry me before the end of
the year. And I am to put off my other visits and come
home this week, and then he will write to daddy, and
come down to Cornwall. Of course it isn’t daddy that
counts at all, since I can manage him perfectly, but I have
a sort of an idea that Guy will get exactly what he wants,
even out of mother. He’s the sort of person that does.
“We haven’t told anybody anything. I haven’t the slightest
doubt that Lady Alistair has guessed, and the girls too, but
even if she writes to mother it’ll only bring things to a
crisis rather sooner. I’m writing to her myself this evening,
so she’ll know by the time you get this.
“I’m not afraid of anything or anyone in the world. Guy
and I have found one another, and nothing else matters.
Besides, I know he’ll manage everything!
“As ever,
“Your loving
“Hazel.”
If Hazel’s letter brought a strange wondering sense of disquietude to
Rosamund, and that not wholly on her cousin’s account, the much
shorter note which she had sent her mother apparently produced no
such effect. Bertha appeared at luncheon with a brow but slightly
corrugated, and only an added tinge of briskness in her manner to
betray perturbation of spirit.
“I see you’ve had a letter from Hazel by the midday post,
Rosamund,” observed Miss Blandflower in the middle of luncheon,
with a praiseworthy desire to dissipate the slight atmosphere of
constraint which had lately been noticeable at meals, in spite of the
valiant and hearty efforts of Mrs. Tregaskis. “When does she return
to the bosom of her family?” She gave a slight giggle in lieu of
quotation marks.
Rosamund hesitated, felt her cousin Bertha glance sharply at her,
and answered nervously:
“I’m not sure—soon, I think.”
“She has two more visits to pay,” said Mrs. Tregaskis coldly. “You
knew that, Rosamund.”
Her husband looked up suddenly.
“She’s coming back on Friday. You knew that, Bertha,” he said
mockingly. “I understand that our parental sanction is required to an
engagement of marriage. Very gratifying, I’m sure, in these
emancipated days.”
Miss Blandflower turned startled eyes from one to the other of
Hazel’s parents.
“They say one wedding makes another,” she sighed with nervous
inappositeness. “But is it really—who is it...?”
“My dear Minnie, Hazel is a silly little girl who ought never to have
been allowed to pay visits without a chaperoning mamma. It serves
me right for having relaxed my rule—but one can’t be in two places
at once, and really these young ladies require such a lot of looking
after!”
Bertha sighed gustily.
“One only wonders how you can manage in the marvellous way you
do, with so much upon your hands,” said Minnie, feeling that this
remark, although far from being original, came at any rate from a
safe stock, and might be more acceptable than further questions.
At all events it steered the conversation into smoother channels, and
no further allusions were made in public to Hazel’s affairs, until three
days later, when Hazel herself returned to Porthlew.
Rosamund was instantly conscious of an indefinable change in her
cousin.
Self-possessed Hazel Tregaskis had always been, but the youthful
security of her manner had somehow deepened into an impression
of inward assurance that held less of self-confidence, and more of
some larger stability, that would not be easily shaken. When her
mother greeted her with matter-of-fact warmth, and said gaily,
“Well, my little girl, I’m glad to have you under my wing again; I
think it’s the last time we must let you go gallivanting off on your
own for the present,” Rosamund saw that Hazel did not give the
petulant shrug or grimace with which the girl Hazel would have
received such a greeting, but looked at her mother with a strange,
remote look that held something of an almost impersonal
compassion.
It was that same look, Rosamund thought, which angered Mrs.
Tregaskis when her daughter resolutely asked her for an interview
that evening.
“No, my darling; I’m not going to let you stay and chatter now.
You’ve had a long journey, and must pop off to bed early. We’ll have
a long talk to-morrow. Dad and I are not at all angry with you, but
I’ve had a letter from Jessie Alistair, and it’s quite plain that I ought
never to have let you go and stay away without me. Now run along
with Rosamund, my pet.”
“What did Lady Alistair say?”
“I shall talk to you about that to-morrow. I am not at all angry with
you, Hazel, but one thing you and Rosamund may as well
understand, since I suppose you’ve told her all about it. You may flirt
with boys of your own age, if you like, and have all the fun that’s
natural and proper, but——” Bertha Tregaskis paused. She spoke
with a quiet and good-humoured implacability, her hands resting on
her broad hips, and her resolute mouth set firmly. “But—to flirt and
get yourself talked about with a married man, is—a—thing—I—don’t
—allow. See, darling?”
Rosamund caught her breath and looked at her cousin. Hazel, who
seldom or never blushed, had flushed the slow, deep crimson of a
woman who hears herself insulted.
“Sir Guy Marleswood is not a married man,” she said slowly. “At
least, neither he nor I think so, which is what matters, after all. He
divorced his wife five years ago. He has asked me to marry him.”
“Very well, darling. When he writes and asks the permission of your
parents, we shall see. But a man of four- or five-and-thirty, who has
led the sort of life that he has led, does not generally want to marry
a little girl of nineteen, even though he may be dishonourable
enough to play at making love to her.”
But this agreeable theory was shattered next day, when Sir Guy
Marleswood wrote a formal statement of his position, and an almost
equally formal request for his daughter’s hand in marriage, to
Frederick Tregaskis. He also stated unemphatically that the following
day would find him at Porthlew Railway Hotel.
Thereafter, Rosamund watched the storm break over the household
with a strangely aching heart.
Bertha regarded Sir Guy as a married man, and said so staunchly.
Frederick Tregaskis, whom Rosamund had never yet heard to agree
with his wife, declined to view the question from an ethical
standpoint, but declared Hazel too young to enter upon a marriage
which would of necessity be regarded more or less dubiously by the
world in general.
“Wait another five years,” he remarked grimly to his daughter, “and
see if you can’t do better for yourself than a divorced baronet fifteen
years older than yourself.”
“No,” said Hazel, her small face set like a flint. “He wants me to
marry him now.”
“I dare say. And I want you to wait. I suppose you owe something to
your father?”
“Yes,” she said, and began to cry. “But not everything in the world. I
owe something to myself. It’s my life.”
It was the passionate cry for individualism that Rosamund had heard
from Morris Severing.
But Hazel Tregaskis, unlike Morris, was directing all the energies of
her will into one channel. And Rosamund, watching, saw those
energies guided and strengthened day by day by the stronger force
that held steadfast behind her.
Guy Marleswood was not of those who fail.
Before the close of that year, the day came when he extorted from
the exasperated Frederick: “Marry her, then. I see you mean to do it,
both of you, and it may as well be with my consent as without it.
Anything to put an end to the subject.”
“Thank you, sir,” said Sir Guy imperturbably. “I will go and tell Mrs.
Tregaskis that we have your consent to the marriage.”
“You will do nothing of the kind. I shall tell her myself. I may as well
get some satisfaction out of it,” said Frederick viciously.
He sought his wife in the library, where she sat, looking unusually
disheartened, amid a pile of leaflets.
“Bertha, you are about to be relieved of one of your responsibilities.”
“I’m thankful to hear it,” she returned wearily.
“I have decided to give Hazel into Marleswood’s keeping.”
“Frederick! You can’t. You’re mad. A child of nineteen—and a
marriage that’s no marriage—she’ll be no more married in the eyes
of God than if she were openly living as that man’s mistress.”
“I’m not concerned with the eyes of God,” said her husband in
detached tones. “It’s perfectly evident to mine that if we don’t give
our consent they mean to do without it, and I don’t choose to have
my daughter making a runaway match. We had better give in
gracefully while it is still possible, Bertha. Marleswood is not the sort
of man to heal a breach, if it came to that.”
“What do you mean?”
“I mean that we don’t want to be cut off from the little girl for ever
after her marriage,” said Frederick, his voice shaking a very little.
“That’s what it’ll mean if we let her go from under our roof in
defiance, Bertha.”
“Hazel is an infatuated, self-willed child, but she is not heartless,”
cried Bertha.
“I do not intend to put her to the test.” Frederick Tregaskis had
regained his habitual dryness of utterance.
With unwonted consideration, he added a word of consolation for his
wife.
“I may as well tell you that I am perfectly satisfied that Marleswood
is a good fellow in every way, and devoted to her. The whole thing,
after all, amounts to a question of conscience, which she is entitled
to judge for herself.”
“She’s not,” flashed Bertha. “She’s only a child, and ought to accept
the ruling of her parents until she’s old enough to judge for herself.”
“I have no doubt,” said Frederick drily, “that all parents, taken as a
class, would agree with you. Unfortunately for ourselves, however,
we have passed into an era where the individual, and not the class,
will rule.”
He walked out of the room, looking older and more deeply lined than
ever.
Rosamund found Mrs. Tregaskis, who never broke down, weeping
violently among the piles of disordered pamphlets.
“Cousin Bertie! Don’t!” cried Rosamund fearfully. “Is it about Hazel?”
Bertha raised a piteously mottled and disfigured face.
“I’m beaten,” she cried. “Frederick has consented to this iniquitous
marriage, and nothing can stop it now. My little girl, whom I’ve
brought up to be good, and to whom I’ve tried to teach religion—
that she should be willing to break my heart, and rush deliberately
into sin, the first time temptation comes near her!”
“No—no. It’s not that. She doesn’t think it’s sin. She doesn’t believe
it’s sin—not for an instant. Her point of view is different.”
“Her point of view!” cried Bertha bitterly. “How dare you talk to me,
a woman of fifty, of such preposterous nonsense? You and she are
children; you know nothing of life, you’ve had no experience. How
can Hazel judge of what is right or wrong? She’s a child—a child.”
In the vehement repetition of the assertion, it seemed to Rosamund
that she found her clue to Bertha Tregaskis’s impotent suffering. She
would not, could not, admit in her daughter any claim to the rights
of an individual.
Hazel’s judgment, unrecognized by her mother, carried with it no
amazement to Rosamund.
Certain faiths, certain scruples and acceptances inborn in Rosamund
and Frances, had been the veriest lip-service to the child Hazel
always. Rosamund recognized in her the purest and most natural
type of highly-evolved paganism.
“You know, Rosamund, I’m not doing anything wrong, although they
won’t believe it. It isn’t wrong to me, and I don’t believe in an
abstract right and wrong. Each individual case has its own laws.”
“Should you do it if you thought it was wrong?”
“I don’t know,” said Hazel thoughtfully. “I can’t imagine seriously
believing that it would matter to God, one way or the other. Should
you? Frances wouldn’t, one knows.”
“If I did it,” slowly said Rosamund, “it would be as a deliberate
choice between good and evil. I should believe myself to be breaking
God’s law—but I might do it, if I thought it worth while.”
She knew that if, as she said, it seemed to her worth while, no laws
of God or man should bind her. But she would break them of
deliberate intent, whereas to Hazel Tregaskis they were non-
existent, myths designed for the wanton frightening of children.
Rosamund recognized the absolute sincerity of Hazel’s point of view,
and sometimes found herself wondering what Sir Guy’s might be.
One day, very soon before the marriage, she held an odd little
conversation with him, standing in the wintry sunshine of the
terrace. Frederick Tregaskis was ahead of them, grimly poking with a
walking-stick at a little drain that was choked with leaves.
“He’s been very kind to me,” said Sir Guy abruptly, indicating with a
gesture the odd little figure.
“I think that he really likes you very much,” said Rosamund. “And
though he would be very angry at being told so, I have always
known that Cousin Frederick adores Hazel.”
Sir Guy nodded with full comprehension.
“Yes, of course. She knows that, too. It’s been the best thing in her
life so far—that and having you and your sister here.” He paused for
a moment or two. “You know,” he said slowly, “I want to try and
make up to her for everything that she hasn’t had, so far. She ought
to have everything. She seems, somehow, so made for happiness.”
“I have never seen Hazel sad,” said Rosamund, rather surprised. “I
think she is happy by nature.”
“Yes, though an atmosphere which might perhaps seem an
unsympathetic one——”
He left the sentence unfinished, and it required no effort on
Rosamund’s part to conjecture his meaning. Sir Guy resented, none
the less implacably that his resentment was expressed by implication
only, the attitude of Mrs. Tregaskis towards her daughter. That Hazel
herself had never resented it, and had only opposed to it the bright
glancing hardness of her impenetrable self-confidence, did not,
Rosamund felt, in any way diminish his perfectly silent ire. Mrs.
Tregaskis herself would be forced to recognize that in this man
fifteen years her senior, Hazel had found champion as well as lover,
knight as well as comrade.
Rosamund turned away with an aching heart, wondering dimly
whether her need had not been greater than Hazel’s.
After the formal consent given by Frederick Tregaskis, there had
been no further discussions between Sir Guy and Mrs. Tregaskis. She
accepted her defeat with the sort of grim gallantry that would
always be characteristic of her, and, as far as Rosamund knew,
attempted no appeal to Hazel. But she aged more perceptibly in the
weeks before Hazel’s marriage than during all the five years that
Rosamund had passed at Porthlew.
No other indication that her guardian recognized defeat was evident
to Rosamund’s eyes. Her manner to her daughter was what it had
always been—kindly, authoritative, at times possessive. She
admitted Sir Guy’s claims to much of her daughter’s time, and even
seemed disposed, gradually, to concede to him rights which he had
not tried to arrogate for himself.
“You mustn’t let this little person be too much in London,” she
observed, with a hand upon Hazel’s shoulder. “We’re very excitable,
and it knocks us up. I had to be a very strict mamma and bring her
home long before the dances had come to an end last year.”
“If we take the St. James’ Square flat, there is no reason why we
shouldn’t spend all the week-ends Hazel likes at Marleswood.”
“Well, I don’t know about week-ends,” said Bertha doubtfully.
“They’re not very restful. I think a home in the country and an
occasional fling in London must be Hazel’s programme.”
She spoke with her customary matter-of-fact assurance and kindly
good sense.
Sir Guy fixed his objectionable monocle more firmly.
“That,” he observed in a detached manner, “is a decision which I
shall leave entirely to my wife.”
If Mrs. Tregaskis found it necessary to readjust her forces after this,
the readjustment was made silently and without delay. But it was
very shortly after that, when it only wanted a week to Hazel’s
wedding-day, that Rosamund again found Cousin Bertha in the
library, struggling with hard, choking sobs. Hazel hung over her,
caressing her with most unwonted demonstrativeness and with tears
in her own pretty eyes. But that they were tears of the merest
surface pity and tenderness was abundantly obvious even without
the gently mournful observation which she made to Rosamund that
evening.
“Poor mother! I hate to see her minding it so, but you know,
Rosamund, I can’t feel as unhappy as I ought.”
“Don’t you wish—sometimes—that you’d waited, as they begged you
to? It would have been the same for you in the end.”
“The same for me, and the same for them,” returned Hazel crisply.
“They wouldn’t have liked it any better ten years hence—at least
mother wouldn’t. I believe daddy’s reconciled already. Mother wants
me to be happy, but in her way.”
“Are you really happy, when you know she is miserable?” spoke
Rosamund with more curiosity than compassion. Hazel coloured, but
faced her cousin with unflinching honesty.
“Yes,” she said, “I am. It’s of no use to pretend, Rosamund. I am
happier than I have ever been in my life. Of course, I should have
preferred it if everything had been straightforward, and there hadn’t
been all this fuss, and having to extort a consent—but it would have
been just the same if they hadn’t given it. Do you know, that’s the
pathos of it, to my mind—they couldn’t do anything. Guy and I
would have married without their consent, just as much as with it.”
“He asked you to, I suppose,” said Rosamund, as though stating a
fact.
Hazel pushed her curling tawny hair from her forehead.
“He asked me if I would, if it came to that, and of course I said yes.
But we both knew it wouldn’t come to that, and that mother would
have to give in. I used to think that if one’s parents forbade a thing,
it became impossible ipso facto, but it doesn’t. They just can’t do
anything at all.”
To Rosamund, Hazel had summed up the situation in that sentence.
They could not do anything at all.
The wedding took place quietly at Porthlew, and they said good-bye
to Hazel, radiant-eyed, and clinging in an unwonted embrace to her
father at the last moment.
Then she drove away with her husband, and Miss Blandflower, in a
piping soprano, remarked to Rosamund:
“It’s like a death in a house, isn’t it? But we must all try and take her
place, now.”
The suggestion drove Frederick, snarling disgustedly, into the study.
Frances went quietly to put away some of the litter in Hazel’s room,
while Rosamund, feeling herself useless and in the way, yet hung
helplessly in the vicinity of Nina Severing, who had remained with
Bertha in the drawing-room after the departure of the few guests.
But no word of Morris reached her.
Nina was murmuring consolation to her friend who, for once
inactive, sat gazing heavily into the fire.
“After all, dearest, the young birds will fly out of the old nest and
leave it desolate. It’s nature.”
Bertha groaned.
“It’s not the selfish loss to myself that I mind, Nina, but the thing
she’s done. If I were giving her to some simple, honest boy of her
own age, how gladly I’d see her go. We mothers don’t ask more
than that, after all—just to see the children happy.”
“I know,” breathed Mrs. Severing. “It’s all one lives for.”
“I’ve no plans or wishes for myself—it’s all for them,” muttered
Bertha disjointedly. “What else has one to care about—an old
gargoyle....”
Nina straightened herself slightly.
“‘Having outlived hope, fear, desire....’” she quoted softly, at the
same time turning her long neck so that the firelight fell upon her
burnished hair and exquisite, appealing profile.
“A man she’s only known a few months,” pursued Bertha bitterly.
“And she’ll disobey her parents, the mother who’s loved and guarded
and cherished her all her life, and break their hearts, for his sake.”
“God grant the poor child may not regret it bitterly one day,”
breathed Nina piously.
There was a long pause.
“Well!” said Bertha, and slowly stood up. “There’s a lot to be done.”
“Do let me help you, dearest.”
“Thanks, Nina, if you would. The girls are somewhere, I suppose.”
“Ah, they’ll be a comfort to you, I hope. They who owe you even
more than Hazel does, if possible.”
“One does what one can. It seems to me that it’s all give, give, give
on our side, and take, take, take on theirs. I feel rather like an
unfortunate pelican feeding its young, sometimes.”
With the words, and the curt laugh that dismissed them, Bertha
Tregaskis regained possession of herself.
IX
ROSAMUND, though unhappy, was not as unhappy as she would
have liked to think herself. The defection of Morris Severing,
although gaining in poignancy by contrast with Hazel’s serene
happiness, was a sorrow of the emotions only, and a certain fierce
sincerity of outlook prevented Rosamund from rating it otherwise.
But she felt that she could have borne it better had the
disappearance of her quondam lover touched the mainsprings of her
life, and left that life dignified by a lasting grief, instead of merely
rendered unprofitable and savourless from an unrecognized sense of
vague discontent.
“I don’t know what Rosamund’s grievance is!” her guardian was
exasperated into exclaiming, nearly a year after Hazel’s marriage. “I
don’t believe she knows herself.”
And, in so saying, diagnosed the case.
Rosamund Grantham, after the manner of the modern generation,
had yet to find herself, and suffered accordingly.
It need scarcely be added that she did not confine her sufferings to
herself.
Frances, overwhelmed by the difficulties of reconciling
responsiveness to Cousin Bertie’s bracing councils of self-reliance,
with submission to Rosamund’s intensely protective and rather
overpowering solicitude, sought more frequently than ever the
soothing society of Nina Severing.
That gentle soul was passing through a period of storm of which she
presently confided the outline to Frances.
“Sometimes, darling, as I sit here alone through the long evenings, I
wonder if my life might have been different if I’d been a more
religious woman. You see, Francie, I married very, very young. I
wasn’t much older than you are now. My husband was not a man
who believed in any very definite creed, and I was young enough to
be altogether influenced by him.”
It was ever Nina’s custom to lay the errors and omissions of her past
at the door of Geoffrey Severing and her youthful marriage.
“Should you like to be a Roman Catholic?” asked Frances suddenly.
“It’s a very beautiful religion, and of course beauty is a religion in
itself, to an artist,” said Nina thoughtfully. “Why do you ask?”
“I’ve often thought,” said Frances very shyly, “that I should like it
myself. It seems such a thorough-going sort of religion. When we
were little, my mother had a Catholic maid—an Irish girl—and she
used to tell us a lot about it. And she was so particular about not
eating meat on certain days and going to Mass every Sunday. She
had to walk quite a long way, but I don’t believe she ever missed
going. Of course she was very superstitious, and used to want us to
wear medals and charms and things, but some of the prayers she
taught us were nice. My mother was a Catholic by birth, too, though
she never went to church or anything.”
“If I were anything, I should certainly be a Catholic,” said Nina with
extreme conviction in her tone. “It’s the only creed which appeals to
me in the slightest degree. It is so beautiful—all that music and
those touching ideas about the Virgin and everything.”
“But—don’t you believe?—isn’t the Church——?” murmured Frances,
embarrassed.
“Dear child, I am afraid the orthodox forms mean very little to me. I
would never wilfully cause pain to any human being, and I try to
help the sadness of the world with my little songs, but that is all. But
I would never shatter the innocent faith of another soul, although I
have outgrown the need of form and ritual myself.”
“Does one outgrow it?” wistfully asked Frances, whose whole nature
unconsciously craved the discipline which is inseparable from any
creed, faithfully followed out in practice.
“Not all of us,” tenderly said Nina, conscious of the exquisite contrast
between the matured, self-reliant soul, made strong through
suffering, and the innocent, inquiring child at her knee. “Not all of
us, dear. Some plants need a support round which to cling, whilst
others stand alone—always alone.” Her voice deepened slightly as
she mused broodingly for a moment on the pathos and beauty of
this horticultural parable. It came as a slight shock when Frances,
generally the most sympathetic of listeners, observed in
unmistakably self-absorbed accents:
“I think that I shall always want a support. It seems to me that I am
meant to live by rule—not by my own judgment at all. That’s why I
like the Roman Catholic idea of the Church being infallible. It would
be such a guide.”
Nina was aware that to no one else would Frances have spoken so
unreservedly, and the reflection was soothing, but it did not prevent
a slight stiffening of tone in her reply.
“Really, dear? But the surest guide in the world is the golden rule
which I have tried to live up to all my life—Never think of yourself at
all. Somehow, if one gives all one’s thoughts and time to other
people, one finds that God takes care of the rest.”
Nina was herself rather surprised at the beauty of the sentiment as
she put it into words, and it served to restore her not very deeply
ruffled serenity.
“I will lend you some books, Frances, if you really want to know
something about various creeds. The religion of Buddha is, to my
mind, the most beautiful of them all,” reflectively said Nina, who had
once read portions of Sir Edwin Arnold’s translation of the “Light of
Asia,” and was persuaded that she had studied it deeply. “It was the
foundation of the Roman Catholic religion, of course—they borrowed
a great deal from it.”
“I should like to read it very much.”
Frances wanted to read anything which spoke, however indirectly, of
Roman Catholic doctrines. If Nina guessed as much, however, she
did not impart her surmise to the vigorously orthodox Bertha
Tregaskis.
That this discreet reticence had been justified was made
superabundantly evident when Mrs. Tregaskis first became aware of
the Romanistic tendencies of her ward.
“People of seventeen must do what they’re told,” she said serenely,
but with an undercurrent of severity. “When you’re one-and-twenty,
Francie, we’ll talk about it again, and meanwhile I strongly advise
you not to think about the subject. You are much too young to
decide such a matter without knowing a great deal more about it,
and from your own showing all this simply arises from restlessness
and desire for excitement. Religion is too serious a matter to be
played with, my dear little girl.”
A certain look of flintlike impenetrability came over Frances’ young
face as she looked at her guardian, and she said nothing more. But
Mrs. Tregaskis was much too acute to suppose that her silence
denoted submission.
“Take her to London,” growled Frederick, when his wife, in her
perplexity, put the case before him. “You ought to get her away from
that silly woman’s influence.”
Bertha did not ask “What silly woman?” since she rightly recognized
that her husband thus denoted her dearest friend, but she decided
to follow his advice.
“We’ll have a month in London, and see all the sights,” she cried.
“Just you and I and Rosamund, Francie, and be regular country
cousins, and go to the National Gallery and British Museum, and a
theatre or two from the dress-circle. Never mind about planting the
bulbs, dear—no, I don’t mind leaving them to Grant, and the garden
must just get on without me for a week or two.”
She stifled a sigh heroically.
“This trip is absolutely for the sake of the girls,” she told Nina
Severing. “Neither of them takes any natural healthy interest in
gardening or in the animals and things, as Hazel used to do, so I
must try what London will do for them. Really, girls are a problem.”
“Nothing to a boy,” sighed Nina. “There’s Morris wandering half over
Europe, in the most unsatisfactory manner, pretending that he is
studying languages, and really doing nothing at all except loaf. I’ve
told him he ought to come back and look after the place in earnest,
but he makes one excuse after another——”
“It’s too bad,” said Bertha sympathetically. “Perhaps if he came back,
now that he and Rosamund are a little older and have rather more
sense....”
“Oh, my dear! he’s got over that nonsense long ago. I always told
you it wouldn’t last. ‘Weak and unstable as water,’ that’s what my
poor Morris is.”
Bertha did not remind Mrs. Severing that everything had been done
to insure the instability of Morris in this particular case. She only said
affectionately:
“Well, good-bye, Nina darling. Don’t forget to take pity on my old
man, since I can’t drag him to London.”
“He must come and cheer me up some afternoon, if he will,”
cordially responded Nina. Both ladies were perfectly aware that
Frederick Tregaskis would do nothing of the sort, and that there
were few things less conducive to the cheering up of either than an
encounter between him and Mrs. Severing. But they exchanged their
fallacious hopes with an air of affectionately reassuring one another.
“I’ve one comfort,” declared Bertie, “I’m hoping to see a very old
friend of mine in town: Sybil Argent. I believe she and her son are
there for a few weeks.”
“Didn’t she become a Catholic?” asked Nina, with a sudden air of
intense interest, which provoked Bertha to a display of extreme
nonchalance instantly.

You might also like