Table Of ContentTexts in Computer Science
Quentin Charatan
Aaron Kans
Programming
in Two Semesters
Using Python and Java
Texts in Computer Science
Series Editors
David Gries, Department of Computer Science, Cornell University, Ithaca, NY,
USA
OritHazzan ,FacultyofEducationinTechnologyandScience,Technion—Israel
Institute of Technology, Haifa, Israel
Titles in this series now included in the Thomson Reuters Book Citation Index!
‘Texts in Computer Science’ (TCS) delivers high-quality instructional content for
undergraduates and graduates in all areas of computing and information science,
with a strong emphasis on core foundational and theoretical material but inclusive
of some prominent applications-related content. TCS books should be reasonably
self-containedandaimtoprovidestudentswithmodernandclearaccountsoftopics
ranging across the computing curriculum. As a result, the books are ideal for
semestercoursesorforindividualself-studyincaseswherepeopleneedtoexpand
their knowledge. All texts are authored by established experts in their fields,
reviewed internally and by the series editors, and provide numerous examples,
problems, and other pedagogical tools; many contain fully worked solutions.
The TCS series is comprised of high-quality, self-contained books that have
broad and comprehensive coverage and are generally in hardback format and
sometimes contain color. For undergraduate textbooks that are likely to be more
brief and modular in their approach, require only black and white, and are under
275pages,SpringerofferstheflexiblydesignedUndergraduateTopicsinComputer
Science series, to which we refer potential authors.
Quentin Charatan (cid:129) Aaron Kans
Programming in Two
Semesters
Using Python and Java
123
QuentinCharatan Aaron Kans
Schoolof Architecture, Computing Schoolof Architecture, Computing
andEngineering andEngineering
University of EastLondon University of EastLondon
London,UK London,UK
ISSN 1868-0941 ISSN 1868-095X (electronic)
Textsin Computer Science
ISBN978-3-031-01325-6 ISBN978-3-031-01326-3 (eBook)
https://doi.org/10.1007/978-3-031-01326-3
©SpringerNatureSwitzerlandAG2022
Thisworkissubjecttocopyright.AllrightsarereservedbythePublisher,whetherthewholeorpart
of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission
orinformationstorageandretrieval,electronicadaptation,computersoftware,orbysimilarordissimilar
methodologynowknownorhereafterdeveloped.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this
publicationdoesnotimply,evenintheabsenceofaspecificstatement,thatsuchnamesareexemptfrom
therelevantprotectivelawsandregulationsandthereforefreeforgeneraluse.
The publisher, the authors and the editors are safe to assume that the advice and information in this
book are believed to be true and accurate at the date of publication. Neither the publisher nor the
authors or the editors give a warranty, expressed or implied, with respect to the material contained
hereinorforanyerrorsoromissionsthatmayhavebeenmade.Thepublisherremainsneutralwithregard
tojurisdictionalclaimsinpublishedmapsandinstitutionalaffiliations.
ThisSpringerimprintispublishedbytheregisteredcompanySpringerNatureSwitzerlandAG
Theregisteredcompanyaddressis:Gewerbestrasse11,6330Cham,Switzerland
In loving memory of Jimmy Glass
(1951–2021) and Nigel Maudsley
(1953–2021)
—Quentin Charatan
In loving memory of our beloved Brigadier
Yashpal Mohan (1953–2021)
—Aaron Kans
Preface
Accompanying website: Go to the publisher’s website, https://link.springer.com,
and search for this textbook. From that page you will be directed to a link that
allows you to download the resources you need for this book.
This book is designed for university students taking a first module in software
developmentorprogramming,followedbyasecond,moreadvancedmodule.This
book takes a unique approach of covering these topics using two popular pro-
gramming languages—Python and Java. Python is the vehicle for the teaching of
fundamental programming concepts that would align with the first introductory
programming module and Java as the vehicle for teaching more advanced pro-
gramming concepts that would align with the second more advanced module.
Design concepts are explained throughout using the UML notation. The topics are
taught from first principles and assume no prior knowledge of the subject.
Thebookisorganizedsoastosupporttwotwelve-week,one-semestermodules,
whichmighttypicallycompriseatwo-hourlecture,aone-hourseminarandaoneor
two-hourlaboratorysession.Theoutcomesatthestartofeachchapterhighlightsits
key learning objectives, the self-test questions at the end of each chapter (which
wouldbeidealasthebasisforseminarsessions)ensurethatthelearningobjectives
forthatchapterhavebeenmet,whiletheprogrammingexercisesthatfollow(which
wouldbeidealforlaboratorysessions)allowtheselearningobjectivestobeapplied
tocompletedifferentprograms.Inadditiontotheseexercisesandquestions,acase
studyisdevelopedineachsemestertoillustratetheuseofthetechniquescoveredin
the text to develop a non-trivial application. Lecturers who teach on modules that
run for fewer than twelve weeks in a semester could treat these case studies as a
self-directed student learning experience, rather than as taught topics.
Theapproachtakeninthisbookisidealforallstudents,includingthoseentering
university with little or no background in the subject matter, perhaps coming from
pre-degree courses in other disciplines, or perhaps returning to study after long
periodsawayfromformaleducation.Itistheauthors’experiencethatsuchstudents
have enormous difficulties in grasping the fundamental programming concepts the
first-time round and therefore require a simpler and gentler introduction to the
subject than is presented in most standard texts.
vii
viii Preface
The book takes an integrated approach to software development by covering
suchtopicsasbasicdesignprinciplesandstandards,testingmethodologiesandthe
user interface, as well as looking at detailed implementation topics.
Inthefirstsemester,considerabletimeisspentconcentratingonthefundamental
programming concepts such as declarations of variables and basic control struc-
tures,functionsandcollections,priortointroducingstudentstoclassesandobjects,
inheritance, file handling, graphics and event-driven programming. Python is
chosen as the vehicle to teach these first semester topics as it is a programming
language very well suited to students who have had no prior programming expe-
rience. Instructions are also provided on how to download and use the Python
interpreter for writing and running Python programs.
Since Java is often found to be challenging programming language for students
completelynewtoprogrammingJavaischoseninsteadasthevehicletocovermore
advanced second semester topics (such as, arrays, interfaces and lambda expres-
sions, exceptions, collection classes, advanced graphics using JavaFX and pack-
ages). A smooth transition is provided from Python to Java by revisiting some
fundamentalconceptssuchasvariables,controlstructures,filehandlingandclasses
and objects in Java and highlighting differences and similarities between the two
languages.
The accompanying website contains all the code from the text book and
instructions for using some common development tools, while the book itself
includes an appendix that contains a guide to the various ways of writing and
running Java programs.
We would like to thank our publisher, Springer, for the encouragement and
guidance that we have received throughout the production of this book. Addi-
tionally, we would especially like to thank the Computing students of the
UniversityofEastLondonfortheirthoughtfulcommentsandfeedback.Forsupport
and inspiration, special thanks are due once again to our families and friends.
London, UK Quentin Charatan
Aaron Kans
Contents
Part I Semester One: Python
1 Python: The First Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Software. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Downloading and Installing the Interpreter . . . . . . . . . . . . . . . 8
1.5 Your First Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.6 Adding Comments to a Program . . . . . . . . . . . . . . . . . . . . . . 12
1.7 Self-test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.8 Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2 Python: Building Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2 Output in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3 An Interactive Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4 Variables and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.5 Arithmetic Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.6 Expressions in Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.7 More About Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.8 Input in Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.9 Formatting Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.10 Type Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.11 Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.12 Self-test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.13 Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3 Python Control Structures: Selection . . . . . . . . . . . . . . . . . . . . . . . 39
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.2 Making Choices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.3 The ‘if’ Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.3.1 Comparison Operators. . . . . . . . . . . . . . . . . . . . . . . 42
3.3.2 Multiple Instructions Within an ‘if’ Statement . . . . . 43
ix
x Contents
3.4 The ‘if…else’ Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.5 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.6 Nested ‘if…else’ Statements . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.7 Self-test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.8 Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4 Python Control Structures: Iteration . . . . . . . . . . . . . . . . . . . . . . . 55
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.2 The ‘for’ Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
4.2.1 Varying the Loop Counter. . . . . . . . . . . . . . . . . . . . 59
4.2.2 The Body of the Loop . . . . . . . . . . . . . . . . . . . . . . 60
4.2.3 Revisiting the Loop Counter . . . . . . . . . . . . . . . . . . 63
4.3 The ‘while’ Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
4.4 The ‘break’ Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
4.5 The ‘continue’ Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
4.6 A Couple of Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
4.7 The ‘try … except’ Statement . . . . . . . . . . . . . . . . . . . . . . . . 71
4.8 Self-test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
4.9 Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
5 Python: Functions and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
5.2 Declaring and Defining Functions . . . . . . . . . . . . . . . . . . . . . 80
5.3 Calling a Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
5.4 Function Input and Output. . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.5 More Examples of Functions. . . . . . . . . . . . . . . . . . . . . . . . . 86
5.6 Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
5.7 Global Variables: The Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . 90
5.8 Using Functions in Menu-Driven Programs . . . . . . . . . . . . . . 91
5.9 Built-In Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
5.10 String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
5.11 Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
5.12 Self-test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
5.13 Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
6 Python Collections: Lists and Tuples . . . . . . . . . . . . . . . . . . . . . . . 105
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
6.2 Creating Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
6.3 Accessing List Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
6.4 Passing Lists as Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . 112
6.5 Returning a List from a Function. . . . . . . . . . . . . . . . . . . . . . 116
6.6 More List Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
6.7 Queues and Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
6.8 Creating a List Using Comprehension . . . . . . . . . . . . . . . . . . 125