Full Download Modern Programming Made Easy: Using Java, Scala, Groovy, and JavaScript 2nd Edition Adam L. Davis PDF DOCX
Full Download Modern Programming Made Easy: Using Java, Scala, Groovy, and JavaScript 2nd Edition Adam L. Davis PDF DOCX
com
https://textbookfull.com/product/modern-programming-made-
easy-using-java-scala-groovy-and-javascript-2nd-edition-
adam-l-davis/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/modern-programming-made-easy-using-
java-scala-groovy-and-javascript-second-edition-adam-l-davis/
textboxfull.com
https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-loucas/
textboxfull.com
https://textbookfull.com/product/head-first-javascript-
programming-2nd-edition-a-learners-guide-to-modern-javascript-eric-
freeman/
textboxfull.com
Head First JavaScript Programming, 2nd Edition: A
Learner’s Guide to Modern JavaScript Eric Freeman
https://textbookfull.com/product/head-first-javascript-
programming-2nd-edition-a-learners-guide-to-modern-javascript-eric-
freeman-2/
textboxfull.com
https://textbookfull.com/product/scala-programming-a-comprehensive-
beginner-s-guide-to-scala-2nd-edition-claudia-alves/
textboxfull.com
https://textbookfull.com/product/u-s-immigration-made-easy-19th-
edition-us-immigration-made-easy-ilona-bray/
textboxfull.com
Modern
Programming
Made Easy
Using Java, Scala, Groovy,
and JavaScript
—
Second Edition
—
Adam L. Davis
ITeBooksFree.com
Modern Programming
Made Easy
Using Java, Scala, Groovy,
and JavaScript
Second Edition
Adam L. Davis
ITeBooksFree.com
Modern Programming Made Easy: Using Java, Scala, Groovy, and
JavaScript
Adam L. Davis
Oviedo, FL, USA
ITeBooksFree.com
Dedicated to all teachers.
Thank you for teaching!
ITeBooksFree.com
Table of Contents
About the Authorࣿ﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿xv
About the Technical Reviewerࣿ﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿xvii
Chapter 1: Introduction������������������������������������������������������������������������1
Problem-Solving ���������������������������������������������������������������������������������������������������1
About This Book ����������������������������������������������������������������������������������������������������2
ITeBooksFree.com
Table of Con en s
Chapter 4: Mathࣿ﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿17
Adding, Subtracting, etc �������������������������������������������������������������������������������������17
More Complex Math��������������������������������������������������������������������������������������������19
Random Numbers �����������������������������������������������������������������������������������������������20
Summary ������������������������������������������������������������������������������������������������������������22
vi
Table of Con en s
Chapter 7: Methodsࣿ﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿41
Call Me ����������������������������������������������������������������������������������������������������������������41
Non-Java �������������������������������������������������������������������������������������������������������42
Break It Down �����������������������������������������������������������������������������������������������������43
Return to Sender�������������������������������������������������������������������������������������������������43
Static�������������������������������������������������������������������������������������������������������������������44
Varargs����������������������������������������������������������������������������������������������������������������45
Main Method �������������������������������������������������������������������������������������������������������45
Exercises�������������������������������������������������������������������������������������������������������������46
Summary ������������������������������������������������������������������������������������������������������������46
Chapter 8: Inheritanceࣿ﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿47
Objectify��������������������������������������������������������������������������������������������������������������48
JavaScript������������������������������������������������������������������������������������������������������49
Parenting 101������������������������������������������������������������������������������������������������������49
JavaScript������������������������������������������������������������������������������������������������������51
Packages ������������������������������������������������������������������������������������������������������������52
Public Parts���������������������������������������������������������������������������������������������������������53
JavaScript������������������������������������������������������������������������������������������������������53
Interfaces ������������������������������������������������������������������������������������������������������������54
Abstract Class �����������������������������������������������������������������������������������������������������55
Enums �����������������������������������������������������������������������������������������������������������������56
Annotations ���������������������������������������������������������������������������������������������������������57
Autoboxing ����������������������������������������������������������������������������������������������������������58
Autoboxing�����������������������������������������������������������������������������������������������������58
Unboxing��������������������������������������������������������������������������������������������������������58
Summary ������������������������������������������������������������������������������������������������������������58
vii
Table of Con en s
viii
Table of Con en s
Renaming a Function�������������������������������������������������������������������������������������88
Wrapping a Function in Another Function and Calling It �������������������������������88
Inline a Function Wherever It Is Called ����������������������������������������������������������89
Extract Common Code into a Function (the Opposite of the Previous) ����������89
ix
Table of Con en s
x
Table of Con en s
xi
Table of Con en s
Appendix A: Java/Groovyࣿ﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿173
No Java Analog �������������������������������������������������������������������������������������������������174
Appendix B: Java/Scalaࣿ﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿175
No Java Analog �������������������������������������������������������������������������������������������������176
Null, Nil, etc ������������������������������������������������������������������������������������������������������176
Appendix C: Java/JavaScriptࣿ﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿177
No Java Analog �������������������������������������������������������������������������������������������������178
Appendix D: Resourcesࣿ﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿179
xii
Table of Con en s
Appendix F: Javaࣿ﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿185
Afterwordࣿ﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿187
Indexࣿ﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿﷿189
xiii
About the Author
Adam L. Davis makes software. He’s spent
many years developing in Java (since Java 1.2)
and has enjoyed using Spring and Hibernate
for more than a decade. Since 2006 he’s
been using Groovy, Grails, HTML, CSS, and
JavaScript, in addition to Java, to create SaaS
web applications that help track finances for
large institutions (among other things).
Adam has a master’s and a bachelor’s
degree in Computer Science from Georgia
Tech. He is also the author of Reactive Streams in Java (Apress, 2019) and
Learning Groovy 3, Second Edition (Apress, 2019). You can check out his
web site at https://github.adamldavis.com/.
xv
About the Technical Reviewer
Manuel Jordan Elera is an autodidactic
developer and researcher who enjoys learning
new technologies for his own experiments and
creating new integrations. Manuel won the
Springy Award—Community Champion and
Spring Champion 2013. In his little free time,
he reads the Bible and composes music on his
guitar. Manuel is known as dr_pompeii. He
has tech-reviewed numerous books for Apress,
including Pro Spring Boot 2 (2019), Rapid
Java Persistence and Microservices (2019), Java Language Features (2018),
Spring Boot 2 Recipes (2018), and Java APIs, Extensions and Libraries
(2018). Read his 13 detailed tutorials about many Spring technologies,
contact him through his blog at www.manueljordanelera.blogspot.com,
and follow him on his Twitter account, @dr_pompeii.
xvii
CHAPTER 1
Introduction
In my experience, learning how to program (in typical computer science
classes) can be very difficult. The curriculum tends to be boring, abstract,
and unattached to “real-world” coding. Owing to how fast technology
progresses, computer science classes tend to teach material that is very
quickly out of date and out of touch. I believe that teaching programming
could be much simpler, and I hope this book achieves that goal.
Problem-Solving
Before you learn to program, the task can seem rather daunting, much like
looking at a mountain before you climb it. However, over time, you will
realize that programming is really about problem-solving.
On your journey toward learning to code, as with so much in life, you
will encounter many obstacles. You may have heard it before, but it really is
true: the path to success is to try, try, and try again. People who persevere
the most tend to be the most successful people.
Programming is fraught with trial and error. Although things will get
easier over time, you’ll never be right all the time. So, much as with most
things in life, you must be patient, diligent, and curious to be successful.
Info Text styled this way usually refers the curious reader to
additional information.
2
CHAPTER 2
Software to Install
Before you begin to program, you must install some basic tools.
Java/Groovy
For Java and Groovy, you will have to install the following:
1
https://adoptopenjdk.net/installation.html
© Adam L. Davis 2020 3
A. L. Davis, Modern Programming Made Easy,
https://doi.org/10.1007/978-1-4842-5569-8_2
Ch慰ter 2 S潦t睡re t I湳t ll
Trying It Out
After installing Groovy, you should use it to try coding. Open a command
prompt (or terminal), type groovyConsole, and hit Enter to begin.
Because most Java code is valid Groovy code, you should keep the
Groovy console open and use it to try out all of the examples from this
book.
You can also easily try out JavaScript in the following way:
2
https://netbeans.apache.org/download/index.html
3
https://groovy.apache.org/download.html
4
Ch慰ter 2 S潦t睡re t I湳t ll
Others
Once you have the preceding installed, you should eventually install the
following:
• Scala4: An object-oriented language built on the JVM
• Git5: A version control program
Code on GitHub
A lot of the code from this book is available on github.com/modernprog.7
You can go there at any time to follow along with the book.
4
www.scala-lang.org/
5
https://git-scm.com/
6
https://maven.apache.org/
7
https://github.com/modernprog
5
CHAPTER 3
The Basics
In this chapter, we’ll cover the basic syntax of Java and similar languages.
Coding Terms
Source file refers to human-readable code. Binary file refers to computer-
readable code (the compiled code). In Java, this binary code is called
bytecode which is read by the Java Virtual Machine (JVM).
In Java, the source files end with .java, and binary files end with
.class (also called class files). You compile source files using a compiler,
which gives you binary files or bytecode.
In Java, the compiler is called javac; in Groovy it is groovyc; and it is
scalac in Scala (see a trend here?). All three of these languages can be
compiled to bytecode and run on the JVM. The bytecode is a common
format regardless of which programming language it was generated from.
However, some languages, such as JavaScript, don’t have to be
compiled. These are called interpreted languages. JavaScript can run in
your browser (such as Firefox or Google Chrome), or it can run on a server
using Node.js, a JavaScript runtime built on Chrome’s V8 JavaScript engine.
1
A bit is the smallest possible amount of information. It corresponds to a 1 or 0.
2
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.
html
8
Chap er 3 The Ba i捳
Groovy types are much the same as Java’s. n Scala, everything is an object,
so primitives don’t exist. owever, they are replaced with corresponding value
types (Int, Long, etc.). JavaScript has only one type of number, Number,
which is similar to Java’s float.
Strings/Declarations
A String is a list of characters (text). It is a very useful built-in class in Java
(and most languages). To define a string, you simply surround some text in
quotes. For example:
9
Chap er 3 The Ba i捳
In Java, you must put the type of the variable in the declaration. That’s
why the first word here is String.
In Groovy and JavaScript, strings can also be surrounded by single
quotes ('hello'). Also, declaring variables is different in each language.
Groovy allows you to use the keyword def, while JavaScript and Scala use
var. Java 10 also introduced using var to define local variables. For example:
Statements
Almost every statement in Java must end in a semicolon (;). In many
other languages, such as Scala, Groovy, and JavaScript, the semicolon is
optional, but in Java, it is necessary. Much as how periods at the end of
each sentence help you to understand the written word, the semicolon
helps the compiler understand the code.
By convention, we usually put each statement on its own line, but this
is not required, as long as semicolons are used to separate each statement.
Assignment
Assignment is an extremely important concept to understand, but it can
be difficult for beginners. However, once you understand it, you will forget
how hard it was to learn.
Let’s start with a metaphor. Imagine you want to hide something
valuable, such as a gold coin. You put it in a safe place and write the
address on a piece of paper. This paper is like a reference to the gold. You
can pass it around and even make copies of it, but the gold remains in the
same place and does not get copied. On the other hand, anyone with the
reference to the gold can get to it. This is how a reference variable works.
10
Chap er 3 The Ba i捳
After running the preceding code, gold and a refer to the string "Au",
while b refers to "Br".
1ꂠpackage com.example.mpme;
2ꂠpublic classꂠSmallClassꂠ{
3ꂠ}
11
Chap er 3 The Ba i捳
Create a Class
1ꂠpackageꂠcom.example.mpme;
2ꂠpublicꂠclassꂠSmallClassꂠ{
3ꂠꂠꂠString name; //field
4ꂠꂠꂠString getName() {returnꂠname;} //getter
5ꂠꂠꂠvoid print() {System.out.println(name);} //method
6ꂠ}
12
Chap er 3 The Ba i捳
This method changes the name field, prints out the new value, and
then returns that value. Try this new method out in the groovyConsole by
defining the class and then executing the following:
Groovy Classes
Groovy is extremely similar to Java but always defaults to public (we will
cover what public means in a later chapter).
1ꂠpackage com.example.mpme;
2ꂠclass SmallClass {
3ꂠꂠꂠString name //property
4ꂠꂠꂠdef print() { println(name) } //method
5ꂠ}
13
Chap er 3 The Ba i捳
Groovy also automatically gives you “getter” and “setter” methods for
properties, so writing the getName method would have been redundant.
JavaScript Prototypes
Although JavaScript has objects, it doesn’t have a class keyword (prior to
ECMAScript 2015). Instead, it uses a concept called prototype. For example,
creating a class can look like the following:
1ꂠfunction SmallClass() {}
2ꂠSmallClass.prototype.name = "name"
3ꂠꀇSmallClass.prototype.print = function() { console.log(this.
name) }
Scala Classes
Scala has a very concise syntax, which puts the properties of a class in
parentheses. Also, types come after the name and a colon. For example:
1ꂠsc = newꂠSmallClass();
14
Chap er 3 The Ba i捳
Comments
As a human, it is sometimes useful for you to leave notes in your source
code for other humans—and even for yourself, later. We call these notes
comments. You write comments thus:
Summary
In this chapter, you learned the basic concepts of programming:
• Compiling source files into binary files
• How objects are instances of classes
• Primitive types, references, and strings
• Fields, methods, and properties
• Variable assignment
• How source code comments work
15
Another Random Scribd Document
with Unrelated Content
The Project Gutenberg eBook of A lady in black
This ebook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with this
ebook or online at www.gutenberg.org. If you are not located in the
United States, you will have to check the laws of the country where
you are located before using this eBook.
Language: English
CHAPTER I.
A MYSTERY SOMEWHERE.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com