Free Essay

Java Programming

In: Computers and Technology

Submitted By onlinerajnish
Words 15086
Pages 61
A Programmer’s Guide to
Java™ SCJP Certification
Third Edition

This page intentionally left blank

A Programmer’s Guide to
Java™ SCJP Certification
A Comprehensive Primer
Third Edition

Khalid A. Mughal
Rolf W. Rasmussen

Upper Saddle River, New Jersey • Boston • Indianapolis • San Francisco
New York • Toronto • Montreal • London • Munich • Paris • Madrid
Capetown • Sidney • Tokyo • Singapore • Mexico City

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests.
For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419 corpsales@pearsontechgroup.com For sales outside the United States please contact:
International Sales international@pearson.com Visit us on the Web: informit.com/aw
Library of Congress Cataloging-in-Publication Data
Mughal, Khalid Azim.
A programmer's guide to Java SCJP certification : a comprehensive primer / Khalid A.
Mughal, Rolf W. Rasmussen.—3rd ed.
p. cm.
Previously published under title: A programmer’s guide to Java certification.
Includes bibliographical references and index.
ISBN 0-321-55605-4 (pbk. : alk. paper)
1. Electronic data processing personnel--Certification. 2. Operating systems (Computers)—Examinations--Study guides. 3. Java (Computer program language)--Examinations-Study guides. I. Rasmussen, Rolf (Rolf W.) II. Mughal, Khalid Azim. Programmer’s guide to Java certification. III. Title.
QA76.3.M846 2008
005.2'762--dc22

2008048822

Copyright © 2009 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to:
ISBN-13: 978-0-321-55605-9
ISBN-10:
0-321-55605-4
Text printed in the United States on recycled paper at Courier in Stoughton, Massachusetts.
First printing, December 2008

To the loving memory of my mother, Zubaida Begum, and my father, Mohammed Azim.
—K.A.M.

For Olivia E. Rasmussen and
Louise J. Dahlmo.
—R.W.R.

This page intentionally left blank

Contents Overview

Foreword
Preface
1 Basics of Java Programming

xxxv xxxvii 1

2 Language Fundamentals

19

3 Declarations

39

4 Access Control

103

5 Operators and Expressions

159

6 Control Flow

203

7 Object-Oriented Programming

283

8 Nested Type Declarations

351

9 Object Lifetime

389

10 Fundamental Classes

423

11 Files and Streams

467

12 Localization, Pattern Matching and Formatting

531

13 Threads

613

14 Generics

661 vii viii

CONTENTS

15 Collections and Maps

747

A Taking the SCJP 1.6 Exam

851

B Objectives for the SCJP 1.6 Exam

857

C Objectives for the SCJP 1.6 Upgrade Exam

863

D Annotated Answers to Review Questions

869

E Solutions to Programming Exercises

935

F Mock Exam

959

G Number Systems and Number Representation
Index

1005
1013

Contents

List of Figures

xxiii

List of Tables

xxvii

List of Examples

xxix

Foreword

xxxv

Preface
1 Basics of Java Programming
1.1
1.2
1.3

1.4
1.5
1.6
1.7
1.8
1.9
1.10

Introduction
Classes
Declaring Members: Fields and Methods
Objects
Class Instantiation, Reference Values, and References
Object Aliases
Instance Members
Invoking Methods
Static Members
Inheritance
Aggregation
Tenets of Java
Review Questions
Java Programs
Sample Java Application
Essential Elements of a Java Application
Compiling and Running an Application
Review Questions
Chapter Summary
Programming Exercise

xxxvii
1
2
2
3
4
4
6
6
7
7
10
12
13
13
15
15
15
16
17
18
18

ix

x

CONTENTS

2

Language Fundamentals
2.1

2.2

2.3

2.4

3

Basic Language Elements
Lexical Tokens
Identifiers
Keywords
Literals
Integer Literals
Floating-Point Literals
Boolean Literals
Character Literals
String Literals
White Spaces
Comments
Review Questions
Primitive Data Types
Integer Types
The char Type
The Floating-Point Types
The boolean Type
Review Questions
Variable Declarations
Declaring and Initializing Variables
Reference Variables
Initial Values for Variables
Default Values for Fields
Initializing Local Variables of Primitive Data Types
Initializing Local Reference Variables
Lifetime of Variables
Review Questions
Chapter Summary
Programming Exercise

Declarations
3.1
3.2

3.3

3.4

Class Declarations
JavaBeans Standard
Naming Patterns for Properties
Naming Patterns for the Event Model
Method Declarations
Statements
Instance Methods and the Object Reference this
Method Overloading
Constructors
The Default Constructor
Overloaded Constructors
Review Questions

19
20
20
20
20
21
22
22
23
23
25
25
26
27
28
28
29
29
30
31
31
31
32
33
33
34
35
35
36
37
37

39
40
41
41
42
44
45
45

47
48
49
51
52

CONTENTS

xi

3.5

3.6

3.7

3.8

3.9

Enumerated Types
Declaring Typesafe Enums
Using Typesafe Enums
Declaring Enum Constructors and Members
Implicit Static Methods for Enum Types
Inherited Methods from the Enum Class
Extending Enum Types: Constant-Specific Class Bodies
Declaring Typesafe Enums Revisited
Review Questions
Arrays
Declaring Array Variables
Constructing an Array
Initializing an Array
Using an Array
Anonymous Arrays
Multidimensional Arrays
Review Questions
Parameter Passing
Passing Primitive Data Values
Passing Reference Values
Passing Arrays
Array Elements as Actual Parameters final Parameters
Variable Arity Methods
Calling a Varargs Method
Varargs and Non-Varargs Method Calls
The main() Method
Program Arguments
Review Questions
Chapter Summary
Programming Exercises

4 Access Control
4.1
4.2

4.3
4.4
4.5
4.6

Java Source File Structure
Packages
Defining Packages
Using Packages
Compiling Code into Packages
Running Code from Packages
Searching for Classes
The JAR Utility
System Properties
Review Questions
Scope Rules
Class Scope for Members

54
54
54
55
57
58
59
62
63
69
70
70
71
72
74
75
79
81
82
84
86
87
89
90
91
93
94
95
96
100
101

103
104
105
106
107
115
117
117
120
122
123
129
129

xii

CONTENTS

4.7
4.8

4.9

4.10

5

Block Scope for Local Variables
Accessibility Modifiers for Top-Level Type Declarations
Other Modifiers for Classes abstract Classes final Classes
Review Questions
Member Accessibility Modifiers public Members protected Members
Default Accessibility for Members private Members
Review Questions
Other Modifiers for Members static Members final Members abstract Methods synchronized Methods native Methods transient Fields volatile Fields
Review Questions
Chapter Summary
Programming Exercise

Operators and Expressions
5.1

5.2

5.3
5.4

5.5

Conversions
Widening and Narrowing Primitive Conversions
Widening and Narrowing Reference Conversions
Boxing and Unboxing Conversions
Other Conversions
Type Conversion Contexts
Assignment Context
Method Invocation Context
Casting Context of the Unary Type Cast Operator: (type)
Numeric Promotion Context
Precedence and Associativity Rules for Operators
Evaluation Order of Operands
Left-Hand Operand Evaluation First
Operand Evaluation before Operation Execution
Left to Right Evaluation of Argument Lists
The Simple Assignment Operator =
Assigning Primitive Values
Assigning References
Multiple Assignments
Type Conversions in Assignment Context

131
132
135
135
136
138
138
139
141
142
143
144
146
147
148
150
150
151
152
153
154
157
157

159
160
160
161
162
162
163
164
164
164
165
166
168
168
168
169
169
169
169
170
171

CONTENTS

xiii

5.6

5.7
5.8

5.9
5.10
5.11

5.12

5.13

5.14
5.15

Review Questions
Arithmetic Operators: *, /, %, +, Arithmetic Operator Precedence and Associativity
Evaluation Order in Arithmetic Expressions
Range of Numeric Values
Unary Arithmetic Operators: -, +
Multiplicative Binary Operators: *, /, %
Additive Binary Operators: +, Numeric Promotions in Arithmetic Expressions
Arithmetic Compound Assignment Operators: *=, /=, %=, +=, -=
Review Questions
The Binary String Concatenation Operator +
Variable Increment and Decrement Operators: ++, -The Increment Operator ++
The Decrement Operator -Review Questions
Boolean Expressions
Relational Operators: =
Equality
Primitive Data Value Equality: ==, !=
Object Reference Equality: ==, !=
Object Value Equality
Boolean Logical Operators: !, ^, &, |
Operand Evaluation for Boolean Logical Operators
Boolean Logical Compound Assignment Operators: &=, ^=, |=
Conditional Operators: &&, ||
Short-Circuit Evaluation
Review Questions
The Conditional Operator: ?:
Other Operators: new, [], instanceof
Chapter Summary
Programming Exercise

6 Control Flow
6.1
6.2

6.3

Overview of Control Flow Statements
Selection Statements
The Simple if Statement
The if-else Statement
The switch Statement
Review Questions
Iteration Statements
The while Statement
The do-while Statement
The for(;;) Statement
The for(:) Statement

173
174
174
174
175
177
178
180
180
182
184
185
186
187
187
188
190
190
191
191
192
193
194
195
195
196
197
199
201
201
202
202

203
204
204
204
205
207
212
216
217
217
218
220

xiv

CONTENTS

6.4

6.5
6.6

6.7

6.8
6.9
6.10

7

Transfer Statements
Labeled Statements
The break Statement
The continue Statement
The return Statement
Review Questions
Stack-Based Execution and Exception Propagation
Exception Types
The Exception Class
The RuntimeException Class
The Error Class
Checked and Unchecked Exceptions
Defining New Exceptions
Exception Handling: try, catch, and finally
The try Block
The catch Block
The finally Block
The throw Statement
The throws Clause
Review Questions
Assertions
The assert Statement and the AssertionError Class
Compiling Assertions
Runtime Enabling and Disabling of Assertions
Using Assertions
Review Questions
Chapter Summary
Programming Exercises

Object-Oriented Programming
7.1

7.2

7.3

7.4
7.5

Single Implementation Inheritance
Inheritance Hierarchy
Relationships: is-a and has-a
The Supertype-Subtype Relationship
Overriding Methods
Instance Method Overriding
Covariant return in Overriding Methods
Overriding vs. Overloading
Hiding Members
Field Hiding
Static Method Hiding
The Object Reference super
Review Questions
Chaining Constructors Using this() and super()
The this() Constructor Call

223
223
224
226
228
229
235
239
241
241
242
243
244
245
245
246
251
255
257
260
265
265
267
269
272
276
279
279

283
284
286
286
287
288
288
290
292
294
294
294
295
297
302
302

CONTENTS

xv

7.6

7.7

7.8
7.9
7.10
7.11

7.12
7.13
7.14

The super() Constructor Call
Review Questions
Interfaces
Defining Interfaces
Abstract Method Declarations
Implementing Interfaces
Extending Interfaces
Interface References
Constants in Interfaces
Review Questions
Arrays and Subtyping
Arrays and Subtype Covariance
Array Store Check
Reference Values and Conversions
Reference Value Assignment Conversions
Method Invocation Conversions Involving References
Overloaded Method Resolution
Reference Casting and the instanceof Operator
The Cast Operator
The instanceof Operator
Review Questions
Polymorphism and Dynamic Method Lookup
Inheritance Versus Aggregation
Basic Concepts in Object-Oriented Design
Encapsulation
Cohesion
Coupling
Review Questions
Chapter Summary
Programming Exercises

8 Nested Type Declarations
8.1
8.2

8.3

8.4

8.5

Overview of Nested Type Declarations
Static Member Types
Declaring and Using Static Member Types
Accessing Members in Enclosing Context
Non-Static Member Classes
Instantiating Non-Static Member Classes
Accessing Members in Enclosing Context
Review Questions
Local Classes
Accessing Declarations in Enclosing Context
Instantiating Local Classes
Anonymous Classes
Extending an Existing Class

305
308
309
310
310
312
313
314
314
315
317
317
319
319
320
323
324
327
327
328
332
340
342
345
345
346
346
347
349
349

351
352
355
355
357
359
360
362
367
371
372
374
377
377

xvi

CONTENTS

Implementing an Interface
Instantiating Anonymous Classes
Accessing Declarations in Enclosing Context
Review Questions
Chapter Summary
Programming Exercise

9

Object Lifetime
9.1
9.2
9.3
9.4
9.5
9.6
9.7
9.8
9.9
9.10
9.11

Garbage Collection
Reachable Objects
Facilitating Garbage Collection
Object Finalization
Finalizer Chaining
Invoking Garbage Collection Programmatically
Review Questions
Initializers
Field Initializer Expressions
Static Initializer Blocks
Instance Initializer Blocks
Constructing Initial Object State
Review Questions
Chapter Summary

10 Fundamental Classes
10.1
10.2
10.3

10.4

Overview of the java.lang Package
The Object Class
Review Questions
The Wrapper Classes
Common Wrapper Class Constructors
Common Wrapper Class Utility Methods
Numeric Wrapper Classes
The Character Class
The Boolean Class
Review Questions
The String Class
Immutability
Creating and Initializing Strings
The CharSequence Interface
Reading Characters from a String
Comparing Strings
Character Case in a String
Concatenation of Strings
Searching for Characters and Substrings
Extracting Substrings
Converting Primitive Values and Objects to Strings

379
380
380
382
386
386

389
390
390
392
396
397
398
401
406
406
410
413
416
420
422

423
424
424
428
428
429
430
433
436
437
437
439
439
439
442
443
445
446
446
448
449
450

CONTENTS

xvii

10.5

Formatting Values
Pattern Matching
Review Questions
The StringBuilder and the StringBuffer Classes
Thread-Safety
Mutability
Constructing String Builders
Reading and Changing Characters in String Builders
Constructing Strings from String Builders
Appending, Inserting, and Deleting Characters in String Builders
Controlling String Builder Capacity
Review Questions
Chapter Summary
Programming Exercises

11 Files and Streams
11.1
11.2

11.3

11.4

11.5
11.6

Input and Output
The File Class
Querying the File System
File or Directory Existence
File and Directory Permissions
Listing Directory Entries
Creating New Files and Directories
Renaming Files and Directories
Deleting Files and Directories
Byte Streams: Input Streams and Output Streams
File Streams
Filter Streams
Reading and Writing Binary Values
Review Questions
Character Streams: Readers and Writers
Print Writers
Writing Text Files
Reading Text Files
Using Buffered Writers
Using Buffered Readers
The Standard Input, Output, and Error Streams
Comparison of Byte Streams and Character Streams
The Console class
Review Questions
Object Serialization
The ObjectOutputStream Class
The ObjectInputStream Class
Customizing Object Serialization
Serialization and Inheritance

450
452
452
456
456
456
457
457
458
458
460
461
464
465

467
468
468
470
472
472
473
473
474
474
475
477
479
479
484
488
490
492
494
495
496
499
500
500
506
510
511
512
517
519

xviii

CONTENTS

Review Questions
Chapter Summary
Programming Exercise

12 Localization, Pattern Matching, and Formatting
12.1
12.2
12.3

12.4

12.5

12.6

12.7

The java.util.Locale Class
The java.util.Date Class
The java.util.Calendar Class
Static Factory Methods to Create a Calendar
Interoperability with the Date Class
Selected get and set Methods
Manipulating a Calendar
Comparing Calendars
The java.text.DateFormat Class
Static Factory Methods to Create a Date/Time Formatter
Formatting Dates
Parsing Strings to Date/Time
Managing the Calendar and the Number Formatter
The java.text.NumberFormat Class
Static Factory Methods to Create a Number Formatter
Formatting Numbers and Currency
Parsing Strings to Numbers
Specifying the Number of Digits
Review Questions
String Pattern Matching Using Regular Expressions
Regular Expression Fundamentals
Escaping Metacharacters
The java.util.regex.Pattern Class
The java.util.regex.Matcher Class
The java.util.Scanner Class
Review Questions
Formatting Values
Overview
Defining Format Specifiers
Conversion Categories and Formatting Conversions
Selected Format Exceptions
Using the format() Method
Review Questions
Chapter Summary
Programming Exercises

522
529
530

531
532
535
536
537
537
537
539
540
541
541
542
543
545
546
546
546
547
547
551
554
554
561
562
566
571
582
593
593
595
597
601
602
604
610
610

13 Threads

613

13.1
13.2
13.3

614
614
615

Multitasking
Overview of Threads
The Main Thread

CONTENTS

xix

13.4

13.5

13.6

Thread Creation
Implementing the Runnable Interface
Extending the Thread Class
Review Questions
Synchronization
Locks
Synchronized Methods
Synchronized Blocks
Review Questions
Thread Transitions
Thread States
Thread Priorities
Thread Scheduler
Running and Yielding
Sleeping and Waking Up
Waiting and Notifying
Joining
Blocking for I/O
Thread Termination
Deadlocks
Review Questions
Chapter Summary
Programming Exercises

14 Generics
14.1
14.2

14.3
14.4

14.5

14.6

Introducing Generics
Generic Types and Parameterized Types
Generic Types
Parameterized Types
Generic Interfaces
Extending Generic Types
Raw Types and Unchecked Warnings
Collections and Generics
Wildcards
The Subtype Covariance Problem with Parameterized Types
Wildcard Types
Subtype Covariance: ? extends Type
Subtype Contravariance: ? super Type
Subtype Bivariance: ?
Subtype Invariance: Type
Some Restrictions on Wildcard Types
Using References of Wildcard Parameterized Types
Generic Reference Assignment
Using Parameterized References to Call Set and Get Methods
Bounded Type Parameters
Multiple Bounds

615
616
619
622
626
626
627
629
631
634
634
638
638
639
640
640
647
649
650
651
653
658
659

661
662
663
663
665
666
668
670
672
673
673
675
675
676
677
677
677
678
679
680
684
686

xx

CONTENTS

14.7
14.8

14.9
14.10

14.11
14.12

14.13

Review Questions
Implementing a Simplified Generic Stack
Generic Methods and Constructors
Generic Method Declaration
Calling Generic Methods
Wildcard Capture
Capture Conversion
Flexibility with Wildcard Parameterized Types
Nested Wildcards
Wildcard Parameterized Types as Formal Parameters
Flexible Comparisons with Wildcards
Recursive Bounds
Type Erasure
Bridge Methods
Implications for Overloading and Overriding
Method Signature
Implications for Overloading
Implications for Overriding
Limitations and Restrictions on Generic Types
Reifiable Types
Implications for instanceof operator
Implications for Casting
Implications for Arrays
Implications for Varargs
Implications for Exception Handling
Implications for Nested Classes
Other Implications
Review Questions
Chapter Summary
Programming Exercises

15 Collections and Maps
15.1

15.2

15.3

Comparing Objects
The equals() Method
The hashCode() Method
The Comparable Interface
The Comparator Interface
Review Questions
The Java Collections Framework
Core Interfaces
Implementations
Collections
Basic Operations
Bulk Operations
Iterators

686
695
697
699
700
703
705
705
705
707
709
712
714
716
716
716
717
718
722
722
723
724
726
729
730
731
733
734
744
745

747
748
751
760
765
771
775
777
778
780
784
784
785
785

CONTENTS

xxi

Array Operations
Review Questions
15.4 Sets
The HashSet and LinkedHashSet Classes
15.5 The SortedSet and NavigableSet Interfaces
The SortedSet Interface
The NavigableSet Interface
The TreeSet Class
15.6 Lists
The ArrayList, LinkedList, and Vector Classes
15.7 Queues
The Queue Interface
The PriorityQueue and LinkedList Classes
The Deque Interface
The ArrayDeque and LinkedList Class
Review Questions
15.8 Maps
Basic Operations
Bulk Operations
Collection Views
15.9 Map Implementations
The HashMap, LinkedHashMap, and Hashtable Classes
15.10 The SortedMap and NavigableMap Interfaces
The SortedMap Interface
The NavigableMap Interface
The TreeMap Class
Review Questions
15.11 Working with Collections
Ordering Elements in Lists
Searching in Collections
Changing Elements in Collections
Sorting Arrays
Searching in Arrays
Creating List Views of Arrays
Miscellaneous Utility Methods in the Arrays Class
Review Questions
Chapter Summary
Programming Exercises

A

Taking the SCJP 1.6 Exam
A.1
A.2

Preparing for the Programmer Exam
Registering for the Exam
Obtaining an Exam Voucher
Signing Up for the Test
Contact Information

790
791
796
796
800
800
801
802
804
806
809
809
810
813
815
816
821
821
822
822
823
823
826
826
827
828
833
838
838
840
841
842
843
845
846
846
849
850

851
851
852
852
852
852

xxii

CONTENTS

A.3

A.4

A.5

B

After Taking the Exam
How the Examination Is Conducted
The Testing Locations
Utilizing the Allotted Time
The Exam Program
The Questions
Types of Questions Asked
Types of Answers Expected
Topics Covered by the Questions
Moving on to Other Java Technology Exams

Objectives for the SCJP 1.6 Exam

853
853
853
853
854
854
854
855
855
856

857

C Objectives for the SCJP 1.6 Upgrade Exam

863

D Annotated Answers to Review Questions

869

E Solutions to Programming Exercises

935

F Mock Exam

959

G

Number Systems and Number Representation
G.1

G.2
G.3

G.4

Number Systems
Binary, Octal, and Hexadecimal Number System
Converting Binary Numbers to Decimals
Converting Octal and Hexadecimal Numbers to Decimals
Relationship between Binary, Octal, and Hexadecimal Numbers
Converting Decimals
Converting Decimals to Binary Numbers
Converting Decimals to Octal and Hexadecimal Numbers
Representing Integers
Calculating 2’s Complement

Index

1005
1005
1005
1006
1007
1007
1008
1008
1009
1010
1011

1013

List of Figures

1.1 UML Notation for Classes
Chapter 1 1
1.2 UML Notation for Objects
1.3 Aliases
1.4 Class Diagram Showing Static Members of a Class
1.5 Members of a Class
1.6 Class Diagram Depicting Inheritance Relationship
1.7 Class Diagram Depicting Aggregation
2.1 Primitive Data Types in Java
Chapter 2 19
3.1 The Event Model
Chapter 3 39
3.2 Array of Arrays
3.3 Parameter Passing: Primitive Data Values
3.4 Parameter Passing: Reference Values
3.5 Parameter Passing: Arrays
4.1 Java Source File Structure
Chapter 4 103
4.2 Package Hierarchy
4.3 File Hierarchy
4.4 Searching for Classes
4.5 Searching in JAR files
4.6 Block Scope
4.7 Public Accessibility
4.8 Protected Accessibility
4.9 Default Accessibility
4.10 Private Accessibility
5.1 Widening Primitive Conversions
Chapter 5 159
5.2 Overflow and Underflow in Floating-point Arithmetic
5.3 Numeric Promotion in Arithmetic Expressions
6.1 Activity 203
Chapter 6 Diagram for if Statements
6.2 Activity Diagram for a switch Statement
6.3 Activity Diagram for the while Statement
6.4 Activity Diagram for the do-while Statement
6.5 Activity Diagram for the for Statement
6.6 Enhanced for Statement
6.7 Method Execution

3
5
6
8
9
10
12
28
43
78
84
85
87
104
105
116
118
121
132
141
142
143
144
160
176
181
205
208
217
218
219
221
237

xxiii

xxiv

LIST OF FIGURES

6.8 Exception Propagation
6.9 Partial Exception Inheritance Hierarchy
6.10 The try-catch-finally Construct
6.11 Exception Handling (Scenario 1)
6.12 Exception Handling (Scenario 2)
6.13 Exception Handling (Scenario 3)
6.14 Execution of the Simple assert Statement (with Assertions Enabled)
6.15 Package Hierarchy
7.1 Inheritance Hierarchy
Chapter 7 283
7.2 Inheritance Relations
7.3 Reference Type Hierarchy: Arrays and Subtype Covariance
7.4 Type Hierarchy to Illustrate Polymorphism
7.5 Implementing Data Structures by Inheritance and Aggregation
8.1 Static Member Classes and Interfaces
Chapter 8 351
8.2 Outer Object with Associated Inner Objects
8.3 Nested Classes and Inheritance
8.4 Local Classes and Inheritance Hierarchy
9.1 Memory389
Chapter 9 Organization at Runtime
10.1 Partial10 423
Chapter Inheritance Hierarchy in the java.lang Package
10.2 Converting Values Between Primitive, Wrapper, and String Types
11.1 Partial11 467
Chapter Byte Stream Inheritance Hierarchies
11.2 Stream Chaining for Reading and Writing Binary Values to a File
11.3 Partial Character Stream Inheritance Hierarchies
11.4 Setting up a PrintWriter to Write to a File
11.5 Setting up Readers to read Characters
11.6 Buffered Writers
11.7 Buffered Readers
11.8 Keyboard and Display as Console
11.9 Object Stream Chaining
13.1 Spawning Threads Using a Runnable Object
Chapter 12 531
13 613
13.2 Spawning Threads—Extending the Thread Class
13.3 Thread States
13.4 Running and Yielding
13.5 Sleeping and Waking up
13.6 Waiting and Notifying
13.7 Thread Communication
13.8 Stack Users
13.9 Joining of Threads
13.10 Deadlock
14.1 Extending Generic Types
Chapter 14 661
14.2 No Subtype Covariance for Parameterized Types
14.4 Partial Type Hierarchy for Node).
The example below shows that only one tag is found in the target. The greedy quantifier + returns the longest possible match in the input.
Index:
Target:
Pattern:
Match:

012345678901234567890123456789012345678901234
My very greedy regex

(3,38:very greedy)

There are counterparts to the greedy quantifiers called the reluctant and the possessive quantifiers (see Table 12.13). A reluctant quantifier (also called lazy quantifier) only reads enough of the input to match the pattern. Such a quantifier will apply its regular expression as few times as possible, only expanding the match as the engine backtracks to find a match for the overall regular expression. In other words, it will return the shortest possible match.
The example below illustrates reluctantness/laziness. The pattern uses the reluctant quantifier +?, and is supposed to recognize a tag as before. The example below shows the result of applying the pattern to a target. The reluctant quantifier
+? returns the shortest possible match for each tag recognized in the input.
Index:
Target:
Pattern:
Match:

012345678901234567890123456789012345678901234567
My very reluctant regex

(3,10:very)(12,21:)(31,41:)

The result is certainly better with the reluctant quantifier. We can improve the matching by using the trick shown in this pattern: ]+>. Since the match has two enclosing angle brackets, the pattern negates the end angle bracket, creating a character class that excludes the end angle bracket. The engine can keep expanding the tag name as long as no end angle bracket is found in the input. When this bracket is found in the input, a match can be announced, without incurring the penalty of backtracking. Note that the pattern below is using the greedy quantifier +.
Index:
Target:
Pattern:
Match:

01234567890123456789012345678901234567890123456
My very powerful regex
]+>
(12,21:)(30,40:)

Lastly, there are the possessive quantifiers that always consume the entire input, and then go for one make-or-break attempt to find a match. A possessive quantifier never backtracks, even if doing so would succeed in finding a match. There are certain situations where possessive quantifiers can outperform the other types of quantifiers, but we will not pursue the subject any further in this book.

12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS
Table 12.13

561

Quantifier Classification
Greedy

Reluctant

Possessive

Matches (R is a regular expression)

R?

R??

R?+

R zero or one time, i.e., R is optional.

R*

R*?

R*+

R zero or more times.

R+

R+?

R++

R one or more times.

Escaping Metacharacters
A regular expression can be specified as a string expression in a Java program. In the declaration below, the string literal "who" contains the pattern who.
String p1 = "who";

// regex: who

The pattern \d represents a single digit character. If we are not careful in how we specify this pattern in a string literal, we run into trouble.
String p2 = "\d";

// Java compiler: Invalid escape sequence!

The escape sequence \d is invalid in the string literal above. Both string literals and regular expressions use a backslash (\) to escape metacharacters. For every backslash in a regular expression, we need to escape it in the string literal, i.e. specify it as a backslash pair (\\). This ensures that the Java compiler accepts the string literal, and the string will contain only one backslash for every backslash pair that is specified in the string literal. A backslash contained in the string is thus interpreted correctly as a backslash in the regular expression.
String p3 = "\\d";
String p4 = "\\.";
String p5 = ".";

// regex: \d
// regex: \.
// regex: .

(i.e. the . non-metacharacter)
(i.e. the . metacharacter)

If we want to use a backslash as a non-metacharacter in a regular expression, we have to escape the backslash (\), i.e use the pattern \\. In order to escape these two backslashes in a string literal, we need to specify two consecutive backslash pairs
(\\\\). Each backslash pair becomes a single backslash inside the string, resulting in the two pairs becoming a single backslash pair, which is interpreted correctly in the regular expression, as the two backslash characters represent a backslash nonmetacharacter.
String nonMetaBackslash = "\\\\";

// regex: \\ (i.e. the \ non-metacharacter)

Below are examples of string literals for some of the regular expressions we have seen earlier. Each backslash in the regular expression is escaped in the string literal.
String p6 = "\\d\\d-\\d\\d-\\d\\d";
String p7 = "\\d+\\.\\d+";
String p8 = "(^[a-z])|(\\?$)";

// regex: \d\d-\d\d-\d\d
// regex: \d+\.\d+
// regex: (^[a-z])|(\?$)

562

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING

The java.util.regex.Pattern Class
The two classes Pattern and Matcher in the java.util.regex package embody the paradigm for working efficiently with regular expressions in Java. It consists of the following steps:
1.

Compiling the regular expression string into a Pattern object which constitutes the compiled representation of the regular expression (i.e., a pattern) mentioned earlier:
Pattern pattern = Pattern.compile(regexStr);

2.

Using the Pattern object to obtain a Matcher (i.e., an engine) for applying the pattern to a specified input of type java.lang.CharSequence:
Matcher matcher = pattern.matcher(input);

3.

Using the operations of the matcher to apply the pattern to the input: boolean eureka = matcher.matches();

The approach outlined above is recommended, as it avoids compiling the regular expression string repeatedly, and it is specially optimized for using the same pattern multiple times on the same input or different inputs. When used on the same input repeatedly, the pattern can be used to find multiple matches.
As mentioned above, the input must be of type CharSequence, which is a readable sequence of char values. The interface CharSequence is implemented by such classes as String and StringBuilder.
With the setup outlined above, it is possible to use the same pattern with different engines. The bookkeeping for doing the actual pattern matching against some input is localized in the matcher, not in the pattern.

Compiling a Pattern
The two methods below can be used to compile a regular expression string into a pattern and to retrieve the regular expression string from the pattern, respectively.
String regexStr = "\\d\\d-\\d\\d-\\d\\d"; // regex: \d\d-\d\d-\d\d
Pattern datePattern = Pattern.compile(regexStr);

static Pattern compile(String regexStr)

Compiles the specified regular expression string into a pattern. Throws the unchecked PatternSyntaxException if the regular expression is invalid. When the source is line-oriented, it is recommended to use the overloaded compile() method that additionally takes the argument Pattern.MULTILINE.
String pattern()

Returns the regular expression string from which this pattern was compiled.

12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS

563

Creating a Matcher
The matcher() method returns a Matcher, which is the engine that does the actual pattern matching. This method does not apply the underlying pattern to the specified input. The matcher provides special operations to actually do the pattern matching.
Matcher dateMatcher = datePattern.matcher("01-03-49 786 09-09-09");

The Pattern class also provides a static convenience method that executes all the steps outlined above for pattern matching. The regular expression string and the input are passed to the static method matches(), which does the pattern matching on the entire input. The regular expression string is compiled and the matcher is created each time the method is called. Calling the matches() method is not recommended if the pattern is to be used multiple times. boolean dateFound = Pattern.matches("\\d\\d-\\d\\d-\\d\\d", "01-03-49"); // true

Matcher matcher(CharSequence input)

Creates a matcher that will match the specified input against this pattern. static boolean matches(String regexStr, CharSequence input)

Compiles the specified regular expression string and attempts to match the specified input against it. The method only returns true if the entire input matches the pattern.

Splitting
The normal mode of pattern matching is to find matches for the pattern in the input.
In other words, the result of pattern matching is the sequences of characters (i.e., the matches, also called groups) that match the pattern. Splitting returns sequences of characters that do not match the pattern. In other words, the matches are spliced out and the sequences of non-matching characters thus formed from the input are returned in an array of type String. The pattern is used as a delimiter to tokenize the input. The token in this case is a sequence of non-matching characters, possibly the empty string. The classes StringTokenizer and Scanner in the java.util package also provide the functionality for tokenizing text-based input. See the subsection The java.util.Scanner Class, p. 571.
The example below shows the results from splitting an input on a given pattern.
The input is a ‘|’-separated list of names. The regular expression string is "\\|", where the metacharacter | is escaped in order to use it as a non-metacharacter.
Splitting the given input according to the specified regular expression, results in the array of String shown below.
Input: "tom|dick|harry"
Split: "\\|"
Results: { "tom", "dick", "harry" }

The split() method can be called on a pattern to create an array by splitting the input according to the pattern. Each successful application of the pattern, meaning each match of the pattern delimiter in the input, results in a split of the input, with

564

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING

the non-matched characters before the match resulting in a new element in the array, and any remaining input being returned as the last element of the array.
String[] split(CharSequence input, int limit)

Splits the specified input around matches of this pattern. The limit determines how many times this pattern will be applied to the input to create the array.
The number of applications of the pattern is controlled by the limit value passed to the method, as explained in Table 12.14. The code below will result in the array shown earlier:
String input = "tom|dick|harry";
String splitRegex = "\\|";
// regex: \|
Pattern splitPattern = Pattern.compile(splitRegex);
String[] results = splitPattern.split(input, 4); // { "tom", "dick", "harry" }
Table 12.14

Implications of the Limit Value in the split() Method
Limit n

No. of Applications

Array Length

Other Remarks

n > 0

At the most n-1 times, meaning it can also be fewer if the input was exhausted No greater than n, meaning it can also be smaller if the input was exhausted Any remaining input is returned in the last element of the array

n == 0

As many times as possible to split the entire input

Any length required to split the entire input

Trailing empty strings are discarded

n < 0

As many times as possible to split the entire input

Any length required to split the entire input

Trailing empty strings are not discarded

Using the split() method is illustrated in Example 12.7. The doPatSplits() method at (1) creates a Pattern at (2) and calls the split() method at (3) on this pattern. Partial output from Example 12.7 is shown below. Limit value 1 does not split the input, limit value 2 splits the input once, and so on. Limit value greater than 3 does not change the results, as the input is exhausted at limit value 3. A non-positive limit value splits the input on the pattern as many times as necessary, until the input is exhausted.
Input: tom|dick|harry
Split: \|
Limit Length Results
3
3
{ "tom", "dick", "harry" }
2
2
{ "tom", "dick|harry" }
1
1
{ "tom|dick|harry" }
0
3
{ "tom", "dick", "harry" }
-1
3
{ "tom", "dick", "harry" }

565

12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS

If we change the input above to the input shown below, we see how empty strings come into the picture. The empty string is returned as a token to “mark” the split when the delimiter is found at the head of any remaining input, or at the end of the input. Five applications of the pattern were necessary to exhaust the input. Note that the limit value 0 does not return trailing empty strings.
Input: |tom||dick|harry|
Split: \|
Limit Length Results
6
6
{ "", "tom", "", "dick", "harry", "" }
5
5
{ "", "tom", "", "dick", "harry|" }
4
4
{ "", "tom", "", "dick|harry|" }
3
3
{ "", "tom", "|dick|harry|" }
2
2
{ "", "tom||dick|harry|" }
1
1
{ "|tom||dick|harry|" }
0
5
{ "", "tom", "", "dick", "harry" }
-1
6
{ "", "tom", "", "dick", "harry", "" }

Example 12.7 Splitting import java.util.regex.Pattern; public class Splitting { public static void main(String[] args) {
System.out.println("===Using the Pattern.split() method==="); doPatSplits("tom|dick|harry", "\\|", -1, 3); doPatSplits("|tom||dick|harry|", "\\|", -1, 6);
System.out.println("===Using the String.split() method==="); doStrSplits("tom|dick|harry", "\\|", -1, 3);
}
public static void doPatSplits(String input, String splitRegex, int lowerLimit, int upperLimit) {
System.out.print("Input: " + input);
System.out.println("
Split: " + splitRegex);
System.out.println("Limit Length Results");
Pattern splitPattern = Pattern.compile(splitRegex); for (int limit = upperLimit; limit >= lowerLimit; limit--) {
String[] results = splitPattern.split(input, limit);
System.out.printf("%3d%6d
", limit, results.length); printCharSeqArray(results); }
}
public static void doStrSplits(String input, String splitRegex, int lowerLimit, int upperLimit) {
System.out.print("Input: " + input);
System.out.println("
Split: " + splitRegex);
System.out.println("Limit Length Results"); for (int limit = upperLimit; limit >= lowerLimit; limit--) {
String[] results = input.split(splitRegex, limit);
System.out.printf("%3d%6d
", limit, results.length);

// (1)

// (2)
// (3)

// (4)

// (5)

566

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING printCharSeqArray(results); }
}
static void printCharSeqArray(CharSequence[] array) {
System.out.print("{ "); for (int i = 0; i < array.length; i++) {
System.out.print("\"" + array[i] + "\"");
System.out.print((i != array.length -1) ? ", " : " ");
}
System.out.println("}");
}

// (6)

}

The String class also has a split() method that takes the regular expression string and the limit as parameters. Given that the reference input is of type String, the call input.split(regexStr,limit) is equivalent to the call Pattern.compile(regexStr). split(input, limit). The doStrSplits() method at (4) in Example 12.7 uses the split() method in the String class. Here is another example of using the split() method from the String class:
String[] results = "tom|dick|harry".split("\\|", 0); // { "tom", "dick", "harry" }

We will not split hairs here any more, but encourage experimenting with splitting various input on different patterns using the code in Example 12.7.

The java.util.regex.Matcher Class
A Matcher is an engine that performs match operations on a character sequence by interpreting a Pattern. A matcher is created from a pattern by invoking the Pattern. matcher() method. Here we will explore the following three modes of operation for a matcher:
1.

One-Shot Matching: Using the matches() method in the Matcher class to match the entire input sequence against the pattern.
Pattern pattern = Pattern.compile("\\d\\d-\\d\\d-\\d\\d");
Matcher matcher = pattern.matcher("01-03-49"); boolean isMatch = matcher.matches();
// true matcher = pattern.matcher("1-3-49"); isMatch = matcher.matches();

// false

The convenience method matches() in the Pattern class in the last subsection calls the matches() method in the Matcher class implicitly. boolean matches()

Attempts to match the entire input sequence against the pattern. The method returns true only if the entire input matches the pattern.

567

12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS

2.

Successive Matching: Using the find() method in the Matcher class to successively apply the pattern on the input sequence to look for the next match (discussed further in this subsection).

3.

Match-and-Replace Mode: Using the matcher to find matches in the input sequence and replace them (discussed further in this subsection).

Successive Matching
The main steps of successive matching using a matcher are somewhat analogous to using an iterator to traverse a collection (p. 786). These steps are embodied in the code below, which is extracted from Example 12.8.
...
Pattern pattern = Pattern.compile(regexStr);
Matcher matcher = pattern.matcher(target); while(matcher.find()) {
...
String matchedStr = matcher.group();
...
}
...

// (2)
// (3)
// (4)
// (7)

Once a matcher has been obtained, the find() method of the Matcher class can be used to find the next match in the input (called target in the code). The find() returns true if a match was found.
If the previous call to the find() method returned true, and the matcher has not been reset since then, the next call to the find() method will advance the search in the target for the next match from the first character not matched by the previous match. If the previous call to the find() returned false, no match was found, and the entire input has been exhausted.
The call to the find() method is usually made in a loop condition, so that any match found can be dealt with successively in the body of the loop. A match found by the find() method is called the previous match (as opposed to the next match which is yet to be found). The characters comprising the previous match are called a group, and can be retrieved by calling the group() method of the Matcher class. The group’s location in the target can be retrieved by calling the start() and the end() methods of the Matcher class, as explained below. The two methods find() and group() are called successively in lockstep to find all matches/groups in the input.
Once pattern matching has commenced, the matcher can be reset. Its target and pattern can also be changed by passing the new target to the reset() method and by passing the new pattern to the usePattern() method, respectively. The reset() method resets the search to start from the beginning of the input, but the usePattern() method does not.

568

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING

boolean find()

Attempts to find the next match in the input that matches the pattern. The first call to this method, or a call to this method after the matcher is reset, always starts the search for a match at the beginning of the input.
String group()

Returns the characters (substring) in the input that comprise the previous match. int start() int end()

The first method returns the start index of the previous match. The second method returns the index of the last character matched, plus one. The values returned by these two methods define a substring in the input.
Matcher reset()
Matcher reset(CharSequence input)

The method resets this matcher, so that the next call to the find() method will begin the search for a match from the start of the current input. The second method resets this matcher, so that the next call to the find() method will begin the search for a match from the start of the new input.
Matcher usePattern(Pattern newPattern)

Replaces the pattern used by this matcher with another pattern. This change does not affect the search position in the input.
Pattern pattern()

Returns the pattern that is interpreted by this matcher.
Example 12.8 is a complete program that illustrates successive matching. In fact, the program in Example 12.8 was used to generate all examples of regular expressions in the subsection Regular Expression Fundamentals, p. 554. Again, we recommend experimenting with successive matching on various inputs and patterns to better understand regular expressions.
Example 12.8 String Pattern Matching import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchMaker { public static void main(String[] args) {
// All examples from the subsection "Regular Expression Fundamentals". matchMaker("o", "All good things come to those who wait"); matchMaker("who", "Interrogation with who, whose and whom."); matchMaker("[^aeiouAEIOU]", "I said I am."); matchMaker("[-A-Z]", "I-love-REGEX.");

569

12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS

matchMaker(".[Hh]o",
"Who is who? Whose is it? To whom it may concern. How are you?"); matchMaker("\\d\\d-\\d\\d-\\d\\d", "01-03-49 786 09-09-09"); matchMaker("\\?$", "Who is who? Who me? Who else?"); matchMaker("([Jj][aA][vV][aA])|([Cc]\\+\\+)", "JaVA jAvA C++ jAv c+++1 javan C+"); matchMaker("a?", "banana"); matchMaker("\\d\\d?-\\d\\d?-\\d\\d?", "01-3-49 23-12 9-09-09 01-01-2010"); matchMaker("a*", "baananaa"); matchMaker("(0|[1-9]\\d*)\\.\\d\\d", ".50 1.50 0.50 10.50 00.50 1.555"); matchMaker("a+", "baananaa"); matchMaker("\\d+\\.\\d+", ".50 1.50 0. 10.50 00.50 1.555"); matchMaker("", "My very greedy regex"); matchMaker("", "My very reluctant regex"); matchMaker("]+>", "My very powerful regex");
// Some more regular expression examples. matchMaker("(^[a-z])|(\\?$)", "who is who? Who me? Who else?"); matchMaker("[\\\\-^$.?*+()|]", "\\-^$.?*+()|"); matchMaker("[-+]?[0-9]+", "+123 -34 567 2.3435"); matchMaker("[a-zA-Z][a-zA-Z0-9]+", "+a123 -X34 567 m2.3mm435"); matchMaker("[^,]+", "+a123, -X34, 567, m2,3mm435"); matchMaker("\\\\", "book\\\\chapter\\section\\"); matchMaker("[^\\\\]+", "book\\\\chapter\\section\\");
}
public static void matchMaker(String regexStr, String target) {
//
System.out.print("Index:
");
for (int i = 0; i < target.length(); i++) {
System.out.print(i%10);
}
System.out.println();
System.out.println("Target: " + target);
System.out.println("Pattern: " + regexStr);
System.out.print( "Match:
");
Pattern pattern = Pattern.compile(regexStr);
//
Matcher matcher = pattern.matcher(target);
//
while(matcher.find()) {
//
int startCharIndex = matcher.start();
//
int lastPlus1Index = matcher.end();
//
int lastCharIndex = startCharIndex == lastPlus1Index ? lastPlus1Index : lastPlus1Index-1;
String matchedStr = matcher.group();
//
System.out.print("(" + startCharIndex + "," + lastCharIndex + ":" + matchedStr + ")");
}
System.out.println();
}
}

Output from the program:
...
Index:
Target:
Pattern:
Match:

0123456789012345678901 book\\chapter\section\ [^\\]+
(0,3:book)(6,12:chapter)(14,20:section)

(1)

(2)
(3)
(4)
(5)
(6)

(7)

570

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING

Match-and-Replace Mode
In this mode, the matcher allows the matched characters in the input to be replaced with new ones. Details of the methods used for this purpose are given below. The find() and the appendReplacement() methods comprise the match-andreplace loop, with the appendReplacement() method completing the operation when the loop finishes.
Note that these methods use a StringBuffer, and have not been updated to work with a StringBuilder.
Matcher appendReplacement(StringBuffer sb, String replacement)

Implements a non-terminal append-and-replace step, i.e., it successively adds the non-matched characters in the input, followed by the replacement of the match, to the string buffer.
The find() method and the appendReplacement() method are used in lockstep to successively replace all matches, and the appendTail() method is called as the last step to complete the match-and-replace operation.
StringBuffer appendTail(StringBuffer sb)

Implements a terminal append-and-replace step, i.e., it copies the remaining characters from the input to the string buffer, which is then returned. It should be called after appendReplacement() operations have completed.
String replaceAll(String replacement)

Replaces every subsequence of the input that matches the pattern with the specified replacement string. The method resets the matcher first and returns the result after the replacement.
String replaceFirst(String replacement)

Replaces the first subsequence of the input that matches the pattern with the specified replacement string. The method resets the matcher first and returns the result after the replacement.
Example 12.9 illustrates the match-and-replace loop. Non-matching characters in the input and the replacements of the matches are successively added to the string buffer in the loop at (1), with the call to the appendTail() method at (3) completing the operation. The same operation is repeated using the replaceAll() method at (4).
Using the replaceAll() method replaces all matches with the same replacement, but the match-and-replace loop offers greater flexibility in this regard, as each replacement can be tailored when a match is found.

571

12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS

Example 12.9 Match and Replace import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchAndReplace { public static void main(String[] args) {
// Match and replace loop:
Pattern pattern = Pattern.compile("be");
String input = "What will be will be.";
System.out.println(input);
Matcher matcher = pattern.matcher(input);
StringBuffer strBuf = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(strBuf, "happen");
}
matcher.appendTail(strBuf);
System.out.println(strBuf);
// Match and replace all: matcher.reset(); String result = matcher.replaceAll("happen");
System.out.println(result);

// (1)
// (2)
// (3)

// (4)

}
}

Output from the program:
What will be will be.
What will happen will happen.
What will happen will happen.

The java.util.Scanner Class
A scanner reads characters from a source and converts them into tokens. The source is usually a text-based input stream containing formatted data. The formatted values in the source are separated by delimiters, usually whitespace. A token is a sequence of characters in the source that comprises a formatted value. A scanner generally uses regular expressions to recognize tokens in the source input. A point to note is that a scanner can also use regular expressions to recognize delimiters, which are normally discarded. Such a scanner is also called a tokenizer (also called a lexical analyzer), and the process is called tokenization. Some scanners also convert the tokens into values of appropriate types for further processing. Scanners with this additional functionality are usually called parsers.
The class Scanner in the java.util package provides powerful tools to implement text scanners which use regular expressions to tokenize and parse formatted data into primitive types and strings. The Pattern.split() method (and the
String.split() method that uses this method) also provide tokenizing capabilities
(p. 563), but these are not as versatile as the Scanner class.

572

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING

We will discuss two modes of operation for a scanner:
• Tokenizing Mode, for tokenizing a stream of formatted data values.
• Multi-Line Mode, for searching or finding matches in line-oriented input.

Basics of Tokenizing
Tokenizing is analogous to successive matching (p. 567), involving reading of the characters in the source, and recognizing tokens in the source. Example 12.10 shows a bare-bones tokenizer that tokenizes a string, but it embodies the paradigm for constructing more sophisticated scanners. The rest of the subsection will present variations on this tokenizer.
1.

The source for the input to the scanner must be identified. The example shows a String as the source, but we can also use other sources, such as a File, an InputStream or a BufferedReader.

2.

A scanner is created and associated with the source that is passed as argument in the constructor call. The Scanner class provides constructors for various kinds of sources.

3.

The bulk of the work of a scanner is done in a lookahead-and-parse loop.
The condition of the loop is a call to a lookahead method to see if an appropriate token can be identified in the remaining source. The Scanner class provides lookahead methods named hasNextType to determine whether the next token in the source is of the appropriate primitive Type. Note that the scanner reads the characters from the source sequentially.
The call to the hasNext() method at (3) returns true if there is a (String) token in the source. The loop terminates when the hasNext() method returns false, meaning that the source string has been exhausted, i.e., there are no more tokens. Each successive call to a lookahead method causes the scanner to advance and look for the next token in the source.

4.

If a lookahead method determines that there is a token in the source, the token can be parsed in the loop body. The Scanner class provides parse methods named nextType to parse the next token in the source to the appropriate primitive type. The call to the next() method at (4) parses the next token to a
String. In the example, the parsed value is printed, but it can be stored and used as desired. Also in the example, the scanner uses the default delimiters
(whitespace) to tokenize the string.
A lookahead method and its corresponding parse method are used in lockstep to ensure that characters in the source are matched and parsed to a token of an appropriate type.

5.

A scanner whose source has been explicitly associated in the code, should be closed when it is no longer needed. This also closes the source, if that is necessary. 12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS

573

Example 12.10 Tokenizing Mode import static java.lang.System.out; import java.util.Scanner; class BareBonesTokenizer { public static void main(String[] args) {
String input = "The world will end today -- not!";// (1) String as source
Scanner lexer = new Scanner(input);
// (2) Create a scanner while (lexer.hasNext()) {
// (3) Processing loop out.println(lexer.next()); // (4) Parsing
}
lexer.close();
// (5) Close the scanner
}
}

Output from the program:
The
world will end today -not!

Constructing a Scanner
A scanner must be constructed and associated with a source before it can be used to parse text-based data. The source of a scanner is passed as an argument in the appropriate constructor call. Once a source is associated with a scanner it cannot be changed. If the source is a byte stream (e.g., an InputStream), the bytes are converted to characters using the default character encoding. A character encoding can also be specified as an additional argument in an overloaded constructor, except when the source is a String.
Scanner(SourceType source)

Returns an appropriate scanner. SourceType can be a String, a File, an InputStream, a ReadableByteChannel, or a Readable (implemented by various Readers).

Lookahead Methods
The Scanner class provides two overloaded hasNext() methods that accept a regular expression specified as a string expression or as a Pattern, respectively. The next token is matched against this pattern. All primitive types and string literals have a pre-defined format which is used by the appropriate lookahead method.
All lookahead methods return true if the match with the next token is successful.
This means that we can safely call the corresponding parse method to parse the

574

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING

token to an appropriate type. Note that a lookahead method does not advance past any input character, regardless of whether the lookahead was successful. It only determines whether appropriate input is available at the current position in the input. boolean hasNext() boolean hasNext(Pattern pattern) boolean hasNext(String pattern)

The first method returns true if this scanner has another (string) token in its input. The last two methods return true if the next token matches the specified pattern or the pattern constructed from the specified string, respectively. boolean hasNextIntegralType() boolean hasNextIntegralType(int radix)

Returns true if the next token in this scanner’s input can be interpreted as a value of the integral type corresponding to IntegralType in the default or specified radix. The name IntegralType can be Byte, Short, Int or Long, corresponding to the primitive types byte, short, int, or long, respectively. boolean hasNextFPType()

Returns true if the next token in this scanner’s input can be interpreted as a value of the floating-point type corresponding to FPType. The name FPType can be Float or Double, corresponding to the types float or double, respectively. boolean hasNextBoolean()

Returns true if the next token in this scanner’s input can be interpreted as a boolean value using a case insensitive pattern created from the string
"true|false".
boolean hasNextLine()

Returns true if there is another line in the input of this scanner.
A scanner uses white space as its default delimiter pattern to identify tokens. The useDelimiters() method of the Scanner class can be used to set a different delimiter pattern for the scanner during parsing. Note that a scanner uses regular expressions for two purposes: a delimiter pattern to identify delimiter characters and a token pattern to find a token in the input.
A scanner is able to read and parse any value that has been formatted by a printf method, provided the same locale is used. The useLocale() method of the Scanner class can be used to change the locale used by a scanner.
The delimiters, the locale, and the radix can be changed any time during the tokenizing process.

12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS

575

Pattern delimiter()
Scanner useDelimiter(Pattern pattern)
Scanner useDelimiter(String pattern)

The first method returns the pattern this scanner is currently using to match delimiters. The last two methods set its delimiting pattern to the specified pattern or to the pattern constructed from the specified pattern string, respectively.
Locale locale()
Scanner useLocale(Locale locale)

These methods return this scanner’s locale or set its locale to the specified locale, respectively. int radix()
Scanner useRadix(int radix)

These methods return this scanner’s default radix or set its radix to the specified radix, respectively.

Parsing the Next Token
The Scanner class provides methods to parse strings and values of all primitive types, except the char type.
Corresponding to the hasNext() methods, the Scanner class provides two overloaded next() methods that accept a regular expression as a string expression or as a Pattern, respectively. This pattern is used to find the next token.
It is important to understand how a parse method works. A call to a parse method first skips over any delimiters at the current position in the source, and then reads characters up to the next delimiter. The scanner attempts to match the non-delimiter characters that have been read against the pattern associated with the parse method. If the match succeeds, a token has been found, which can be parsed accordingly. The current position is advanced to the new delimiter character after the token. The upshot of this behavior is that if a parse method is not called when a lookahead method reports there is a token, the scanner will not advance in the input. In other words, tokenizing will not proceed unless the next token is
“cleared.”
A scanner will throw an InputMismatchException when it cannot parse the input, and the current position will remain unchanged.
String next()
String next(Pattern pattern)
String next(String pattern)

The first method scans and returns the next token as a String. The last two methods return the next string in the input that matches the specified pattern or the pattern constructed from the specified string, respectively.

576

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING
ReturnIntegralType nextIntegralType()
ReturnIntegralType nextIntegralType(int radix)

Returns the next token in the input as a value of primitive type corresponding to IntegralType. The name IntegralType can be Byte, Short, Int, or Long, corresponding to the primitive types byte, short, int, or long, respectively. The name
ReturnIntegralType is the primitive type corresponding to the name IntegralType.
ReturnFPType nextFPType()

Returns the next token in the input as a value of the primitive type corresponding to FPType. The name FPType can be Float or Double, corresponding to the primitive types float or double, respectively. The name ReturnFPType is the primitive type corresponding to the name FPType. boolean nextBoolean()

Returns the next token in the input as a boolean value.
String nextLine()

Advances this scanner past the current line and returns the input that was skipped. Parsing Primitive Values
Example 12.11 illustrates parsing of primitive values (and strings) from formatted data. To parse such values, we need to know what type of values occur in what order in the input so that an appropriate lookahead and a corresponding parse method can be used. We also need to know what locale was used to format them and which delimiters separate the individual values in the input. This is exactly the information accepted by the parse() method at (1).
The order in which the different type of values occur in the input is specified by the vararg parameter tokenTypes, whose element type is the enum type TokenType. A call to the method parse(), such as the one shown below, thus indicates the order, the type and the number of values to expect in the input. parse(locale, input, delimiters,
TokenType.INT, TokenType.DOUBLE, TokenType.BOOL,
TokenType.INT, TokenType.LONG,
TokenType.STR);

Example 12.11 can be used as the basis for experimenting with a scanner for parsing primitive values in the formatted input.
Example 12.11 Parsing Primitive Values and Strings import static java.lang.System.out; import java.util.Locale; import java.util.Scanner;

12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS

577

public class ParsingPrimitiveValues {
/** Types of tokens to parse */ enum TokenType { INT, LONG, FLOAT, DOUBLE, BOOL, STR } public static void main(String[] args) {
// Using default delimiters (i.e. whitespace).
// Note Norwegian locale format for floating-point numbers.
String input = "123 45,56 false 567 722 blahblah";
String delimiters = "default";
Locale locale = new Locale("no", "NO"); parse(locale, input, delimiters,
TokenType.INT, TokenType.DOUBLE, TokenType.BOOL,
TokenType.INT, TokenType.LONG,
TokenType.STR);
// Note the use of backslash to escape characters in regex. input = "2008 | 2 | true"; delimiters = "\\s*\\|\\s*"; parse(null, input, delimiters,
TokenType.INT, TokenType.INT, TokenType.BOOL);
// Another example of a regex to specify delimiters. input = "Always = true | 2 $ U"; delimiters = "\\s*(\\||\\$|=)\\s*"; parse(null, input, delimiters,
TokenType.STR, TokenType.BOOL, TokenType.INT, TokenType.STR);
}
/**
* Parses the input using the locale, the delimiters and
* expected sequence of tokens.
*/
public static void parse(Locale locale, String input, String delimiters,
TokenType... tokenTypes) { // (1) Vararg
Scanner lexer = new Scanner(input);
// (2) Create a scanner. if (!delimiters.equalsIgnoreCase("default")) { // (3) Change delimiters? lexer.useDelimiter(delimiters); } if (locale != null) {
// (4) Change locale? lexer.useLocale(locale); } out.println("Locale: " + lexer.locale()); out.println("Delim: " + delimiters); out.println("Input: " + input); out.print("Tokens: ");
// (5) Iterate through the tokens: for (TokenType tType : tokenTypes) { if (!lexer.hasNext()) break;
// (6) Handle premature end of input. switch(tType) { case INT: out.print(""); break; case LONG: out.print(""); break; case FLOAT: out.print(""); break; case DOUBLE: out.print(""); break;

578

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING case BOOL: case STR: default: out.print(""); break; out.print(""); break; assert false;

}
}
System.out.println("\n"); lexer.close(); // (7) Close the scanner.

}
}

Output from the program:
Locale:
Delim:
Input:
Tokens:

no_NO default 123 45,56 false 567 722 blahblah

Locale:
Delim:
Input:
Tokens:

en_GB
\s*\|\s*
2008 | 2 | true

Locale:
Delim:
Input:
Tokens:

en_GB
\s*(\||\$|=)\s*
Always = true | 2 $ U

Miscellaneous Scanner Methods
The skip() method can be used to skip characters in the input during the course of tokenizing. This operation ignores delimiters and will only skip input that matches the specified pattern. If no such input is found, it throws a NoSuchElementException.
The match() method can be called after the value of a token has been returned. The
MatchResult interface provides methods to retrieve the start index, the end index, and the group of the token. For example, after parsing a floating-point number in the input, a MatchResult can be obtained by calling the match() method, which can then be queried about the location of the characters comprising the value, etc.
When the source is an input stream (e.g., a File, an InputStream, a Reader), a read operation can throw an IOException. If this exception is thrown in the course of a lookahead method call, it is not propagated by the scanner and the scanner assumes that the end of the input has been reached. Subsequent calls to a lookahead method will throw a NoSuchElementException. To determine whether processing terminated because of a genuine end of input or an IOException, the ioException() method can be called to verify the situation.
Closing a scanner is recommended when the client code has explicit control over the assigning of the source. Calling a scanning method on a closed scanner results in an IllegalStateException.

12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS

579

Scanner skip(Pattern pattern)
Scanner skip(String pattern)

These methods skip input that matches the specified pattern or the pattern constructed from the specified string, respectively, ignoring any delimiters. If no match is found at the current position, no input is skipped and a NoSuchElementException is thrown.
MatchResult match()

Returns the match result of the last scanning operation performed by this scanner. IOException ioException()

Returns the IOException last thrown by this scanner’s underlying Readable object. Scanner reset()

Resets this scanner to the default state with regard to delimiters, locale, and radix. void close()

Closes this scanner. When a scanner is closed, it will close its input source if the source implements the Closeable interface (implemented by various Channels,
InputStreams, Readers).

Using Delimiters and Patterns with a Scanner
Example 12.12 is the analog of Example 12.8 that uses a scanner instead of a matcher. The thing to note about Example 12.12 is the loop at (4). The method call hasNext() looks ahead to see if there is any input. The method call hasNext(pattern) attempts to match the pattern with the next token (found using the delimiters). If the attempt is not successful, the method call next() returns the token, which is ignored, but advances the scanner.
The split() method (p. 563) in the Pattern and the String classes can also tokenize, but is not as versatile as a scanner. The example below shows the difference between a scanner and a matcher. In the tokenizing mode, a scanner tokenizes and then attempts to match the token with the pattern. A matcher does not tokenize, but searches for the pattern in the input. The results below make this difference quite apparent in the number of matches the two approaches find in the same input. Results from a scanner (see Example 12.12):
Index:
Target:
Delimit:
Pattern:
Match:

01234567890123456789012345678901
JaVA jAvA C++ jAv c+++1 javan C+ default ([Jj][aA][vV][aA])|([Cc]\+\+)
(0,3:JaVA)(5,8:jAvA)(10,12:C++)

580

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING

Results from a matcher (see Example 12.8):
Index:
Target:
Pattern:
Match:

01234567890123456789012345678901
JaVA jAvA C++ jAv c+++1 javan C+
([Jj][aA][vV][aA])|([Cc]\+\+)
(0,3:JaVA)(5,8:jAvA)(10,12:C++)(18,20:c++)(24,27:java)

Example 12.12 Using Delimiters and Patterns with a Scanner import static java.lang.System.out; import java.util.Scanner; import java.util.regex.MatchResult; import java.util.regex.Pattern; public class Tokenizer { public static void main(String[] args) { tokenize("([Jj][aA][vV][aA])|([Cc]\\+\\+)", "JaVA jAvA C++ jAv c+++1 javan C+", "default"); tokenize("[a-z[A-Z]]+", "C:\\Program Files\\3MM\\MSN2Lite\\Help", "\\\\");
}
public static void tokenize(String regexStr, String source,
String delimiters) {
// (1)
System.out.print("Index:
"); for (int i = 0; i < source.length(); i++) {
System.out.print(i%10);
}
System.out.println();
System.out.println("Target: " + source);
System.out.println("Delimit: " + delimiters);
System.out.println("Pattern: " + regexStr);
System.out.print( "Match:
");
Pattern pattern = Pattern.compile(regexStr);
// (2)
Scanner lexer = new Scanner(source);
// (3) if (!delimiters.equalsIgnoreCase("default")) lexer.useDelimiter(delimiters); // (5) while(lexer.hasNext()) {
// (4) if (lexer.hasNext(pattern)) {
// (5)
String matchedStr = lexer.next(pattern);
// (5)
MatchResult matchResult = lexer.match();
// (6) int startCharIndex = matchResult.start(); int lastPlus1Index = matchResult.end(); int lastCharIndex = startCharIndex == lastPlus1Index ? lastPlus1Index : lastPlus1Index-1; out.print("(" + startCharIndex + "," + lastCharIndex + ":" + matchedStr + ")");
} else { lexer.next(); // (7)
}
}
System.out.println();
}
}

12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS

581

Output from the program:
...
Index:
Target:
Delimit:
Pattern:
Match:

0123456789012345678901234567890123
C:\Program Files\3MM\MSN2Lite\Help
\\
[a-z[A-Z]]+
(30,33:Help)

Multi-Line Mode
If the input is line-oriented, the scanner can be used to perform search in the input one line at a time. The methods hasNextLine(), findInLine(), and nextLine() form the trinity that implements the multi-line mode of searching the input with a pattern.
Example 12.13 illustrates the basics of the multi-line use of a scanner. The program processes a text file line by line, printing the names found in each line. The program essentially comprises two nested loops: an outer loop to access the input one line at a time, and an inner loop to search for all names in this line. The name of the source file is specified as a program argument at (1). The pattern that defines a name is specified at (2), and a scanner with the text file as the source is created at (3).
The call to the lookahead method hasNextLine() at (4) checks to see if there is another line of input. If that is the case, the findInLine() method is called at (5) to find the first match. All characters in the line are treated as being significant by the findInLine() method, including the delimiters that are set for the scanner. If a call to the findInLine() method results in a match, it advances the search past the matched input in the line and returns the matched input. The value returned by the findInLine() method can be used as a condition in an inner loop to successively find remaining occurrences of the pattern in the line at (8).
If no match is found in the line, the findInLine() method returns the value null and the search position remains unchanged. The findInLine() method never reads past a line separator. This means that the scanner does not budge if a match cannot be found in the line or if it has reached the end of the line. A call to the nextLine() method at (9) reads the rest of the line, i.e., any characters from the current position to the end of the line, including the line separator. The scanner is thus poised to process the next line.
Since the findInLine() method only recognizes the line separator as a delimiter, absence of line separators in the input may result in buffering of the input while the scanner tries to match the search pattern.
String findInLine(Pattern pattern)
String findInLine(String pattern)

These methods attempt to find the next occurrence of the specified pattern or the pattern constructed from the specified string, respectively, ignoring any delimiters. 582

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING

Example 12.13 Multi-Line Mode import static java.lang.System.out; import import import import

java.io.File; java.io.IOException; java.util.Scanner; java.util.regex.Pattern; class MultiLineMode { public static void main(String[] args) throws IOException {
String source = args[0];
// (1) Filename from program args
Pattern pattern = Pattern.compile("[a-zA-Z]+",
Pattern.MULTILINE);// (2) Search pattern
Scanner lexer = new Scanner(new File(source));// (3) Create a scanner
// Outer loop: while (lexer.hasNextLine()) {
// (4) Lookahead for next line
String match = lexer.findInLine(pattern);
// (5) Find the first match
// Inner loop: while (match != null) {
// (6) Parse rest of the line out.println(match); // (7) Process the match match = lexer.findInLine(pattern);
// (8) Get the next match
}
lexer.nextLine();
// (9) Clear rest of the line
}
IOException ioe = lexer.ioException(); if (ioe != null)
// (10) Check for read problem throw ioe; lexer.close(); // (11) Close the scanner
}
}

Running the program:
>java MultiLineMode MultiLineMode.java import static
...
scanner

Review Questions
12.7

Which statements are true about the following target string?
"oblaada oblaadi"

Select the three correct answers.
(a)
(b)
(c)
(d)

The regular expression a+ will match two substrings of the target string.
The regular expression aa+ will match two substrings of the target string.
The regular expression (aa)+ will match two substrings of the target string.
The regular expressions aa+ and (aa)+ will match the same two substrings of the target string.

583

12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS

12.8

Which statements are true about the following target string?
"oblaada oblaadi"

Select the three correct answers.
(a) The regular expression a? will match five non-empty substrings of the target string. (b) The regular expression aa? will match two non-empty substrings of the target string. (c) The regular expression (aa)? will match two non-empty substrings of the target string.
(d) The regular expressions aa? and (aa)? will not match the same non-empty substrings of the target string.
12.9

Which statement is true about the following target string?
"oblaada oblaadi"

Select the one correct answer.
(a) The regular expression a* will match three non-empty substrings of the target string. (b) The regular expression aa* will match at least two non-empty substrings of the target string.
(c) The regular expression (aa)* will match two non-empty substrings of the target string.
(d) The regular expressions a* and aa* will match the same non-empty substrings of the target string.
(e) All of the above.
12.10

Which statement is true about the following target string?
"0.5 7UP _4me"

Select the one correct answer.
(a)
(b)
(c)
(d)
(e)
(f)
(g)
12.11

The pattern \d will match 0.5, 7, and 4 in the target string.
The pattern \d will match 0, ., 5, 7, and 4 in the target string.
The pattern \w will match UP and me in the target string.
The pattern \s will match 0.5, 7UP, and _4me in the target string.
The pattern . will match the . character in the target string.
The regular expression [meUP] will match UP and me in the target string.
None of the above.

Which statements are true about the following program? import java.util.regex.Pattern; public class RQ500_10 { public static void main(String[] args) {
System.out.println(Pattern.matches("+?\d", "+2007"));
System.out.println(Pattern.matches("+?\\d+","+2007"));
System.out.println(Pattern.matches("\+?\\d+", "+2007"));

// (1)
// (2)
// (3)

584

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING
System.out.println(Pattern.matches("\\+?\\d+", "+2007"));

// (4)

}
}

Select the two correct answers.
(a) Only in the statements at (1) and (2) will the compiler report an invalid escape sequence. (b) Only in the statements at (3) and (4) will the compiler report an invalid escape sequence. (c) Only in the statements at (1) and (3) will the compiler report an invalid escape sequence. (d) The statements at (2) and (4) will compile but will throw an exception at runtime.
(e) After any compile-time errors have been eliminated, only one of the statements will print true when executed.
(f) None of the above.
12.12

Given the following code: import java.util.regex.Pattern; public class RQ500_20 { public static void main(String[] args) {
String[] regexes = {
"(-|+)\\d+",
"(-|+)?\\d+", "(-|\\+)\\d+",
"(-|\\+)?\\d+", "[-+]?\\d+", "[-+]?[0-9]+",
"[-\\+]?\\d+" };
// (1) INSERT DECLARATION STATEMENT HERE

// 0, 1, 2
// 3, 4, 5
// 6

System.out.println(Pattern.matches(regexes[i], "2007"));
System.out.println(Pattern.matches(regexes[i], "-2007"));
System.out.println(Pattern.matches(regexes[i], "+2007"));
}
}

Which declarations, when inserted independently at (1), will make the program print: true true true

Select the four correct answers.
(a) int i = 0;
(b) int i = 1;
(c) int i = 2;
(d) int i = 3;
(e) int i = 4;
(f) int i = 5;
(g) int i = 6;

12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS

12.13

585

Given the following code: import java.util.regex.Pattern; import java.util.regex.Matcher; public class RQ500_40 { public static void main(String[] args) {
String regex = "ja[^java]*va";
String index = "012345678901234567890123456";
String target = "jambo valued jam vacationer";
Pattern pattern = __________.compile(_________);
Matcher matcher = __________.matcher(_________); while(matcher.___________()) { int startIndex = matcher.__________(); int endIndex
= matcher.__________(); int lastIndex = startIndex == endIndex ? endIndex : endIndex-1;
String matchedStr = matcher._______();
System.out.print("(" + startIndex + "," + lastIndex + ":" + matchedStr + ")");
}
System.out.println();
}
}

Which identifiers, when filled in the blanks in the order they are specified, will make the program print:
(0,7:jambo va)(13,18:jam va)

Select the one correct answer.
(a) Pattern, pattern, target, regex,
(b) Matcher, pattern, regex, target,
(c) Matcher, pattern, regex, target,
(d) Pattern, regex, pattern, target,
(e) Pattern, regex, pattern, target,
(f) Pattern, regex, pattern, target,
12.14

find, start, end, group hasMore, start, end, element hasNext, start, end, next find, start, end, group hasNext, start, end, next find, start, end, result

What will the program print when compiled and run? public class RQ500_60 { public static void main(String[] args) {
String regex
= "[Jj].?[Aa].?[Vv].?[Aa]";
String target1 = "JAVA JaVa java jaVA";
String target2 = "JAAAVA JaVVa jjaavvaa ja VA";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(target1); makeMatch(matcher); matcher.reset(); makeMatch(matcher); matcher.reset(target2); makeMatch(matcher); } public static void makeMatch(Matcher matcher) {
System.out.print("|");

586

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING while(matcher.find()) {
System.out.print(matcher.group() + "|");
}
System.out.println();
}
}

Select the one correct answer.
(a) |JAVA|JaVa|java|jaVA|
|JAAAVA|JaVVa|jjaavva|ja VA|

(b) |JAVA|JaVa|java|jaVA|
|
|JAAAVA|JaVVa|jjaavva|ja VA|
(c) |JAVA|JaVa|java|jaVA|
|JAVA|JaVa|java|jaVA|
|JAAAVA|JaVVa|jjaavva|ja VA|
(d) |JAVA|JaVa|java|jaVA|
|JAVA|JaVa|java|jaVA|
|JaVVa|jjaavva|ja VA|

(e) The program will throw an exception when run.
12.15

What will the program print when compiled and run? public class RQ500_70 { public static void main(String[] args) {
System.out.print(Pattern.compile("\\s+")
.matcher("|
To be
.replaceAll(" "));
}
}

|\n|or \tnot \t\t\tto be|")

Select the one correct answer.
(a) | To be |
|or not to be|

(b) | To be |
|or

not

to be|

(c) | To be |
(d)
(e)
(f)
(g)
(h)
12.16

|or \tnot \t\t\tto be|
| To be | |or not to be|
| To be ||or not to be|
| To be ||or \tnot \tto be|

The program will not compile.
The program will throw an exception when run.

What will the program print when compiled and run? public class RQ500_80 { public static void main(String[] args) { matchMaker("X.*z", "XyzXyz Xz");
// (1) matchMaker("X.+z", "XyzXyz Xz");
// (2) matchMaker("X.*?z", "XyzXyz Xz"); // (3)

12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS matchMaker("X.+?z", "XyzXyz Xz");

// (4)

} public static void matchMaker(String regStr, String target) {
Matcher matcher = Pattern.compile(regStr).matcher(target);
System.out.print("|");
while(matcher.find()) {
System.out.print(matcher.group() + "|");
}
System.out.println();
}
}

Select the one correct answer.
(a) |Xyz|Xyz|Xz|
|XyzXyz|Xz|
|Xyz|Xyz|Xz|
|Xyz|Xyz|
(b) |XyzXyz Xz|
|XyzXyz Xz|
|Xyz|Xyz|Xz|
|Xyz|Xyz|
(c) |XyzXyz Xz|
|XyzXyz|Xz|
|XyzXyz Xz|
|XyzXyz|Xz|

(d) The program will throw an exception when run.
12.17

What will the program print when compiled and run? public class RQ500_90 { public static void main(String[] args) {
CharSequence inputStr = "no 7up 4 _u too!";
String patternStr = "[a-zA-Z0-9_]+";
Matcher matcher = Pattern.compile(patternStr).matcher(inputStr);
StringBuffer buf = new StringBuffer(); while (matcher.find()) {
String matchedStr = matcher.group(); matchedStr = Character.toUpperCase(matchedStr.charAt(0)) + matchedStr.substring(1); matcher.appendReplacement(buf, matchedStr);
}
matcher.appendTail(buf);
System.out.println(buf);
}
}

Select the one correct answer.
(a) No 7Up 4 _U Too!
(b) No 7up 4 _u Too!
(c) No 7Up 4 _u Too!
(d) No 7up 4 _U Too!
(e) The program will throw an exception when run.

587

588

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING

12.18

What will the program print when compiled and run? public class RQ500_110 { public static void main(String[] args) { printArray("Smile:-)and:)the:-(world.-)smiles:o)with-you". split("[.:\\-()o]+"));
}
private static void printArray(T[] array) {
System.out.print("|");
for (T element : array)
System.out.print(element + "|");
System.out.println();
}
}

Select the one correct answer.
(a) |Smile|and|the|world|smiles|with-you|
(b) |Smile|and|the|world|smiles|with-y|u|
(c) |Smile|and|the|world|smiles|with|you|
(d) |Smile|and|the|w|rld|smiles|with|y|u|
(e) The program will not compile.
(f) The program will compile and will throw an exception when run.
(g) The program will compile and will execute normally without printing anything.
12.19

Which statements are true about the Scanner class?
Select the 3 correct answers.
(a) The Scanner class has constructors that can accept the following as an argument: a String, a StringBuffer, a StringBuilder, a File, an InputStream, a Reader.
(b) The Scanner class provides a method called hasNextBoolean, but not a method called hasNextChar.
(c) The methods hasNext(), next(), skip(), findInLine(), and useDelimiters() of the Scanner class can take a Pattern or a String as an argument.
(d) The situation where the scanner cannot match the next token or where the input is exhausted, can be detected by catching an unchecked NoSuchElementException in the program.

12.20

Given the following code: public class RQ600_10 { public static void main(String[] args) {
Scanner lexer = new Scanner(System.in);
// (1) INSERT PRINT STATEMENT HERE.
}
}

12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS

589

Which print statements, when inserted independently at (1), will not make the program run as follows (with user input shown in bold):
>java RQ600_10
99 20.07 true 786
99
>

Select the three correct answers.
(a) System.out.println(lexer.nextByte());
(b) System.out.println(lexer.nextShort());
(c) System.out.println(lexer.nextInt());
(d) System.out.println(lexer.nextLong());
(e) System.out.println(lexer.nextDouble());
(f) System.out.println(lexer.nextBoolean());
(g) System.out.println(lexer.next());
(h) System.out.println(lexer.nextLine());
12.21

Given the following code: public class RQ600_30 { public static void main(String[] args) {
String input = "A00.20BCDE0.0060.0F0.800";
Scanner lexer = new Scanner(input).useDelimiter(____(1)_____);
System.out.print("|");
while (lexer.hasNext()) {
System.out.print(lexer.next() + "|");
System.out.print(lexer.nextInt() + "|");
}
lexer.close();
}
}

Which pattern strings, when inserted at (1), will not give the following output:
|A|2|BCDE|6|F|8|

Select the two correct answers.
(a) "[0\\.]+"
(b) "[0.]+"
(c) "(0|.)+"
(d) "(0|\\.)+"
(e) "0+(\\.)*"
(f) "0+\\.*0*"
12.22

What will the program print when compiled and run? public class RQ600_40 { public static void main(String[] args) {
String input = "_AB..0C.-12.),DEF0..-34G.(H.";
Scanner lexer = new Scanner(input).useDelimiter("\\w+\\."); while (lexer.hasNext())
System.out.print(lexer.next());

590

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING lexer.close(); }
}

Select the one correct answer.
(a) .-.),.-.(
(b) -),-(
(c) .-),-(.
(d) .-),.-(
(e) The program will not compile.
(f) The program will compile and will throw an exception when run.
12.23

Given the following code: public class RQ600_50 { public static void main(String[] args) {
String input = "1234||567.|12.34|.56||78.|.";
String delimiters = "\\|+";
// (1) INSERT DECLARATION HERE lexIt(regex, delimiters, input);
}
public static void lexIt(String regex, String delimiters, String input) {
Scanner lexer = new Scanner(input).useDelimiter(delimiters); while (lexer.hasNext()) { if (lexer.hasNext(regex))
System.out.printf("%7s", lexer.next(regex) + ","); else System.out.printf("%7s", "X" + lexer.next() + ",");
}
System.out.println(); lexer.close(); }
}

Which declaration statements, when inserted at (1), will give the following output:
1234,

567., 12.34,

.56,

78.,

X.,

Select the one correct answer.
(a) String regex = "\\d+\\.?";
(b) String regex = "\\.?\\d+";
(c) String regex = "\\d+\\.\\d+";
(d) String regex = "\\d*\\.?\\d*";
(e) String regex = "\\d+\\.?\\d*";
(f) String regex = "(\\d+\\.?|\\.?\\d+|\\d+\\.\\d+)";
(g) The program will not compile regardless of which declaration from above is inserted at (1).
(h) The program will compile and run, but will throw an exception regardless of which declaration from above is inserted at (1).

12.6: STRING PATTERN MATCHING USING REGULAR EXPRESSIONS

12.24

591

What will the program print when compiled and run? public class RQ600_70 { public static void main(String[] args) {
Scanner lexer = new Scanner("B4, we were||m8s & :-) 2C,1 THR,"); lexer.useDelimiter("[|,]"); System.out.print(""); lexer.useDelimiter("[a-z|& ]+");
System.out.print("");
lexer.useDelimiter("[ ,]");
System.out.print("");
lexer.next(); lexer.close(); }
}

Select the one correct answer.
(a) < we were>
(b) < we were>
(c)
(d) < we were>
(e) The program will not compile.
(f) The program will compile and will throw an exception when run.
12.25

What will the program print when compiled and run? public class RQ600_80 { public static void main(String[] args) {
Scanner lexer = new Scanner("Trick or treat"); while(lexer.hasNext()) { if(lexer.hasNext("[kcirTtea]+")) System.out.print("Trick!"); lexer.next(); } lexer.close(); }
}

Select the one correct answer.
(a)
(b)
(c)
(d)
(e)

The program will not compile.
The program will compile and will throw an exception when run.
The program will compile and will go into an infinite loop when run.
The program will compile, run, and terminate normally, without any output.
The program will compile, run, and terminate normally, with the output
Trick!.
(f) The program will compile, run, and terminate normally, with the output
Trick!Trick!.
(g) The program will compile, run, and terminate normally, with the output
Trick!treat!.

592

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING

12.26

Given the following code: public class RQ600_20 { public static void main(String[] args) {
System.out.print("|");
// (1) INSERT CODE HERE
System.out.println();
lexer.close();
}

Which code, when inserted independently at (1), will not print one of the lines shown below:
|2007| -25.0|mp3 4 u | true| after8|
|mp|u|true|after|
|2007.0|25.0|0.0|mp3|4.0|u|true|after8|
|4|
|2007|25|0|3|4|8|
|2007.0|-25.0|

Select the three correct answers.
(a) Scanner lexer = new Scanner("2007, -25.0,mp3 4 u , true, after8"); lexer.useDelimiter(","); while(lexer.hasNext())
System.out.print(lexer.next() + "|");

(b) Scanner lexer = new Scanner("2007, -25.0,mp3 4 u , true, after8"); lexer.useDelimiter("\\s*,\\s*"); while(lexer.hasNext()) if(lexer.hasNextDouble()) System.out.print(lexer.nextDouble() + "|"); else lexer.next();
(c) Scanner lexer = new Scanner("2007, -25.0,mp3 4 u , true, after8"); lexer.useDelimiter("\\s*,\\s*"); while(lexer.hasNext()) if(lexer.hasNextDouble()) System.out.print(lexer.nextDouble() + "|");

(d) Scanner lexer = new Scanner("2007, -25.0,mp3 4 u , true, after8"); lexer.useDelimiter("[,\\- .a-z]+"); while(lexer.hasNext()) if(lexer.hasNextInt())
System.out.print(lexer.nextInt() + "|"); else lexer.next();

(e) Scanner lexer = new Scanner("2007, -25.0,mp3 4 u , true, after8"); lexer.useDelimiter("[,\\- .\\d]+"); while(lexer.hasNext()) if(lexer.hasNextBoolean())
System.out.print(lexer.nextInt() + "|"); else lexer.next();

12.7: FORMATTING VALUES

593

(f) Scanner lexer = new Scanner("2007, -25.0,mp3 4 u , true, after8"); lexer.useDelimiter("[,\\- .\\d]+"); while(lexer.hasNext()) if(lexer.hasNextBoolean())
System.out.print(lexer.nextBoolean() + "|"); else System.out.print(lexer.next() + "|");

(g) Scanner lexer = new Scanner("2007, -25.0,mp3 4 u , true, after8"); lexer.useDelimiter("[,\\- .]+"); while(lexer.hasNext()) if(lexer.hasNextDouble())
System.out.print(lexer.nextDouble() + "|"); else System.out.print(lexer.next() + "|");

(h) Scanner lexer = new Scanner("2007, -25.0,mp3 4 u , true, after8"); lexer.useDelimiter("[,\\- .]+"); do { if(lexer.hasNextInt()) System.out.print(lexer.nextInt() + "|");
} while(lexer.hasNext());

(i) Scanner lexer = new Scanner("2007, -25.0,mp3 4 u , true, after8"); lexer.reset(); do { if(lexer.hasNextInt()) System.out.print(lexer.nextInt() + "|"); else lexer.next();
} while(lexer.hasNext());

12.7 Formatting Values
Overview
The class java.util.Formatter provides the core support for formatted text representation of primitive values and objects through its overloaded format() methods: format(String format, Object... args) format(Locale l, String format, Object... args)

Writes a string that is a result of applying the specified format string to the values in the vararg array args. The resulting string is written to the destination object that is associated with the formatter.
The destination object of a formatter is specified when the formatter is created. The destination object can, for example, be a String, a StringBuilder, a file, or any OutputStream.

594

CHAPTER 12: LOCALIZATION, PATTERN MATCHING, AND FORMATTING

The classes java.io.PrintStream and java.io.PrintWriter also provide an overloaded format() method with the same signature for formatted output. These streams use an associated Formatter that sends the output to the PrintStream or the PrintWriter, respectively. However, the format() method returns the current Formatter, PrintStream, or PrintWriter, respectively, for these classes, allowing method calls to be chained. The String class also provides an analogous format() method, but it is static. Unlike the format() method of the classes mentioned earlier, this static method returns the resulting string after formatting the values.
In addition, the classes PrintStream and PrintWriter provide the following convenience methods: printf(String format, Object... args) printf(Locale l, String format, Object... args)

These methods delegate the formatting to the format() method in the respective classes.
The java.io.Console only provides the first form of the format() and the printf() methods (without the locale specification), writing the resulting string to the console’s output stream, and returning the current console.
The syntax of the format string provides support for layout justification and alignment, common formats for numeric, string, and date/time values, and some locale-specific formatting. The format string can specify fixed text and embedded format specifiers. The fixed text is copied to the output verbatim, and the format specifiers are replaced by the textual representation of the corresponding argument values. The mechanics of formatting values is illustrated below by using the
PrintStream that is associated with the System.out field, i.e., the standard output stream. The following call to the printf() method of this PrintStream formats three values: System.out.printf("Formatted output|%6d|%8.3f|%10s|%n", // Format string
2008, Math.PI, "Hello");
// Values to format

At runtime, the following characters are printed to the standard output stream, according to the default locale (in this case, Norwegian):
Formatted output|

2008|

3,142|

Hello|

The format string is the first actual parameter in the method call. It contains four format specifiers. The first three are %6d, %8.3f, and %10s, which specify how the three arguments should be processed. Their location in the format string specifies where the textual representation of the arguments should be inserted. The fourth format specifier %n is special, and stands for a platform-specific line separator. All other text in the format string is fixed, including any other spaces or punctuation, and is printed unchanged.

12.7: FORMATTING VALUES

595

An implicit vararg array is created for the values of the three arguments specified in the call, and passed to the method. In the above example, the first value is formatted according to the first format specifier, the second value is formatted according to the second format specifier, and so on. The '|' character has been used in the format string to show how many character positions are taken up by the text representation of each value. The output shows that the int value was written rightjustified, spanning six character positions using the format specifier %6d, the double value of Math.PI took up eight character positions and was rounded to three decimal places using the format specifier %8.3f, and the String value was written rightjustified spanning ten character positions using the format specifier %10s. Since the default locale is Norwegian, the decimal sign is a comma (,) in the output. We now turn to the details of defining format specifiers.

Defining Format Specifiers
The general syntax of a format specifier is as follows:
%[argument_index][flags][width][precision]conversion

Only the special character % and the formatting conversion are not optional. Table
12.15 provides an overview of the formatting conversions. The occurrence of the character % in a format string marks the start of a format specifier, and the associated formatting conversion marks the end of the format specifier. A format specifier in the format string is replaced either by the textual representation of the corresponding value or by the specifier’s special meaning. The compiler does not provide much help regarding the validity of the format specifier. Depending on the error in the format specifier, a corresponding exception is thrown at runtime (see
Selected Format Exceptions, p. 601).
The optional argument_index has the format i$, or it is the < character. In the format i$, i is a decimal integer indicating the position of the argument in the vararg array, starting with position 1. The first argument is referenced by 1$, the second by 2$, and so on. The < character indicates the same argument that was used in the preceding format specifier in the format string, and cannot therefore occur in the first format specifier. The following printf() statements illustrate argument indexing:
String fmtYMD = "Year-Month-Day: %3$s-%2$s-%1$s%n";
String fmtDMY = "Day-Month-Year: %1$s-%2$s-%3$s%n"; out.printf(fmtYMD, 1, "March", 2008); // Year-Month-Day: 2008-March-1 out.printf(fmtDMY, 1, "March", 2008); // Day-Month-Year: 1-March-2008
out.printf("|%s|%

Similar Documents

Free Essay

Java Programming

...JAVA Programming PAPER Q1. A template argument is preceded by the keyword ________. ► vector ► class ► template ► type* Q2. Which of the following causes run time binding? ► Declaring object of abstract class ► Declaring pointer of abstract class ► Declaring overridden methods as non-virtual ► None of the given Q3. A function template can not be overloaded by another function template. ► True ► False Q4. Which of the following is the best approach if it is required to have more than one functions having exactly same functionality and implemented on different data types? ► Templates ► Overloading ► Data hiding ► Encapsulation Q5. Identify the correct way of declaring an object of user defined template class A for char type members? ► A< char >obj; ► Aobj; ► Aobj; ► Obj A; Q6. The user must define the operation of the copy constructor. ► True ► False Q7. Template functions use _________ than ordinary functions. ► Greater Memory ► Lesser Memory ► Equal Memory ► None of the given options Q8. The find() algorithm ► finds matching sequences of elements in two containers. ► finds a container that matches a specified container. ► takes iterators as its first two arguments. ► takes container elements as its first two arguments. Q9. Compiler performs ________...

Words: 1150 - Pages: 5

Free Essay

Java Programming

...MTH 112 Differential Calculus MTH 122 Integral Calculus MTH 142 Vectors and Geometry PHY 124 Geometric and Wave Optics PHY 132 Electricity, Magnetism and Modern Physics PHY 192 Introductory Practical Physics II * Students are expected to offer at least one elective course per semester. Also they can only register a maximum of 25 units per semester Total Credit Unit - Compulsory Total Credit Unit - Elective Total Credit Units 20 0 20 200 Level Course Code 1st Semester GST 201 CIT 211 GST 203 CIT 213 CIT 215 CIT 237 MTH 211 MTH 213 MTH 241 MTH 281 Course Titles Unit(s) Status Nigerian Peoples and Cultures Introduction to Operating Systems Introduction to Philosophy and Logic Elementary Data Processing Introduction to Programming Languages Programming & Algorithms Introduction to Set Theory and Abstract Algebra Numerical Analysis I Introduction to Real Analysis Mathematical Methods I * Students are expected to offer at least one elective course...

Words: 911 - Pages: 4

Premium Essay

Java Programming and Inheritance

...Muhammad 1 Mikey Muhammad Date Class Mr.Radev Java Programming and Inheritance As time continues to pass, the world we now live in is dominated by the use of technology. Technology continues to advance, and it seems, as if we can’t function without it. Whether it’s the use of technology in school or work, it is everywhere and you cannot escape it. It is important for us to be able to adapt to it and learn how the new inventions work and function properly. The Internet for example is one of the most important aspects of life today. The Internet allows us to have access to everything, and have endless information at our fingertips, whether it’s through a computer screen or through the use of a cellular device, or even a watch. Some of us choose to only use this amazing tool that has been provided to us, and then some us choose to go more in depth with this tool we call the Internet and want to know how it works. Although it takes millions of programs and concepts and other factors to get the Internet working how it does, the focus of my term paper will be Inheritance in Java. In order to truly understand what the specifics of Java are, you have to start with the totality of the Java program. The Java language could easily be the most difficult to understand because there are so many components to how it works. Java is a programing language and computing platform, which is a huge part of making the Internet work. It was first released by Sun Microsystems in 1995. Without...

Words: 1332 - Pages: 6

Premium Essay

Java Programming Language Sl-275

...Sun Educational Services Java Programming Language SL-275 Sun Educational Services Java Programming Language September 1999 Copyright 1999 Sun Microsystems, Inc., 901 San Antonio Road, Palo Alto, California 94303, U.S.A. All rights reserved. This product or document is protected by copyright and distributed under licenses restricting its use, copying, distribution, and decompilation. No part of this product or document may be reproduced in any form by any means without prior written authorization of Sun and its licensors, if any. Third-party software, including font technology, is copyrighted and licensed from Sun suppliers. Parts of the product may be derived from Berkeley BSD systems, licensed from the University of California. UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open Company, Ltd. Sun, Sun Microsystems, the Sun Logo, Solstice, Java, JavaBeans, JDK, and Solaris are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the U.S. and other countries. Products bearing SPARC trademarks are based upon an architecture developed by Sun Microsystems, Inc. The OPEN LOOK and Sun Graphical User Interface was developed by Sun Microsystems, Inc. for its users and licensees. Sun acknowledges the pioneering efforts of Xerox in researching and developing the...

Words: 6064 - Pages: 25

Premium Essay

Java Programming Execution Phases

...Advanced Programing in Java Umair Javed CS-391 Java Program Development and Execution Steps Java program normally go through five phases. These are 1. 2. 3. 4. 5. Edit, Compile, Load, Verify and Execute We look over all the above mentioned phases in a bit detail. First consider the following figure that summarizes the all phases of a java program. Phase 1 Editor Disk Program is created in the editor and stored on disk. Compiler creates bytecodes and stores them on disk. Phase 2 Compiler Disk Primary Memory Phase 3 Class Loader Class loader puts bytecodes in memory. Disk Primary Memory Phase 4 Bytecode Verifier Bytecode verifier confirms that all bytecodes are valid and do not violate Java’s security restrictions. Phase 5 Primary Memory Interpreter Interpreter reads bytecodes and translates them into a language that the computer can understand, possibly storing data values as the program executes. Umair© 2005, All Rights Reserved -1- TA: Munawar Nadeem Handout 3 Advanced Programing in Java Umair Javed CS-391 Phase 1: Edit Phase 1 consists of editing a file. This is accomplished with an editor program. The programmer types a java program using the editor like notepad, and make corrections if necessary. When the programmer specifies that the file in the editor should be saved, the program is stored on a secondary storage device such as a disk. Java program file name ends with a .java extension. On Windows...

Words: 696 - Pages: 3

Free Essay

Java Programming

...Activity 1: Numbers I. Problem Create an application that will input a start value and end value. The application will start value all the members from the start to end value and all the odd and even. II. Analysis Input Variable Data Type Description SV Integer Starting Value EV Integer End Value Output Variable NOS Integer Numbers OD Integer Odd numbers EV Integer Even numbers III. Output Layout IV. Codes import java.util.Scanner; public class EvenOdd { public static void main (String[]args) { int a; int b; Scanner in = new Scanner (System.in); System.out.print ("Starting Value: "); a=in.nextInt(); System.out.print ("End Value: "); b=in.nextInt(); System.out.println("number\t"+"odd\t"+"even\t"); for(int i=a; i<=b;i++) { System.out.print(i); if(i%2==0) { System.out.println("\t\t"+i );//even } else { System.out.println("\t"+i); //odd } } } } Activity 2: Transaction I. Problem Create an application that will transact the following item codes with looping and will print a receipt. II. Analysis Input Variable Data Type Description A Integer Letter A Price B Integer Letter B Price C Integer Letter C Price D Integer Letter D Price E Integer Letter E Price q Integer Quantity tno Integer ...

Words: 1321 - Pages: 6

Free Essay

Java Programming

...Encapsulation is whereby variables, functions are protected from being accessed by other codes outside the class. This helps manage the code being protected and has least if any impact on other parts of a program if change was to occur in the protected code. Encapsulation is important in java due to its flexibility and it is easy to change with new requirement, the class has total control over what is stored in its field, one part of the code can be changed and the rest of the would not be affected and also encapsulation is useful since it is easy to maintain. Both the mutator and accessor method are used to enforce data encapsulation where their jobs is returning and setting the value state of an object. Mutator method is used to set the private field value. It is denoted by using set at the beginning of the method name. Example of a mutator method would be public void setSchool (String school). Accessor method is used to return the private field value. It is denoted by using get at the beginning of the method name. An example of accessor method would be public String getFirstName () Implicit parameter is the object on which the method is invoked whereas explicit parameter is the parameter of the method other than the object the method is invoking and is enclosed in a parentheses. To access an explicit parameter variable inside the method refer the parameter variable by its name. To illustrate what is an implicit and explicit parameter we use the following code: Person...

Words: 498 - Pages: 2

Premium Essay

Intro to Programming Usin Java

...Introduction to Programming Using Java Course Specifications Course length: 1.0 day(s) Course Description Course Objective: You will learn the basic concepts of programming using Java as the tool for learning. Target Student: This course is intended for students with no prior academic background in a field other than computer science or programming, who wish to embark on a course of study that will prepare them for employment as professional software developers. Students who interact on a business basis with software development professionals and need to improve communication through better understanding of the concepts and terminology used by professional programmers will also benefit equally. Prerequisites: Students should be familiar with using personal computers with a mouse and keyboard. Basic typing skills are mandatory. Students should be comfortable in the use of the Windows 2000, Windows XP, Windows Vista environment, or Windows 7 environment. To ensure your success in this course, we recommend that you take the following Element K courses or have equivalent experience: Microsoft Windows Vista: Level 1Microsoft Windows XP Professional: Level 1 Course Objectives Upon successful completion of this course, students will be able to: · explore various programming concepts. · create simple programs. · use methods to define the behavior of classes. · implement the object-oriented methodology. · handle errors in a program. Course Content Lesson 1: Introduction...

Words: 323 - Pages: 2

Free Essay

Assignment and Essay.... Others)Information Technology (Programming/ Languages (Java, C++, Vb,.Net, & Etc)/Database Design/ Computer Networking/ System Analysis/ Project Management/Project Development/ It & Society/ and.

...ASSIGNMENT and ESSAY. ... others)Information Technology (Programming/ Languages (Java, C++, VB, .NET, & etc)/Database Design/ Computer Networking/ System Analysis/ Project Management/Project Development/ IT & Society/ and. - NET programmers continue to struggle with the complexities of a hybrid managed/unmanaged environment. ..... Sorry, I had to laugh at that paper! ... Java on the other hand is cross-platform, and also traditionally runs as an ... - NET programmers continue to struggle with the complexities of a hybrid managed/unmanaged environment. ..... Sorry, I had to laugh at that paper! ... Java on the other hand is cross-platform, and also traditionally runsASSIGNMENT and ESSAY. ... others)Information Technology (Programming/ Languages (Java, C++, VB, .NET, & etc)/Database Design/ Computer Networking/ System Analysis/ Project Management/Project Development/ IT & Society/ and. - NET programmers continue to struggle with the complexiASSIGNMENT and ESSAY. ... others)Information Technology (Programming/ Languages (Java, C++, VB, .NET, & etc)/Database Design/ Computer Networking/ System Analysis/ Project Management/Project Development/ IT & Society/ and. - NET programmers continue to struggle with the complexities of a hybrid managed/unmanaged environment. ..... Sorry, I had to laugh at that paper! ... Java on the other hand is cross-platform, and also traditionally runs as an ... - NET programmers continue to struggle with the complexities of a hybrid managed/unmanaged environment...

Words: 784 - Pages: 4

Free Essay

Object Oriented Programming in Java ‐ Exercises

...OBJECT ORIENTED PROGRAMMING IN JAVA ‐ EXERCISES    CHAPTER 1    1. Write Text‐Based Application using Object‐Oriented Approach to display your name.    // filename: Name.java // Class containing display() method, notice the class doesnt have a main() method public class Name { public void display() { System.out.println("Mohamed Faisal"); } } // filename: DisplayName.java // place in same folder as the Name.java file // Class containing the main() method public class DisplayName { public static void main(String[] args) { Name myname = new Name(); // creating a new object of Name class myname.display(); // executing the display() method in the Name class } }   2. Write a java Applet to display your age.  // filename: DisplayNameApplet.java import java.applet.Applet; // import necessary libraries for an applet import java.awt.Graphics; public class DisplayNameApplet extends Applet { public void paint(Graphics g) { g.drawString("Mohamed Faisal", 50, 25); } } // filename: DisplayNameApplet.htm // place in same folder as the compiled DisplayNameApplet.class file Displaying my Name CHAPTER 2    3.  Write a program that calculates and prints the product of three integers.    // filename: Q1.java import java.util.Scanner; // import Scanner libraries for input public class Q1 { public static void main(String[] args) { Scanner input = new Scanner (System.in); int number1; int number2; int number3; System.out.println("Enter the First Number"); www.oumstudents...

Words: 3130 - Pages: 13

Free Essay

Object Oriented Programming -Java

...Table of Contents TASK A ....................................................................................................................................................... 4 Provide the UML diagrams for the given problem with clear explanations on the design decisions. Derive detailed Use Case diagram, Class diagram & a sequence diagram. Whenever necessary document the relevant assumptions you made. ...................................................................................... 4 TASK B ....................................................................................................................................................... 7 Provide an alternative OO design for the same problem ......................................................................... 7 Object Oriented Known as Methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance. ......................................................................... 7 TASK C ....................................................................................................................................................... 9 There are many system design patterns available in system development. Critically evaluate singleton, factory and abstract factory design patterns and apply the most suitable design pattern for your system development.................................................................................................

Words: 4819 - Pages: 20

Premium Essay

Step by Step Java Eclipse Programming

...They should not be abolished because: * Exams make people better at the subject * content tested in exams are random, so people study everything * since it affects grades, people study harder * there is competition, people study harder * They help the teacher to know what material the students have not mastered and need to study further * They give people important practice in dealing with stress, which can help later on with life's challenges. * The examination system has its own benefits. It's like a self-test to see where we are and how good we fare in something that we do. The purpose of exam has always been a test to let us, human beings, to know our limits and our standards. * Frankly speaking, who can put his/her hand on their heart and say that they would have learned the lessons regarding different subjects themselves if the present examination system were not there and didn't made the students to learn and understand the topics dealing with different subjects to perform at the end of the year. The ANSWER would be No, They wouldn't have. Exams should not be abolished.  Until a better form of demonstrating knowledge comes along, exams must remain in place. In the world of occupations, exams are especially useful.  A given state, county, town, or other regionally-governed area requires certain knowledge for certain occupations.  The most effective way of evaluating an individual's knowledge of a given topic is the use of exams.  These...

Words: 623 - Pages: 3

Premium Essay

Getting Started

...School management system project in java pdf This documentation have all the details about school management system, even in this. Our VisionTo become one of the leading lights in software and webapplication. Generate Bonafide Certificate into PDF Format : 72. AppSolit Project : Managing School Java, Swing, Itext, PDF, JFrame. Boutique Management Software in Java with MySql, JDBC Swing. 2010-повідомлень: 5-авторів: 5Student Management System deals with all kind of student details, academic.pdf Student Management System.pdf Size: 381. Student Management System is software which is helpful for students as well as the school authorities. 2 Database Programming with JDBC and Java by OReillyNeed help with school management system college project java download? Do you specialise in school management system college. School Grade System is a simple project developed in java, jsp servlet for. Tags: exam grading system, exam management system, school grade. Tags: convert pdf files, convert ppt in java, file converter, free java project.School Grade System is a simple project developed in java, jsp servlet for small schools that want to. Knowledge Management System ServletJSP Project. school management system project in java with source code Student attendance management system project in java with source code. At product Instructions for downloadable manuals in PDF. The Relationship Of School Uniforms To Student Attendance.School of informatics. The aim of the project is to develop a...

Words: 610 - Pages: 3

Free Essay

Bankaccount.Java and Program 2.Java

...Question Compile the two test files (BankAccount.java first and then Program2.java second). Execute Program2 with the following inputs: starting balance - $500 (don't enter the dollar sign) monthly interest rate - 0.00125 (this is a 1.5% annual rate) monthly pay - $1000 (don't enter the dollar sign) withdrawal amount - $900 (don't enter the dollar sign) Verify that you earn $0.75 in interest and have an ending balance at the end of the month of $600.75. Then modify the BankAccount class's constructor method to create a BankAccount object which stores a monthly interest when the user inputs an annual interest rate of the format "nnn.nn" (i.e. 1.5). Note that the BankAccount constructor stored a monthly interest rate for the BankAccount object's instance field originally, but the user had to convert the annual rate to a monthly rate (i.e. 1.5 to 0.00125). Then modify the Program2 driver class to prompt the user for an annual interest rate. Recompile both classes and execute the modified Program2 driver class again, this time with following inputs: starting balance - $500 (don't enter the dollar sign) annual interest rate - 1.5 monthly pay - $1000 (don't enter the dollar sign) withdrawal amount - $900 (don't enter the dollar sign) Verify that you still earn $0.75 in interest and still have an ending balance at the end of the month of $600.75 as you did with the original code. Submit only the modified source code...

Words: 1152 - Pages: 5

Free Essay

Programming

...- -[ ] http://blog.renren.com/blog/73603/740437492 » / [ ] 2011-07-18 10:47 | ( : ) http://coolshell.cn/articles/4990.html 6 12 programming An open letter to those who want to start id Mailper Python Web “ Build Your Programming Technical Skills “ ( ) Notes/ActiveX/COM/ADO/ATL/.NET …… Mailper Delphi/Power builder ” Lotus 5 3D TA 561 ” The architecture of w-ai.org –... Some updates and my first Engl... / 10 Unix/Linux Windows 1 Linux Web+ 1 Python/Ruby Web 2 iOS Android Windows Windows Windows Linux+ Windows 9 : csv ( log python csv, python open, python sys) 52 word count (sys, os, path) (python sqlite) print Google (phrase, domain, use reader to follow tech blogs) 2 ( Vim / Emacs / Notepad++ Source Insight ( ctag) Cool IDE) / / 3 Unix/Linux Shell windows man ls/chmod/chown/rm/find/ln/cat/mount/mkdir/tar/gzip … sed/awk/grep/tail/less/more … ps/top/lsof/netstat/kill/tcpdump/iptables/dd… /etc /var/log /proc linux vmware player Ubuntu 1 6 9/6/13 1:36 PM - -[ ] http://blog.renren.com/blog/73603/740437492 /Linux 4 Web Web HTML CSS HTML Firefox + Firebug Javascript HTML DOM Firefox + Firebug Apache PHP PHP PHP chrome Nginx HTML MySQL MySQL SQL http://www.stanford.edu/~ouster/cgi-bin/cs142-fall10/index.php ) javascript HTTP: The Definite Guide browsers) Cookie/Session jQuery 4 3-5 ExtJS + Ajax ( +JSON (proxy, gateway, Javascript box model chrome DOM http://oreilly.com/catalog/9780596527402)...

Words: 807 - Pages: 4