introduction to the art of programming using · pdf fileprogramming using scala markc.lewis...
TRANSCRIPT
CHAPMAN & I IALL/CRC
! ! XIIUXVxs IN COMPUTING
INTRODUCTION
TOTHE ART OF
PROGRAMMING
USING
SCALA
Mark C. Lewis
CRC PressTaylor &Francis CroupBoca Raton London New York
CRC Press is an imprint of the
Taylor & Francis Group, an Informa business
A CHAPMAN & HALL BOOK
Contents
List of Figures xix
List of Tables xxv
Acknowledgments xxvii
Preface xxix
I Introductory Concepts 1
1 Basics of Computers, Computing, and Programming 3
1.1 History 3
1.2 Hardware 5
1.3 Software 7
1.4 Nature of Programming 9
1.5 Programming Paradigms 11
1.5.1 Imperative Programming 11
1.5.2 Functional Programming 11
1.5.3 Object-Oriented Programming 12
1.5.4 Logic Programming 13
1.5.5 Nature of Scala 13
1.6 End of Chapter Material 13
1.6.1 Summary of Concepts 13
1.6.2 Exercises 14
1.6.3 Projects 14
2 Getting to Know the Tools 17
2.1 Unix/Linux (includes Mac OS X) 18
2.1.1 Command-Line 18
2.1.1.1 Files and Directories 18
2.1.1.2 Aside 22
2.1.1.3 Helpful Tips 23
2.1.1.4 Permissions 24
2.1.1.5 Compression/Archiving 26
2.1.1.6 Remote 27
2.1.1.7 Other Commands 29
2.1.2 I/O Redirection 30
2.1.3 Text Editors (vi/vim) 31
2.2 Windows 33
2.2.1 Command-Line 34
2.2.1.1 Files and Directories 34
2.2.2 Text Editors 36
v
vi Contents
2.2.2.1 Edit 36
2.2.2.2 Notepad 36
2.2.2.3 Others 36
2.2.3 Other Commands 37
2.3 Scala Tools •
38
2.4 End of Chapter Material 40
2.4.1 Summary of Concepts 40
2.4.2 Exercises 41
3 Scala Basics 43
3.1 Expressions, Types, and Basic Math 43
3.2 Objects and Methods 46
3.3 Other Basic Types 48
3.4 Back to the Numbers 50
3.4.1 Binary Arithmetic 52
3.4.2 Negative Numbers in Binary 53
3.4.3 Other Integer Types 54
3.4.4 Octal and Hexadecimal 55
3.4.5 Non-Integer Numbers 56
3.5 The math Object 57
3.6 Details of Char and String 58
3.7 Naming Values and Variables 59
3.8 Sequential Execution 63
3.8.1 Comments 64
3.9 A Tip for Learning to Program 64
3.10 End of Chapter Material 65
3.10.1 Problem-Solving Approach 65
3.10.2 Summary of Concepts 66
3.10.3 Self-Directed Study 67
3.10.4 Exercises 68
4 Conditionals ^1
4.1 Motivating Example 71
4.2 The if Expression 72
4.3 Comparisons 75
4.4 Boolean Logic 76
4.5 Bigger Expressions 79
4.6 End of Chapter Material 83
4.6.1 Problem-Solving Approach 83
4.6.2 Summary of Concepts 84
4.6.3 Self-Directed Study 84
4.6.4 Exercises 85
4.6.5 Projects 87
5 Functions 89
5.1 Motivating Example 89
5.2 Function Refresher 90
5.3 Making and Using Functions 91
5.4 Problem Decomposition 95
5.5 Function Literals 100
5.6 Non-Functional Functions/Procedures 101
Contents vii
5.7 type Declarations 103
5.8 Putting it Together 103
5.9 End of Chapter Material 105
5.9.1 Problem-Solving Approach 105
5.9.2 Summary of Concepts 106
5.9.3 Self-Directed Study 106
5.9.4 Exercises 107
5.9.5 Projects 108
6 Recursion for Iteration 111
6.1 Basics of Recursion Ill
6.2 Writing Recursion 113
6.3 User Input 117
6.4 Abstraction 120
6.5 Matching 123
6.6 Putting it Together 125
6.7 Looking Ahead 127
6.8 End of Chapter Material 128
6.8.1 Problem-Solving Approach 128
6.8.2 Summary of Concepts 128
6.8.3 Self-Directed Study 129
6.8.4 Exercises 130
6.8.5 Projects 131
7 Arrays and Lists in Scala 135
7.1 Making Arrays 136
7.2 Using Arrays 137
7.3 Lists 140
7.4 Standard Methods 143
7.4.1 Basic Methods 143
7.4.2 Higher-Order Methods 146
7.4.3 Combinatorial/Iterator Methods 151
7.5 A World of Types 153
7.5.1 The Option Type 154
7.5.2 Parametric Functions 154
7.5.3 Subtyping 156
7.6 Variable Length Argument Lists 156
7.7 Mutability and Aliasing 159
7.8 Basic Argument Passing 162
7.9 Pass-By-Name 165
7.10 Fill and Tabulate 167
7.11 Complete Grades Script/Software Development 168
7.12 End of Chapter Material 173
7.12.1 Problem-Solving Approach 173
7.12.2 Summary of Concepts 173
7.12.3 Self-Directed Study 174
7.12.4 Exercises 175
7.12.5 Projects 177
viii Contents
8 Loops 181
8.1 while Loop 181
8.2 do-while Loop 183
8.3 for Loop 184
8.3.1 Range Type •
186
8.3.2 yield 188
8.3.3 if Guards 188
8.3.4 Multiple Generators 189
8.3.5 Patterns in for Loops 190
8.3.6 Variable Declarations 191
8.4 Multidimensional Arrays 191
8.5 Testing 195
8.6 Putting it Together 198
8.7 End of Chapter Material 2°0
8.7.1 Problem-Solving Approach 200
8.7.2 Summary of Concepts 201
8.7.3 Self-Directed Study 202
8.7.4 Exercises 203
8.7.5 Projects 204
9 Text Files 209
9.1 I/O Redirection 21°
9.2 Packages and import Statements 210
9.3 Reading from Files 212
9.3.1 Iterators 2X3
9.3.2 String split Method 214
9.3.3 Reading from Other Things 215
9.3.4 Other Options (Java Based) 216
9.4 Writing to File 217
9.4.1 Appending to File 2i7
9.5 Use Case: Simple Encryption 219
9.5.1 Command-Line Arguments 220
9.5.2 Mapping a File 220
9.5.3 Character Offset 221
9.5.4 Alphabet Flip 221
9.5.5 Key Word 222
9.5.6 Putting it Together 222
9.5.7 Primes and Real Cryptography 223
9.6 End of Chapter Material 224
9.6.1 Summary of Concepts 224
9.6.2 Self-Directed Study 225
9.6.3 Exercises 225
9.6.4 Projects 226
10 Case Classes 229
10.1 User-Defined Types 230
10.2 Case Classes 230
10.2.1 Making Objects 231
10.2.2 Accessing Elements 231
10.2.3 Named and Default Arguments (Advanced) 232
10.2.4 The copy Method 233
Contents ix
10.2.5 Case Class Patterns 234
10.3 Mutable Classes 234
10.4 Putting it Together 235
10.5 End of Chapter Material 241
10.5.1 Summary of Concepts 241
10.5.2 Self-Directed Study 241
10.5.3 Exercises 242
10.5.4 Projects 243
11 GUIs 247
11.1 GUI Libraries and History 247
11.2 GUI Components 248
11.2.1 Frames and Windows 248
11.2.2 Components 250
11.2.3 Panels and Panes 251
11.2.4 Menus 255
11.2.5 Tooltips 258
11.3 Basic Interactivity 258
11.3.1 Partial Functions 258
11.3.2 Publishers and Reactors 259
11.3.3 Mnemonics 260
11.4 FileChooser 261
11.5 Tables 262
11.5.1 Table Actions 264
11.5.2 Table Models 266
11.6 Putting it Together 268
11.7 End of Chapter Material 278
11.7.1 Summary of Concepts 278
11.7.2 Self-Directed Study 279
11.7.3 Exercises 280
11.7.4 Projects 280
12 Graphics 283
12.1 Custom-Drawn Panels 284
12.2 java.awt.Graphics2D 284
12.2.1 Shapes 285
12.2.2 Settings 287
12.2.2.1 Paint 287
12.2.2.2 Stroke 290
12.2.2.3 Font 292
12.2.2.4 Clip 294
12.2.2.5 Transform 294
12.2.3 Affine Transforms 294
12.3 Images 297
12.3.1 Bufferedlmage 297
12.3.2 Reading and Writing Images 298
12.3.3 Double Buffering 298
12.3.4 Revisiting TexturePaint 299
12.4 Other Events 299
12.4.1 Mouse Events 301
12.4.2 Keyboard Events 302
X Contents
30312.5 Animation with Timer
12.6 Putting it Together 4
12.7 End of Chapter Material 3J?712.7.1 Summary of Concepts 307
<?f)Q12.7.2 Exercises 6m
12.7.3 Projects 309
13 Sorting and Searching 315
13.1 Basic Comparison Sorts 31^
13.1.1 Bubble Sort 316
13.1.2 Selection Sort (Min/Max) 318
13.1.3 Insertion Sort 319
13.1.4 Testing and Verifying Sorts 320
13.1.5 Sort Visualization 321
13.1.6 Order Analysis 323
13.1.7 Shell Sort (Diminishing Gap Sort) 324
13.2 Searching 325
13.2.1 Sequential Search (Linear Search) 326
13.2.2 Binary Search 327
13.3 Performance and Timing 332
13.4 Classifying Bugs 333
13.5 Memory Layout 336
13.6 Sorting/Searching with case classes 336
13.7 Putting it Together 341
13.8 End of Chapter Material 343
13.8.1 Summary of Concepts 343
13.8.2 Exercises 344
13.8.3 Projects 345
14 XML 349
14.1 Description of XML 350
14.1.1 Tags 350
14.1.2 Elements 350
14.1.3 Attributes 351
14.1.4 Content 351
14.1.5 Special Characters 351
14.1.6 Comments 351
14.1.7 Overall Format 352
14.1.8 Comparison to Flat File 352
14.1.8.1 Flexibility in XML 352
14.2 XMLinScala 353
14.2.1 Loading XML 353
14.2.2 Parsing XML 354
14.2.3 Building XML 356
14.2.4 Writing XML to File 357
14.2.5 XML Patterns 358
14.3 Putting it Together 358
14.4 End of Chapter Material 364
14.4.1 Summary of Concepts 3^4
14.4.2 Self-Directed Study 365
14.4.3 Exercises 365
Contents xi
14.4.4 Projects 366
15 Recursion 369
15.1 Power of Recursion 369
15.2 Fibonacci Numbers 370
15.3 Permutations 373
15.4 Towers of Hanoi 374
15.5 Mazes 377
15.6 Sorts 380
15.6.1 Divide and Conquer Sorts 381
15.6.1.1 Merge Sort 381
15.6.1.2 Quicksort 383
15.7 Putting it Together 384
15.8 End of Chapter Material 386
15.8.1 Summary of Concepts 386
15.8.2 Exercises 386
15.8.3 Projects 387
II Object-Orientation, Abstraction, and Data Structures 391
16 Object-Orientation 393
16.1 Basics of Object-Orientation 394
16.1.1 Methods and Members 394
16.1.1.1 Parameters as Members 395
16.1.1.2 Visibility 396
16.1.2 this Keyword 399
16.1.3 Encapsulation/Separating Interface from Implementation 399
16.1.4 Special Methods 400
16.1.4.1 Operator Methods 400
16.1.4.2 Unary Operators 402
16.1.4.3 Property Assignment Methods 403
16.1.4.4 The apply Method 404
16.1.4.5 The update Method 405
16.1.4.6 Overloading Methods 405
16.1.4.7 Vector Example 406
16.1.5 object Declarations 408
16.1.5.1 Applications 408
16.1.5.2 Introduction to Companion Objects 409
16.1.6 Object Decomposition 409
16.2 Revisiting the API 409
16.3 Meaning of case classes 410
16.4 import Options 410
16.5 End of Chapter Material 412
16.5.1 Summary of Concepts 412
16.5.2 Exercises 414
16.5.3 Projects 415
xii Contents
17 Bigger Programs/New Tools 423
17.1 Eclipse IDE 423
17.2 Concurrent Versions System (CVS) 430
17.2.1 Making the Repository 430
17.2.2 Set-up in Eclipse 430
17.2.3 Getting Access on Other Systems 432
17.2.4 Repository Usage and Rules 432
17.3 Scaladoc Comments 433
17.4 End of Chapter Material 434
17.4.1 Summary of Concepts 434
17.4.2 Exercises 435
17.4.3 Projects 435
18 A Project (Drawing Program)18.1 Software Development Stages 437
18.2 Analysis 438
18.3 Design 440
18.4 Making Packages 441
18.5 Project Analysis and Design 442
18.6 Implementing the GUI 442
18.7 End of Chapter Material 447
18.7.1 Summary of Concepts 447
18.7.2 Exercises 448
18.7.3 Projects 448
19 Abstraction and Polymorphism 4^
19.1 Polymorphism 452
19.2 Inclusion Polymorphism (Inheritance and Subtyping) 452
19.2.1 private Visibility and Inheritance 4«>9
19.2.2 Protected Visibility 459
19.2.3 Calling Methods on the Supertype 460
19.2.4 Anonymous Classes 4^1
19.2.5 Abstract Classes 461
19.2.6 traits 463
19.2.7 final 464
19.2.8 Method Resolution 466
19.2.9 Inheriting from Function Types 467
19.3 Inheritance in the Project 4*>8
19.3.1 Drawables 468
19.3.2 Integration with Drawing 478
19.4 Parametric Polymorphism 481
19.4.1 Parametric Types 482
19.4.2 Parametric Functions and Methods 485
19.4.3 Type Bounds 488
19.5 End of Chapter Material 492
19.5.1 Summary of Concepts 492
19.5.2 Exercises 493
19.5.3 Projects 493
Contents xiii
20 Other Collection Types 497
20.1 The scala.collection Packages 497
20.1.1 scala.collection.immutable 498
20.1.2 scala.collection.mutable 500
20.2 Sets 501
20.2.1 Running Through Sets 502
20.2.2 Mutable vs. Immutable '502
20.2.3 Using a Set 504
20.3 Maps 505
20.3.1 Looping Through a Map 507
20.3.2 Using Maps 508
20.4 Buffers 510
20.5 Collections as Functions 512
20.6 Project Integration 512
20.6.1 Commands 512
20.6.2 Adding Drawables 515
20.7 End of Chapter Material 517
20.7.1 Summary of Concepts 517
20.7.2 Exercises 517
20.7.3 Projects 518
21 Multithreading and Concurrency 521
21.1 The Multicore Future 522
21.2 Basic Threads 523
21.2.1 Problems with Threads 526
21.2.1.1 Race Conditions 526
21.2.1.2 Deadlock 528
21.2.2 Synchronization 529
21.2.3 Wait/Notify 532
21.2.4 Other Thread Methods 534
21.3 Concurrency Library 535
21.3.1 Executors and Executor Services 535
21.3.2 Callable and Futures 536
21.3.3 Parallel Data Structures 539
21.3.3.1 Shared Barriers 539
21.3.3.2 The Exchange 540
21.3.3.3 Assembly Line 540
21.3.3.4 Ticketed Passengers 541
21.3.3.5 Other Threadsafe Types 541
21.3.4 Atomic (java.util.concurrent.atomic) 542
21.3.5 Locks (java.util.concurrent.locks) 542
21.4 Parallel Collections 543
21.5 Introduction to Scala Actors 546
21.6 Multithreaded Mandelbrot (Project Integration) 548
21.7 Multithreading in GUIs 551
21.8 Animated Bouncing Balls (Project Integration) 553
21.9 End of Chapter Material 555
21.9.1 Summary of Concepts 555
21.9.2 Exercises 556
21.9.3 Projects 557
xjv Contents
22 Stream I/O 561
22.1 The java.io Package 561
22.2 Streams for Files 562
22.3 Exceptions 564
22.3.1 try-catch-finally 565
22.3.2 Effect of Exceptions 567
22.3.3 Loan Pattern 567
22.4 Decorating Streams 568
22.4.1 Buffering 568
22.4.2 Binary Data 569
22.4.3 Serialization - 572
22.4.3.1 Binary Serialization 572
22.4.3.2 XML Serialization 577
22.5 Saving Drawings (Project Integration) 578
22.6 End of Chapter Material 594
22.6.1 Summary of Concepts 594
22.6.2 Exercises 595
22.6.3 Projects 596
23 Networking 597
23.1 TCP and UDP 597
23.2 Sockets 598
23.2.1 TCP Sockets 598
23.2.2 UDP Sockets 599
23.2.3 Streams from Sockets 601
23.3 URLs 606
23.4 Remote Method Invocation (RMI) 608
23.5 Collaborative Drawing (Project Integration) 616
23.6 End of Chapter Material 628
23.6.1 Summary of Concepts 628
23.6.2 Exercises. . .
628
23.6.3 Projects 629
24 Stacks and Queues 633
24.1 Abstract Data Types (ADTs) 634
24.2 Operations on Stacks and Queues 634
24.3 Real Meaning of O 635
24.4 0(1) Requirement 636
24.5 Array-Based Stack 637
24.6 Array-Based Queue 641
24.7 Unit Tests 644
24.7.1 Setup 644
24.7.2 Writing Tests 645
24.7.3 Test Suites 648
24.7.4 Test-Driven Development 649
24.8 RPN Calculator 649
24.9 End of Chapter Material 653
24.9.1 Summary of Concepts 653
24.9.2 Exercises 654
24.9.3 Projects 654
Contents xv
25 Linked Lists 657
25.1 The List/Seq ADT 657
25.2 Nature of Arrays 658
25.3 Nature of Linked Lists 658
25.4 Mutable Singly-Linked List 660
25.4.1 Implementing mutable.Buffer 663
25.4.2 Iterators 666
25.5 Mutable Doubly-Linked List 668
25.6 Immutable Singly-Linked List 671
25.7 Linked List-Based Stacks and Queues 674
25.7.1 Linked List-Based Stack 674
25.7.2 Linked List-Based Queue 675
25.8 End of Chapter Material 676
25.8.1 Summary of Concepts 676
25.8.2 Exercises 676
25.8.3 Projects 677
26 Priority Queues 679
26.1 Two Approaches 679
26.1.1 Searching by Priority 680
26.1.2 Sorted Linked List 680
26.1.3 Problems with Arrays 682
26.2 Project Integration: Discrete Event Simulation 683
26.2.1 Cell Splitting 683
26.2.2 Collision Handling 686
26.3 End of Chapter Material 693
26.3.1 Summary of Concepts 693
26.3.2 Exercises 694
26.3.3 Projects 694
27 Refactoring 697
27.1 Smells 698
27.2 Refactorings 701
27.2.1 Built-in Refactoring Methods 701
27.2.2 Introduce Null Object 702
27.2.3 Add and Remove Parameter 702
27.2.4 Cures for Switch Statements 703
27.2.5 Consolidate Conditional Expression . 705
27.2.6 Convert Procedural Design to Objects 705
27.2.7 Encapsulate Collection 705
27.2.8 Push Down or Pull Up Field or Method 706
27.2.9 Substitute Algorithm 707
27.3 End of Chapter Material 707
27.3.1 Summary of Concepts 707
27.3.2 Exercises 708
27.3.3 Projects 708
xvi Contents
28 Recursion 709
28.1 Refresher 709
28.2 Project Integration: A Maze 710
28.2.1 Revisiting the Basic Approach 713
28.2.2 Graphical Editing 714
28.2.3 Optimizing the Maze 716
28.3 Graph Traversal 718
28.4 Divide and Conquer "725
28.4.1 Merge Sort 725
28.4.2 Quicksort 727
28.4.3 Formula Parser •-• 730
28.5 End of Chapter Material 731
28.5.1 Summary of Concepts 731
28.5.2 Exercises•
732
28.5.3 Projects 732
29 Trees 735
29.1 General Trees 735
29.1.1 Implementations 736
29.1.2 Traversals 737
29.2 Project Integration: Formula Parsing 740
29.2.1 Formula Tree Traversals and In-order Traversal 744
29.3 Binary Search Trees: Binary Trees as Maps 745
29.3.1 Order Analysis 751
29.3.2 Immutable BSTs 752
29.4 End of Chapter Material 755
29.4.1 Summary of Concepts 755
29.4.2 Exercises 756
29.4.3 Projects 757
30 Regular Expressions and Context-Free Parsers 759
30.1 Chomsky Grammars 759
30.1.1 Regular Grammars 761
30.1.2 Context-Free Grammars 761
30.1.3 Context-Sensitive Grammars 762
30.1.4 Recursively Enumerable Grammars 762
30.2 Regular Expressions 762
30.2.1 Characters and Character Classes 763
30.2.2 Logical Operators and Capturing Groups 764
30.2.3 Greedy Quantifiers 765
30.2.4 Boundary Requirements 765
30.2.5 Using Regular Expressions in Code 766
30.2.6 Drawback of Regular Expressions 768
30.3 Context-Free Parsers 768
30.3.1 Default Output 770
30.3.2 Specified Output 770
30.4 Project Integration 771
30.5 End of Chapter Material 774
30.5.1 Summary of Concepts 774
30.5.2 Exercises 775
30.5.3 Projects 776
Contents xvii
31 Spatial Trees 777
31.1 Spatial Data and Grids 777
31.2 Quadtrees and Octrees 782
31.3 kD-Trees 785
31.4 Efficient Bouncing Balls 789
31.5 End of Chapter Material 789
31.5.1 Summary of Concepts 789
31.5.2 Exercises 790
31.5.3 Projects 790
32 Binary Heaps 793
32.1 Binary Heaps 793
32.1.1 Binary Heaps as Arrays 793
32.2 Heaps as Priority Queues 794
32.3 Heapsort 801
32.4 End of Chapter Material 802
32.4.1 Summary of Concepts 802
32.4.2 Exercises 803
32.4.3 Projects 803
33 Direct Access Binary Files 805
33.1 Random Access Files 805
33.1.1 Fixed Record Length 806
33.1.2 Indexed Variable Record Length 811
33.2 Linked Structures in Files 813
33.3 End of Chapter Material 821
33.3.1 Summary of Concepts 821
33.3.2 Exercises 821
33.3.3 Projects 822
34 Actors 823
34.1 Actor Model 823
34.2 scala.actors Package 824
34.2.1 The receive Method and Messages 825
34.2.2 react, loop, and loopWhile 828
34.3 Circuit Simulation 830
34.4 End of Chapter Material 840
34.4.1 Summary of Concepts 840
34.4.2 Exercises 841
34.4.3 Projects 842
35 Augmenting Trees 843
35.1 Augmentation of Trees 843
35.2 Balanced BSTs 844
35.2.1 Rotations 844
35.2.2 Implementation 845
35.3 Order-Statistic Trees: Trees as Sequences 849
35.4 Augmented Spatial Trees 856
35.5 End of Chapter Material 860
35.5.1 Summary of Concepts 860
35.5.2 Exercises 861
xviii Contents
35.5.3 Projects 861
36 Wrapping Up 865
36.1 What You Have Learned 865
36.2 The Possibilities/A Pet Project 866
36.2.1 General 2-D Application 866
36.2.2 3-D Application 866
36.2.3 Mobile 867
36.2.4 Web 867
36.2.5 GPGPU 867
36.2.6 AI, NLP, and Machine Learning 867
36.2.7 Open-Source and More 868
36.3 End of Chapter Material 868
36.3.1 Exercises 868
36.3.2 Projects 868
A Quick Preview of Java 869
A.l Hello World 869
A.2 Arrays, Primitives, and More 871
A.3 File Names and Interfaces 872
A.4 Constructors and ^Override 874
A.5 Generics and Polymorphism 875
A.6 Lacking Functional Aspects 876
A.7 Much More 876
B Advanced Scala Concepts 877
B. l abstract type Members 877
B.2 Enumerations 878
B.3 implicits 878
B.3.1 Basic implict Converstions 878
B.3.2 Rules and Limits on implicits 879
B.3.3 implicit Parameters 880
B.3.4 The Meaning of <% 880
B.4 sealed classes 880
B.5 Covariant, Contravariant, and Invariant Types 881
B.6 Extractors 883
B.6.1 unapply 883
B.6.2 unapplySeq 884
B.7 lazy Member Data 884
B.8 scala.collection.immutable.Stream 885
B.9 Nested Types 885
B.10 Self Types 887
B.ll Making Executable JAR Files 887
C Glossary 889
Bibliography 893
Index 895