Learn to Program with Kotlin: From the Basics to Projects with Text and Image Processing 1st Edition Tim Lavers download pdf
Learn to Program with Kotlin: From the Basics to Projects with Text and Image Processing 1st Edition Tim Lavers download pdf
com
https://textbookfull.com/product/learn-to-program-with-
kotlin-from-the-basics-to-projects-with-text-and-image-
processing-1st-edition-tim-lavers-2/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/learn-to-program-with-kotlin-from-
the-basics-to-projects-with-text-and-image-processing-1st-edition-tim-
lavers-2/
textboxfull.com
https://textbookfull.com/product/sensor-projects-with-raspberry-pi-
internet-of-things-and-digital-image-processing-1st-edition-guillermo-
guillen/
textboxfull.com
https://textbookfull.com/product/learn-raspberry-pi-programming-with-
python-learn-to-program-on-the-worlds-most-popular-tiny-computer-
donat/
textboxfull.com
Learn to Program with Python 3: A Step-by-Step Guide to
Programming Irv Kalb
https://textbookfull.com/product/learn-to-program-with-
python-3-a-step-by-step-guide-to-programming-irv-kalb/
textboxfull.com
https://textbookfull.com/product/reactive-programming-with-kotlin-
first-edition-learn-rx-with-rxjava-rxkotlin-and-rxandroid-alex-
sullivan/
textboxfull.com
https://textbookfull.com/product/learn-to-program-with-
python-3-a-step-by-step-guide-to-programming-2nd-edition-irv-kalb/
textboxfull.com
https://textbookfull.com/product/natural-language-processing-with-
spark-nlp-learning-to-understand-text-at-scale-1st-edition-alex-
thomas/
textboxfull.com
Tim Lavers
This work is subject to copyright. All rights are solely and exclusively
licensed by the Publisher, whether the whole or part 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 or information storage and
retrieval, electronic adaptation, computer software, or by similar or
dissimilar methodology now known or hereafter developed.
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 herein or for any errors or omissions that may have
been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.
January 2021
Tim Lavers
Table of Contents
Part I: Basics
Chapter 1:Getting Started
1.1 What Is Programming?
1.2 Installing Java
1.3 Installing Git
1.4 Installing IntelliJ
1.5 Our First Program
1.6 Changing the Appearance of IntelliJ
1.7 Troubleshooting
1.8 Running Our First Program
1.9 Source Code for Our Program
Chapter 2:Simple Patterns
2.1 Shades of Gray
2.2 Changing the Pattern
2.3 Solutions to Challenges
Chapter 3:Arrays and Loops
3.1 Array Indexes
3.2 Loops
3.3 Nested Loops
3.4 Summary and Solutions to Challenges
Chapter 4:Binary Choices
4.1 If-Else Statements
4.2 The Or Operator
4.3 The And Operator
4.4 If-Else-If Statements
4.5 Summary and Solutions to Challenges
Chapter 5:Integers
5.1 Addition, Subtraction, and Multiplication
5.2 Division
5.3 Making Patterns Using Arithmetic
5.4 Summary and Solutions to Challenges
Chapter 6:Values and Variables
6.1 Using vars
6.2 Using vals
6.3 Scope
6.4 Summary and Solutions to Challenges
Chapter 7:Strings
7.1 Strings as Objects
7.2 String Iteration
7.3 Building New Strings
7.4 More on String Iteration
7.5 Summary and Solutions to Challenges
Chapter 8:Data Structures
8.1 Lists
8.2 Sets
8.3 Maps
8.4 null Objects
8.5 Summary and Solutions to Challenges
Chapter 9:The File System
9.1 Reading
9.2 Writing
9.3 Summary and Solutions to Challenges
Part II: Text
Chapter 10:Project Austen
10.1 Object-Oriented Programming
10.2 Unit Tests
10.3 Project Structure and Setup
10.4 LineTest and Line
10.5 Further Tests of Line
10.6 HistogramTest and Histogram
10.7 BookTest and Book
10.8 Back to LineTest and Line
10.9 Testing with Real Data
10.10 Almost Finished
10.11 Counting the Words
10.12 Putting Things in Order
10.13 Taking Things Further
10.14 Summary
Chapter 11:Anagrams
11.1 Main Classes
11.2 The Dictionary Class
11.3 The Term Class
11.4 Permutations
11.5 The permutations Function
11.6 Generating the Permutations of a Term
11.7 Putting It All Together
11.8 Summary
Chapter 12:Palindromes
12.1 Reversing a Term
12.2 Detecting Palindromes
12.3 Putting It All Together
12.4 Summary
Chapter 13:Word Switch
13.1 The Algorithm
13.1.1 Generation 1
13.1.2 Generation 2
13.1.3 Generation 3
13.1.4 Generation 4
13.1.5 Algorithm Termination with Success
13.1.6 Algorithm Termination with Failure
13.2 Main Classes and Project Setup
13.3 The WordChecker Class
13.4 The WordNode Class
13.5 Refactoring WordNodeTest
13.6 Further Tests of WordNode
13.7 Implementing WordNode
13.8 The WordSwitch Class
13.9 The Implementation of lookForTarget
13.10 Finding the Path
13.11 Putting It All Together
13.12 Summary and Step Details
13.12.1 Details of Project Step 13.1
13.12.2 Details of Project Step 13.2
13.12.3 Details of Project Step 13.3
13.12.4 Details of Project Step 13.4
13.12.5 Details of Project Step 13.5
13.12.6 Details of Project Step 13.6
13.12.7 Details of Project Step 13.9
13.12.8 Details of Project Step 13.10
13.12.9 Details of Project Step 13.12
13.12.10 Details of Project Step 13.13
13.12.11 Details of Project Step 13.16
13.12.12 Details of Project Step 13.21
Part III: Images
Chapter 14:Color Pictures
14.1 Modeling Color
14.2 Modeling Pictures
14.3 Photographs
14.4 Flipping an Image
14.5 Summary and Solutions to Challenges
Chapter 15:Pixel Transformations
15.1 Blood Sunset
15.2 A Unit Test
15.3 Conditional Transformations
15.4 Position-Based Transformations
15.5 Summary and Solutions to Challenges
Chapter 16:Cropping and Resizing Images
16.1 Cropping
16.2 Improving the Unit Tests
16.3 Shrinking an Image
16.4 Storing Images
16.5 Summary and Solutions to Challenges
Chapter 17:Project Dino
17.1 Producing the Screen
17.2 Photographing the Dinosaur
17.3 First Attempt at Superposition
17.4 Letting the Background Through
17.5 Hiding the Feet
17.6 Summary
Part IV: Vision
Chapter 18:Overview
18.1 A Bit More Kotlin
18.2 Project Structure
18.3 Image Slicing
18.4 Summary and Step Details
18.1.1 Details of Project Step 18.1
18.2.2 Details of Project Step 18.2
18.3.3 Details of Project Step 18.3
Chapter 19:Finding Digits
19.1 DigitFinder
19.2 Thresholding the Sign Images
19.3 Slicing the Thresholded Image
19.4 A More General Slicing Function
19.5 Filtering the Slices
19.6 Summary and Step Details
19.1.1 Details of Project Step 19.1
19.2.2 Details of Project Step 19.2
19.3.3 Details of Project Step 19.3
Chapter 20:Parsing the Images
20.1 Terminology
20.2 Project Structure
20.3 Identifying the Digit “1”
20.4 Identifying the Digit “2”
20.5 Identifying “5” and “7”
20.6 Identifying “0”
20.7 Summary and Step Details
20.1.1 Details of Project Step 20.1
20.2.2 Details of Project Step 20.2
20.3.3 Details of Project Step 20.3
20.4.4 Details of Project Step 20.4
20.5.5 Details of Project Step 20.5
20.6.6 Details of Project Step 20.6
Chapter 21:Reading Speed Signs
21.1 SpeedReader
21.2 Base 10 Numbers
21.3 Putting It All Together
21.4 Summary
21.5 Project Steps
21.5.1 Details of Project Step 21.1
21.5.2 Details of Project Step 21.3
21.5.3 Details of Project Step 21.4
Index
About the Author
Tim Lavers
has 25 years’ experience in commercial
software engineering. He has worked on
a variety of applications using many
different programming languages. He
loves learning new programming
technologies and passing that knowledge
on to his colleagues. He also taught
mathematics for several years, and from
that knows how to help people learn
difficult things. Apart from
programming, Tim enjoys running,
bushwalking, and playing the piano.
About the Technical Reviewer
Ted Hagos
is a software developer by trade; at the moment, he’s Chief Technology
Officer and Data Protection Officer of RenditionDigital International, a
software development company based out of Dublin. He wore many
hats in his 20+ years in software development, for example, team lead,
project manager, architect, and director for development. He also spent
time as a trainer for IBM Advanced Career Education, Ateneo ITI, and
Asia Pacific College. He has written Learn Android Studio 4 (2020) and
Beginning Android Games Development (2020) for Apress.
Part I
Basics
Basics
In Part I, we set up Kotlin and learn the most important features of the
language.
© The Author(s), under exclusive license to APress Media, LLC, part of Springer
Nature 2021
T. Lavers, Learn to Program with Kotlin
https://doi.org/10.1007/978-1-4842-6815-5_1
1. Getting Started
Tim Lavers1
(1) Woonona, NSW, Australia
2. Wait for the “Get from Version Control” dialog, shown in Figure 1-3,
to show.
Figure 1-4 The project in IntelliJ. This part of the IntelliJ user interface is called the
project tree. It contains the source files for our programs
The upper left-hand side of this screen has what is called the project
tree. We will use this later to locate our first program and run it. The
project tree can be shown or hidden by pressing the Alt and 1 keys at
the same time. If, for whatever reason, the project tree is not showing,
use this key combination to reveal it. If that does not work, use the
menu Windows ➤ Restore Default Layout to put things right.
1.7 Troubleshooting
If IntelliJ was installed before Git, you may get an error message about
the path to the Git executable not being found. This can generally be
fixed by setting the path within IntelliJ. To do this, choose File ➤
Settings and then select Git under the Version Control
heading, as in Figure 1-6. The Test button in the top right-hand corner
can be used to check that IntelliJ knows where Git is installed. If this
test fails, you may need to adjust the setup in IntelliJ by changing the
Path to Git executable value.
1 package lpk.basics
2
3 import javax.swing.ImageIcon
4 import javax.swing.JFrame
5 import javax.swing.SwingUtilities
6
7 fun main() {
8 SwingUtilities.invokeLater {
FirstProgram().doLaunch() }
9 }
10 class FirstProgram {
11
12 fun tileColors() : Array<Array<Int>> {
13 return arrayOf(
14 arrayOf(0, 255),
15 arrayOf(255, 0)
16 )
17 }
18
19 fun doLaunch() {
20 val frame = JFrame("Basics")
21 frame.defaultCloseOperation =
JFrame.EXIT_ON_CLOSE
22 frame.iconImage =
ImageIcon("./src/lpk/basics/icon.png").image
23 frame.add(TilePanel(tileColors()))
24 frame.pack()
25 frame.isVisible = true
26 }
27 }
Note that some of the import statements (lines 3 to 5) might not
be showing. Instead, they might appear as a collapsed code block that
can be expanded by clicking the + sign.
Figure 1-9 The import statements might be hidden as a collapsed code block
Even this short program contains a lot of detail that will be totally
incomprehensible to a first-time programmer. Don’t worry! You don’t
need to understand everything at once. The main parts of the program
can be understood in the following terms:
1. The first line tells the system what package our program belongs
in. The complete name of a program includes its package, just as the
combination of street name plus other details makes a postal
address unique.
2. The import statements let the system know what other programs
are needed in our code. All software that does anything remotely
complex, such as showing a user interface, makes use of prebuilt
components. The import statements are used to make them
available to our code.
4. Lines 19 to 26 tell the system how to turn the block of colors into a
user interface element that can be drawn on the screen.
5. The function called main on line 7 is the starting point for the
system to launch the program.
2. Simple Patterns
Tim Lavers1
(1) Woonona, NSW, Australia
In this chapter, we will see how black, white, and other shades of gray
can be represented in Kotlin. This will allow us to modify our program
from the previous chapter to show different tile patterns. In making
these changes, we will be getting familiar with basic Kotlin syntax and
with the programming environment.
A LORD OF LANDS
By Ramsey Benson.
326 pp., 12mo. $1.50.
The unusual and convincing narrative of the experiences of a man
of good sense, with wages of $50 a month and five children,
following his determination to leave the city and farm it in the
Northwest.
“A book of real adventure—an adventure in living. More
thrilling than an African jungle story, and not lacking in humor
and pathos. Nothing is more wonderful than the way the
commonest details contribute to the homely interest, just as
long ago we were fascinated by the ‘Swiss Family
Robinson.’”—The Independent.
“Does for the humble workingman what ‘The Fat of the
Land’ did for the well-to-do. Will appeal instantly and
throughout its entire length to the lover of the outdoor life.”—
Boston Transcript.
“Unique in literature ... holds many fascinations ... told with
the utmost art.”—San Francisco Chronicle.
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