Learn to Program with Kotlin: From the Basics to Projects with Text and Image Processing 1st Edition Tim Lavers 2024 scribd download
Learn to Program with Kotlin: From the Basics to Projects with Text and Image Processing 1st Edition Tim Lavers 2024 scribd download
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.
The elder Disraeli has argued that Raleigh could not possibly have
written the whole of that large tome, “The History of the World,”
himself, for want of books of reference whilst in the Tower. But as his
friends supplied him with books, and he himself had probably taken
copious notes for the work while living in the old home of the
Desmonds at Youghal, in Ireland, where a remnant of the old
Desmond library is still existing, the argument can scarcely be
considered proved. The late Sir John Pope Hennessy has pointed
out in his work on “Raleigh in Ireland,” that, by an odd coincidence,
the son of the sixteenth Earl of Desmond, whose lands Raleigh held
in Ireland, was a fellow-prisoner of Sir Walter’s in the Tower during
his first imprisonment in the fortress during Elizabeth’s reign.
Desmond died in prison in 1608, and was buried in St Peter’s
Chapel. Raleigh had this youth’s sad fate in his mind, it seems, when
he wrote from the Tower, “Wee shall be judged as we judge—and be
dealt withal as wee deal with others in this life, if wee believe God
Himself.”
An almost contemporary historian, Sir Richard Baker, refers to
Raleigh’s imprisonment in the following quaint manner:—“He was
kept in the Tower, where he had great honour; he spent his time in
writing, and had been a happy man if he had never been released.”
A strange description, surely, of what is generally understood by the
term, “happy man.”
Henry, Prince of Wales, seems to have been the only member of
his family who appreciated Sir Walter, frequently visiting him at the
Tower. On one of the occasions when he had left him, the young
prince remarked to one of his following that no king except his father
could keep such a bird in such a cage. The Prince’s mother, Queen
Anne, seems also to have shown some interest in Raleigh’s fate, and
to have tried to induce her miserable husband to set him free.
Arabella Stuart.
(From a Contemporary Miniature.)
In 1611 Arabella Stuart was brought a prisoner into the Tower, and
with her, Lady Shrewsbury. When the news of Arabella’s marriage
with young William Seymour reached the King, her fate was sealed,
for by this marriage the half-captivity in which she had lived was
changed into captivity for life; and few of James the First’s evil
actions, and they were not a few, were more mean or cowardly than
his treatment of his poor kinswoman, Arabella Hertford.
She had never been known to mix in politics, and if she had any
ambition, it was the noble ambition of wishing to lead a pure life
away from an infamous court. Poor Arabella used to declare that
although she was often asked to marry some foreign prince, nothing
on earth would induce her to marry any man whom she did not know,
or for whom she had no liking.
At Christmastide of 1609, James, hearing a rumour that seemed to
point to Arabella being married to some foreign prince, had sent her
to the Tower, releasing her when he discovered that his fears were
groundless, and giving his consent to her marrying one of his
subjects should she wish to do so. Unfortunately, Arabella took
advantage of the King’s consent, trusting to his word, but she found
to her bitter cost how hollow and false that promise was. In the
following February (1610) she plighted her troth to William Seymour,
both probably relying upon the Royal word. Whether James had
forgotten that Seymour was a probable suitor for Arabella’s hand
when he gave his promise cannot be known, but Arabella could not
have made a more unlucky choice, as far as she herself was
concerned, for the Suffolk claims had been recognised by Act of
Parliament; and the same Parliament which had acknowledged
James the First could not alter the order of succession, and,
consequently, William Seymour being the grandson of Lord Hertford,
by his wife, Catharine Grey, was in what was called the “Suffolk
Succession.” His marriage to Arabella brought her still nearer to the
Crown, and any children born of the marriage would have had a
good chance of succeeding to the throne.
The young couple were summoned to appear before the Council,
and were charged to give up all thoughts of marriage. But, in spite of
King and Council, they were secretly married in the month of May
1611—a month said to be unlucky for marriages. Two months
afterwards the news reached the King, and the storm burst over the
unlucky lovers. Arabella was sent a prisoner to Lambeth Palace, and
her husband to the Tower. From Lambeth Arabella was first removed
to the house of Mr Conyers at Highgate, and thence she was to be
sent to Durham Castle in charge of the Bishop. At Highgate,
however, she fell ill, or pretended to fall ill, and the famous attempt
made to escape by herself and her husband took place.
By some means she procured a disguise in the shape of a wig and
male attire, with long, yellow riding-boots and a rapier, and thus
accoutred, on the 4th of June she rode to Blackwall, where she had
hoped to find her husband, but, failing in this, she rowed with a
female attendant and a Mr Markham, who had accompanied her
from Highgate, to a French vessel lying near Leigh, which took them
on board. Seymour, also disguised, escaped from the Tower by
following a cart laden with wooden billets. He got away unperceived,
and managed to reach a boat waiting for him by the wharf at the Iron
Gate, but, on arriving at Leigh, they found the French ship, with
Arabella on board, had put out to sea. The weather was against the
ship in which Seymour was sailing making Calais, and he had to go
on to Ostend, where he disembarked.
Lady Arabella Seymour.
Sweet brother
every one forſakes me but those that cannot helpe me.
Your most unfortunate ſister
Arbella Seymaure
Her Autograph from the Original in the Possession of
John Thane.
Meanwhile, a hue and cry rang out from London. King’s
messengers galloped in hot haste from Whitehall to Deptford, and
orders arrived at all the southern ports to search all ships and barks
that might contain the runaways; a proclamation was issued to arrest
the principals and the abettors of their flight. A ship of war was sent
over to Calais, and others were despatched along the French coast
as far as Flanders to intercept the fugitives. When half-way across
the Channel, one of these vessels, named the Adventurer, came in
sight of a ship crowding on all sail in order to reach Calais; the wind,
meanwhile, had dropped, and further flight was impossible. A boat
was lowered from the Adventurer, the crew who manned it being
armed to the teeth. A few shots were exchanged, and the flying
vessel, which proved to be French, was boarded, and the poor
runaway was taken back to the English man-of-war; on board of her
Arabella was made a prisoner, and as a prisoner was landed at the
Tower, never to leave it again until her luckless body was taken from
it for burial at Westminster.
James made as much ado about this attempted escape of the
Hertfords as if he had discovered a second Gunpowder Plot. And not
only did he have all those who had been concerned in Arabella’s
flight seized and imprisoned in the Tower, but kept the Countess of
Shrewsbury and the Earl strict prisoners in their house, and ordered
the old Earl of Hertford to appear before him.
From all appearances William Seymour showed a lack of courage
at this time, not unlike the husband of Lady Catherine Seymour in
the last reign, for he remained abroad while the storm with all its fury
fell and crushed his young wife. Poor Arabella lingered on in her
prison till death released her from her troubles on the 25th of
September 1615. She had been kept both in the Belfry Tower and in
the Lieutenant’s House, but had lost her reason some time previous
to her final release both from durance and the world. Her body was
taken in the dead of night to Westminster Abbey, and placed below
the coffin of Mary Queen of Scots. Mickle, the author of “Cumnor
Hall,” and “There’s nae luck about the house,” is credited with having
written the touching ballad on Arabella Stuart, which is included in
Evans’s “Old Ballads.”
“Where London’s Tower its turrets shew,
So stately by old Thames’s side,
Fair Arabella, child of woe,
For many a day had sat and sighed.
And as she heard the waves arise,
And as she heard the black wind roar,
As fast did heave her heartfelt sighs,
And still so fast her tears did pour.”
“... that nor the good might be defrauded, nor the great so cured;
But both might know their ways are understood,
And the reward and punishment assured.”
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