Table Of ContentOBJECT-ORIENTATION,
ABSTRACTION, AND
DATA STRUCTURES
USING
SCALA
S E C O N D E D I T I O N
CHAPMAN & HALL/CRC
TEXTBOOKS IN COMPUTING
Series Editors
John Impagliazzo Andrew McGettrick
Professor Emeritus, Hofstra University Department of Computer
and Information Sciences
University of Strathclyde
Aims and Scope
This series covers traditional areas of computing, as well as related technical areas, such as
software engineering, artificial intelligence, computer engineering, information systems, and
information technology. The series will accommodate textbooks for undergraduate and gradu-
ate students, generally adhering to worldwide curriculum standards from professional societ-
ies. The editors wish to encourage new and imaginative ideas and proposals, and are keen to
help and encourage new authors. The editors welcome proposals that: provide groundbreaking
and imaginative perspectives on aspects of computing; present topics in a new and exciting
context; open up opportunities for emerging areas, such as multi-media, security, and mobile
systems; capture new developments and applications in emerging fields of computing; and
address topics that provide support for computing, such as mathematics, statistics, life and
physical sciences, and business.
Published Titles
Paul Anderson, Web 2.0 and Beyond: Principles and Technologies
Henrik Bærbak Christensen, Flexible, Reliable Software: Using Patterns and
Agile Development
John S. Conery, Explorations in Computing: An Introduction to Computer
Science
John S. Conery, Explorations in Computing: An Introduction to Computer
Science and Python Programming
Iztok Fajfar, Start Programming Using HTML, CSS, and JavaScript
Jessen Havill, Discovering Computer Science: Interdisciplinary Problems,
Principles, and Python Programming
Ted Herman, A Functional Start to Computing with Python
Pascal Hitzler, Markus Krötzsch, and Sebastian Rudolph, Foundations of
Semantic Web Technologies
Mark J. Johnson, A Concise Introduction to Data Structures using Java
Mark J. Johnson, A Concise Introduction to Programming in Python
Lisa C. Kaczmarczyk, Computers and Society: Computing for Good
Mark C. Lewis, Introduction to the Art of Programming Using Scala
Mark C. Lewis and Lisa L. Lacher, Introduction to Programming and Problem-
Solving Using Scala, Second Edition
Published Titles Continued
Mark C. Lewis and Lisa L. Lacher, Object-Orientation, Abstraction, and Data Structures Using
Scala, Second Edition
Efrem G. Mallach, Information Systems: What Every Business Student Needs to Know
Bill Manaris and Andrew R. Brown, Making Music with Computers: Creative Programming
in Python
Uvais Qidwai and C.H. Chen, Digital Image Processing: An Algorithmic Approach with
MATLAB®
David D. Riley and Kenny A. Hunt, Computational Thinking for the Modern Problem Solver
Henry M. Walker, The Tao of Computing, Second Edition
Aharon Yadin, Computer Systems Architecture
Chapman & Hall/CRC
TEXTBOOKS IN COMPUTING
OBJECT-ORIENTATION,
ABSTRACTION, AND
DATA STRUCTURES
USING
SCALA
S E C O N D E D I T I O N
Mark C. Lewis
Lisa L. Lacher
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2017 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works
Printed on acid-free paper
Version Date: 20161109
International Standard Book Number-13: 978-1-4987-3216-1 (Paperback)
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been
made to publish reliable data and information, but the author and publisher cannot assume responsibility for the valid-
ity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright
holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this
form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may
rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or uti-
lized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopy-
ing, microfilming, and recording, or in any information storage or retrieval system, without written permission from the
publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://
www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923,
978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For
organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for
identification and explanation without intent to infringe.
Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com
and the CRC Press Web site at
http://www.crcpress.com
Contents
List of Figures xvii
List of Tables xxiii
Preface xxv
1 Scala Language Basics 1
1.1 First Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Variables and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Statements and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4.1 Lambda Expressions/Closures . . . . . . . . . . . . . . . . . . . . 9
1.5 Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.5.1 Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.5.2 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.5.3 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.6 Declarations and Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.7 Essential Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.7.1 Standard Input and import Statements . . . . . . . . . . . . . . . 19
1.7.2 Basic Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.7.2.1 Array and List . . . . . . . . . . . . . . . . . . . . . . . 21
1.7.2.2 Creating Collections Using fill . . . . . . . . . . . . . . 22
1.7.2.3 Creating Collections Using tabulate . . . . . . . . . . . 22
1.7.2.4 Creating Arrays Using new . . . . . . . . . . . . . . . . . 23
1.7.2.5 Creating Lists Using Cons . . . . . . . . . . . . . . . . . 24
1.7.2.6 Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.7.2.7 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.7.2.8 Higher-Order Methods . . . . . . . . . . . . . . . . . . . 29
1.7.3 The Option Type . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1.7.4 Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
1.8 Other Language Features . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
1.8.1 Unfinished Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
1.8.2 Named Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
1.8.3 Default Parameter Values . . . . . . . . . . . . . . . . . . . . . . . 38
1.8.4 Curried Functions/Methods . . . . . . . . . . . . . . . . . . . . . . 38
1.8.5 Pass-by-Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
1.9 The Read, Evaluate, Print Loop (REPL) . . . . . . . . . . . . . . . . . . 41
1.10 Putting It Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
1.11 End of Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
1.11.1 Summary of Concepts . . . . . . . . . . . . . . . . . . . . . . . . . 43
1.11.2 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
vii
viii Contents
2 Basics of Object-Orientation and Software Development 51
2.1 The Meaning of Object-Orientation . . . . . . . . . . . . . . . . . . . . . 51
2.2 What Are classes? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
2.3 Software Development Stages . . . . . . . . . . . . . . . . . . . . . . . . . 59
2.4 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2.5 Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.6 Bank Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2.6.1 Bank Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
2.6.2 Bank Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
2.6.3 Bank Implementation . . . . . . . . . . . . . . . . . . . . . . . . . 66
2.6.4 Making an Application . . . . . . . . . . . . . . . . . . . . . . . . 72
2.7 Making Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
2.8 End of Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
2.8.1 Summary of Concepts . . . . . . . . . . . . . . . . . . . . . . . . . 77
2.8.2 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
2.8.3 Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
3 Details of Object-Orientation in Scala 81
3.1 The 2D Vector classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
3.1.1 this Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
3.2 Special Methods and Symbolic Names . . . . . . . . . . . . . . . . . . . . 84
3.2.1 Scala Naming Rules/Symbolic Names . . . . . . . . . . . . . . . . 84
3.2.1.1 Precedence and Associativity . . . . . . . . . . . . . . . . 88
3.2.2 Unary Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
3.2.3 Property Assignment Methods . . . . . . . . . . . . . . . . . . . . 89
3.2.4 The apply Method. . . . . . . . . . . . . . . . . . . . . . . . . . . 91
3.2.5 The update Method . . . . . . . . . . . . . . . . . . . . . . . . . . 92
3.3 object Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
3.3.1 Companion Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 94
3.4 Final Versions of 2D Vectors . . . . . . . . . . . . . . . . . . . . . . . . . 96
3.5 case classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
3.6 Encapsulation/Separating Interface from Implementation . . . . . . . . . 102
3.7 Revisiting the API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
3.8 import Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
3.9 End of Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
3.9.1 Summary of Concepts . . . . . . . . . . . . . . . . . . . . . . . . . 106
3.9.2 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
3.9.3 Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
4 Abstraction and Polymorphism 115
4.1 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
4.2 Inclusion Polymorphism (Inheritance and Subtyping) . . . . . . . . . . . . 118
4.2.1 Inheritance in the Scala Type System . . . . . . . . . . . . . . . . 122
4.2.2 Inheritance in Scala Code . . . . . . . . . . . . . . . . . . . . . . . 122
4.2.3 private Visibility and Inheritance . . . . . . . . . . . . . . . . . . 126
4.2.4 protected Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . 127
4.2.5 Calling Methods on the Supertype . . . . . . . . . . . . . . . . . . 127
4.2.6 Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
4.2.7 traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
4.2.8 final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
4.2.9 Method Resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Contents ix
4.2.10 Inheriting from Function Types. . . . . . . . . . . . . . . . . . . . 134
4.2.11 Enumerations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
4.3 Parametric Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
4.3.1 Parametric Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
4.3.2 Parametric Functions and Methods . . . . . . . . . . . . . . . . . 140
4.3.2.1 Parametric Sorts . . . . . . . . . . . . . . . . . . . . . . . 142
4.3.3 Type Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
4.3.3.1 Type Bounds and Sorting. . . . . . . . . . . . . . . . . . 145
4.4 End of Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
4.4.1 Summary of Concepts . . . . . . . . . . . . . . . . . . . . . . . . . 147
4.4.2 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
4.4.3 Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
5 GUIs and Graphics 149
5.1 Project Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
5.2 ScalaFX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
5.3 Drawing Program Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
5.3.1 Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
5.3.2 Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
5.3.3 Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
5.4 Drawing Program Design . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
5.5 Interactivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
5.5.1 Properties, Observables, and Bindings . . . . . . . . . . . . . . . . 166
5.6 Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
5.6.1 Shapes and Scene Graph . . . . . . . . . . . . . . . . . . . . . . . 167
5.6.2 Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
5.7 Keyboard and Mouse Input . . . . . . . . . . . . . . . . . . . . . . . . . . 174
5.8 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
5.9 Other Elements of ScalaFX . . . . . . . . . . . . . . . . . . . . . . . . . . 179
5.10 End of Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
5.10.1 Summary of Concepts . . . . . . . . . . . . . . . . . . . . . . . . . 179
5.10.2 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
5.10.3 Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
6 Other Collection Types 185
6.1 The scala.collection Packages . . . . . . . . . . . . . . . . . . . . . . . . . 185
6.1.1 scala.collection.immutable . . . . . . . . . . . . . . . . . . . . . . . 186
6.1.2 scala.collection.mutable . . . . . . . . . . . . . . . . . . . . . . . . 188
6.2 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
6.2.1 Running through Sets . . . . . . . . . . . . . . . . . . . . . . . . . 190
6.2.2 Mutable vs. Immutable . . . . . . . . . . . . . . . . . . . . . . . . 191
6.2.3 Using a Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
6.3 Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
6.3.1 Looping through a Map . . . . . . . . . . . . . . . . . . . . . . . . 195
6.3.2 Using Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
6.4 Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
6.5 Collections as Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
6.6 Project Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
6.6.1 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
6.6.2 Adding Drawables . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
6.7 End of Chapter Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Description:Praise for the first edition: "The well-written, comprehensive book…[is] aiming to become a de facto reference for the language and its features and capabilities. The pace is appropriate for beginners; programming concepts are introduced progressively through a range of examples and then used as t