Introduction to Programming Using Java free on line 1st Edition David J. Eck ([email protected]) 2024 scribd download
Introduction to Programming Using Java free on line 1st Edition David J. Eck ([email protected]) 2024 scribd download
com
https://ebookmeta.com/product/introduction-to-programming-
using-java-free-on-line-1st-edition-david-j-eck-eckhws-edu/
OR CLICK BUTTON
DOWNLOAD NOW
https://ebookmeta.com/product/mathematical-computing-an-introduction-
to-programming-using-maple-betounes-david-redfern-mylan/
ebookmeta.com
https://ebookmeta.com/product/guide-to-java-a-concise-introduction-to-
programming-2nd-edition-james-t-streib/
ebookmeta.com
https://ebookmeta.com/product/java-an-introduction-to-problem-solving-
and-programming-7th-edition-walter-savith/
ebookmeta.com
https://ebookmeta.com/product/a-primer-on-scientific-programming-with-
python-texts-in-computational-science-and-engineering-6-langtangen/
ebookmeta.com
Insight Guides Tanzania Zanzibar 4th Edition Insight Guies
https://ebookmeta.com/product/insight-guides-tanzania-zanzibar-4th-
edition-insight-guies/
ebookmeta.com
https://ebookmeta.com/product/safe-haven-grove-city-omegas-book-1-1st-
edition-jarica-james/
ebookmeta.com
https://ebookmeta.com/product/fictional-portrayals-of-spain-s-
transition-to-democracy-transitional-fantasies-1st-edition-anne-l-
walsh/
ebookmeta.com
The Good Immigrants How the Yellow Peril Became the Model
Minority Madeline Y. Hsu
https://ebookmeta.com/product/the-good-immigrants-how-the-yellow-
peril-became-the-model-minority-madeline-y-hsu/
ebookmeta.com
https://ebookmeta.com/product/introduction-to-advertising-
understanding-and-managing-the-advertising-process-1st-edition-
emmanuel-mogaji/
ebookmeta.com
Nature-based Solutions and Water Security: An Action
Agenda for the 21st Century 1st Edition Jan Cassin
https://ebookmeta.com/product/nature-based-solutions-and-water-
security-an-action-agenda-for-the-21st-century-1st-edition-jan-cassin/
ebookmeta.com
Introduction to Programming Using Java
Version 9, JavaFX Edition
May, 2022
David J. Eck
Hobart and William Smith Colleges
Preface xi
i
CONTENTS ii
3 Control 75
3.1 Blocks, Loops, and Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.1.1 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.1.2 The Basic While Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
3.1.3 The Basic If Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
3.1.4 Control Abstractiont . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
3.1.5 Definite Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3.2 Algorithm Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
3.2.1 Pseudocode and Stepwise Refinement . . . . . . . . . . . . . . . . . . . . 83
3.2.2 The 3N+1 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
3.2.3 Coding, Testing, Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 89
3.3 while and do..while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
3.3.1 The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
3.3.2 The do..while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
3.3.3 break and continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
3.4 The for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
3.4.1 For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
3.4.2 Example: Counting Divisors . . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.4.3 Nested for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
3.5 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
3.5.1 The Dangling else Problem . . . . . . . . . . . . . . . . . . . . . . . . . . 106
3.5.2 Multiway Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
3.5.3 If Statement Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
3.5.4 The Empty Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
3.6 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
3.6.1 The Basic switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 113
3.6.2 Menus and switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . 115
3.6.3 Enums in switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . 116
3.6.4 Definite Assignment and switch Statements . . . . . . . . . . . . . . . . . 117
3.6.5 Switch Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
3.6.6 The Traditional switch Statement . . . . . . . . . . . . . . . . . . . . . . 118
3.7 Exceptions and try..catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
CONTENTS iii
4 Subroutines 147
4.1 Black Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
4.2 Static Subroutines and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
4.2.1 Subroutine Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
4.2.2 Calling Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
4.2.3 Subroutines in Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
4.2.4 Member Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
4.3 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
4.3.1 Using Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
4.3.2 Formal and Actual Parameters . . . . . . . . . . . . . . . . . . . . . . . . 159
4.3.3 Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
4.3.4 Subroutine Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
4.3.5 Array Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
4.3.6 Command-line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
4.3.7 Throwing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
4.3.8 Global and Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 166
4.4 Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
4.4.1 The return statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
4.4.2 Function Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
4.4.3 3N+1 Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
4.5 Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
4.5.1 First-class Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
4.5.2 Functional Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
4.5.3 Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
4.5.4 Method References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
4.6 APIs, Packages, Modules, and Javadoc . . . . . . . . . . . . . . . . . . . . . . . . 178
4.6.1 Toolboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
4.6.2 Java’s Standard Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
4.6.3 Using Classes from Packages . . . . . . . . . . . . . . . . . . . . . . . . . 181
4.6.4 About Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
4.6.5 Javadoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
CONTENTS iv
Glossary 753
Preface
xi
Preface xii
∗ ∗ ∗
Java currently has two major approaches to Graphical User Interface programming: JavaFX
and Swing. This edition of the textbook uses JavaFX, but there is an alternative edition
that uses Swing. The main differences are in Chapters 6 and 13, which are devoted to GUI
programming, but GUI programs and GUI-related material in other chapters also use JavaFX
exclusively. For the Swing edition, much of the GUI material is taken from Version 7 of this
textbook, with some updating and modification.
Swing is a standard part of Java. JavaFX was introduced as a more modern approach to
GUI programming, but it must be downloaded and installed separately from Java itself, which
makes it more complicated to use. Swing and JavaFX can both be used to write complex, fully
functional GUI programs, and either one is a reasonable choice. Version 8 of this textbook used
JavaFX. The alternative edition that uses Swing has been added for Version 9.
GUI programming was never included in the textbook as an end in itself, and it would take
another textbook to cover the topic in its entirety. I cover GUI because it is a great example of
object-oriented programming, it lets me introduce event-driven programs, and it lets students
literally see the effect of the code that they write. JavaFX and Swing both offer good support
for all of those purposes.
∗ ∗ ∗
Version 8 of this textbook originally covered Java 8, but minor updates of that version added
notes about new features in Java 9 through Java 16. Version 9 of the book covers Java 17. The
main change from Version 8 is the addition of the Swing edition. A section on records has been
added to Chapter 7. Many examples have been modified to use text blocks and the new switch
statement syntax. Some references to the general idea of “abstraction” have been added, such
as a short subsection on control abstraction in Section 3.1. A short subsection on final classes
and methods has been added to Section 5.5. There are also small corrections and modifications
throughout.
The majority of this textbook is valid for Java 8, and the book does not cover features
added after Java 8 in as much detail. Note that only Java 8, 11, and 17 are “long-term
support” releases. When I introduce a feature that requires Java 11 or Java 17, I will make
note of that fact. However, I will never refer to any of the non-long-term-support releases.
∗ ∗ ∗
The first version of the book was written in 1996, and there have been several versions since
then. Version 9 will be the last version. All editions are archived (at least until my retirement
in December 2022 and hopefully beyond) at the following Web addresses:
• First edition: https://math.hws.edu/eck/cs124/javanotes1/ (Covers Java 1.0.)
• Second edition: https://math.hws.edu/eck/cs124/javanotes2/ (Covers Java 1.1.)
• Third edition: https://math.hws.edu/eck/cs124/javanotes3/ (Covers Java 1.1.)
• Fourth edition: https://math.hws.edu/eck/cs124/javanotes4/ (Covers Java 1.4.)
• Fifth edition: https://math.hws.edu/eck/cs124/javanotes5/ (Covers Java 5.0.)
• Sixth edition: https://math.hws.edu/eck/cs124/javanotes6/ (Covers Java 5.0, with a bit
of 6.0.)
• Seventh edition: https://math.hws.edu/eck/cs124/javanotes7/ (Covers Java 7.)
• Eighth edition: https://math.hws.edu/eck/cs124/javanotes8/ (Covers Java 8, plus a bit
of 11 and 17.)
Preface xiii
Grève-tori
Mutta tämä merkki oli ollut niin väläyksellinen, että Favras jäi
epätietoiseksi, mitä se merkitsi.
Ovi aukeni.
»Isä», sanoi Favras, »suokaa anteeksi, etten minä voi avata teille
muuta kuin sydämeni. Nämä herrat ovat järjestäneet niin, että voin
teille avata vain sen.»
»Ettekö voi irroittaa tuomitun käsiä siksi ajaksi, jonka hän viipyy
parissani?» kysyi kirkkoherra.
»Se ei ole meidän vallassamme», vastasi oikeudenpalvelija.
Favras nousi lujin askelin rattaille. Hän istahti sille puolelle, mihin
tuohus oli kiinnitetty, sillä hän oivalsi, että tuohus oli nimenomaan
häntä varten.
Uteliaitten tulva oli niin voimakas, että rattaitten oli pakko tuon
tuostakin pysähtyä ja ne pääsivät etenemään vain kun vartiosto oli
raivannut niille tien, jonka heti taas sulki kansanvuo särkien sen tielle
nousseen heikon padon.
Matkaa jatkettiin.
Mustiin puettu nuori mies kääntyi äkkiä; nämä sanat oli lausunut
muuan hallikantaja, jonka kasvoja hän ei voinut nähdä, koska mies
oli lauseen lopetettuaan painanut hattunsa leveän lieren silmilleen.
Nyt oltiin ihan varmoja, ettei vanki ollut karannut eikä enää
karkaisikaan.
»Se johtuu siitä, että kreivi de Horn oli murhaaja, mutta sinä olet
marttyyri», virkkoi muuan ääni.
»Hyppää, markiisi!»
Tuskin hän oli lausunut nämä sanat, kun teloittaja sysäsi häntä ja
hänen ruumiinsa alkoi häälyä ilmassa.
Kuninkuus on pelastettu
»Herra Weber?»
Jos hän olisi kuunnellut tarkasti, kun kreivi sivuutti hänet, olisi hän
varmaankin kuullut, kuinka sydän löi tuossa leveässä rinnassa.
Kuningatar jatkoi: