Learn to Program with Kotlin From the Basics to Projects with Text and Image Processing — Tim Lavers Learn to Program with Kotlin From the Basics to Projects with Text and Image Processing Tim Lavers Learn to Program with Kotlin: From the Basics to Projects with Text and Image Processing Tim Lavers Woonona, NSW, Australia ISBN-13 (pbk): 978-1-4842-6814-8 ISBN-13 (electronic): 978-1-4842-6815-5 https://doi.org/10.1007/978-1-4842-6815-5 Copyright © 2021 by Tim Lavers This work is subject to copyright. All rights are reserved 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. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Steve Anglin Development Editor: Matthew Moodie Coordinating Editor: Mark Powers Cover designed by eStudioCalamar Cover image by Christian Lue on Unsplash (www.unsplash.com) Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected]; for reprint, paperback, or audio rights, please e-mail [email protected]. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/9781484268148. For more detailed information, please visit http://www.apress.com/source- code. Printed on acid-free paper Table of Contents About the Author �����������������������������������������������������������������������������������������������������xi About the Technical Reviewer �������������������������������������������������������������������������������xiii Preface ��������������������������������������������������������������������������������������������������������������������xv Part I: Basics ��������������������������������������������������������������������������������������������������1 Chapter 1: Getting Started ����������������������������������������������������������������������������������������3 1.1 What Is Programming? ............................................................................................................3 1.2 Installing Java .........................................................................................................................4 1.3 Installing Git ............................................................................................................................4 1.4 Installing IntelliJ ......................................................................................................................5 1.5 Our First Program ....................................................................................................................5 1.6 Changing the Appearance of IntelliJ .......................................................................................7 1.7 Troubleshooting .......................................................................................................................8 1.8 Running Our First Program .....................................................................................................8 1.9 Source Code for Our Program .................................................................................................9 Chapter 2: Simple Patterns �������������������������������������������������������������������������������������13 2.1 Shades of Gray ......................................................................................................................13 2.2 Changing the Pattern ............................................................................................................15 2.3 Solutions to Challenges ........................................................................................................18 Chapter 3: Arrays and Loops ����������������������������������������������������������������������������������21 3.1 Array Indexes ........................................................................................................................21 3.2 Loops ....................................................................................................................................24 3.3 Nested Loops ........................................................................................................................27 3.4 Summary and Solutions to Challenges .................................................................................28 iii Table of ConTenTs Chapter 4: Binary Choices ��������������������������������������������������������������������������������������33 4.1 If-Else Statements ................................................................................................................33 4.2 The Or Operator .....................................................................................................................37 4.3 The And Operator ..................................................................................................................40 4.4 If-Else-If Statements .............................................................................................................41 4.5 Summary and Solutions to Challenges .................................................................................44 Chapter 5: Integers �������������������������������������������������������������������������������������������������49 5.1 Addition, Subtraction, and Multiplication ..............................................................................49 5.2 Division .................................................................................................................................54 5.3 Making Patterns Using Arithmetic ........................................................................................55 5.4 Summary and Solutions to Challenges .................................................................................59 Chapter 6: Values and Variables �����������������������������������������������������������������������������65 6.1 Using vars ............................................................................................................................65 6.2 Using vals ............................................................................................................................67 6.3 Scope ....................................................................................................................................69 6.4 Summary and Solutions to Challenges .................................................................................71 Chapter 7: Strings ���������������������������������������������������������������������������������������������������75 7.1 Strings as Objects .................................................................................................................75 7.2 String Iteration ...................................................................................................................78 7.3 Building New Strings .........................................................................................................80 7.4 More on String Iteration .....................................................................................................82 7.5 Summary and Solutions to Challenges .................................................................................84 Chapter 8: Data Structures �������������������������������������������������������������������������������������91 8.1 Lists ......................................................................................................................................91 8.2 Sets .......................................................................................................................................93 8.3 Maps .....................................................................................................................................94 8.4 null Objects .........................................................................................................................95 8.5 Summary and Solutions to Challenges .................................................................................99 iv Table of ConTenTs Chapter 9: The File System �����������������������������������������������������������������������������������101 9.1 Reading ...............................................................................................................................102 9.2 Writing .................................................................................................................................104 9.3 Summary and Solutions to Challenges ...............................................................................106 Part II: Text �������������������������������������������������������������������������������������������������109 Chapter 10: Project Austen �����������������������������������������������������������������������������������111 10.1 Object-Oriented Programming ..........................................................................................111 10.2 Unit Tests...........................................................................................................................112 10.3 Project Structure and Setup ..............................................................................................112 10.4 LineTest and Line ..............................................................................................................114 10.5 Further Tests of Line ........................................................................................................122 10.6 HistogramTest and Histogram ...........................................................................................125 10.7 BookTest and Book ...........................................................................................................133 10.8 Back to LineTest and Line ............................................................................................138 10.9 Testing with Real Data ......................................................................................................142 10.10 Almost Finished ...............................................................................................................143 10.11 Counting the Words .........................................................................................................148 10.12 Putting Things in Order ...................................................................................................150 10.13 Taking Things Further ......................................................................................................155 10.14 Summary.........................................................................................................................156 Chapter 11: Anagrams ������������������������������������������������������������������������������������������157 11.1 Main Classes .....................................................................................................................157 11.2 The Dictionary Class .....................................................................................................159 11.3 The Term Class .................................................................................................................164 11.4 Permutations .....................................................................................................................165 11.5 The permutations Function ...........................................................................................165 11.6 G enerating the Permutations of a Term ............................................................................169 11.7 Putting It All Together ........................................................................................................180 11.8 Summary...........................................................................................................................183 v Table of ConTenTs Chapter 12: Palindromes ��������������������������������������������������������������������������������������185 12.1 Reversing a Term ..............................................................................................................185 12.2 Detecting Palindromes ......................................................................................................189 12.3 Putting It All Together ........................................................................................................190 12.4 Summary...........................................................................................................................193 Chapter 13: Word Switch ��������������������������������������������������������������������������������������195 13.1 The Algorithm ....................................................................................................................195 13.1.1 Generation 1 ............................................................................................................. 195 13.1.2 Generation 2 ............................................................................................................. 196 13.1.3 Generation 3 ............................................................................................................. 196 13.1.4 Generation 4 ............................................................................................................. 196 13.1.5 Algorithm Termination with Success ........................................................................196 13.1.6 Algorithm Termination with Failure...........................................................................197 13.2 Main Classes and Project Setup .......................................................................................197 13.3 The WordChecker Class ...................................................................................................198 13.4 The WordNode Class .........................................................................................................201 13.5 Refactoring WordNodeTest..............................................................................................203 13.6 Further Tests of WordNode ...............................................................................................205 13.7 Implementing WordNode ..................................................................................................206 13.8 The WordSwitch Class .....................................................................................................208 13.9 The Implementation of lookForTarget ..........................................................................212 13.10 Finding the Path ..............................................................................................................215 13.11 Putting It All Together ......................................................................................................221 13.12 Summary and Step Details .............................................................................................222 13.12.1 D etails of Project Step 13.1 .................................................................................... 222 13.12.2 Details of Project Step 13.2 .................................................................................... 222 13.12.3 Details of Project Step 13.3 .................................................................................... 223 13.12.4 Details of Project Step 13.4 .................................................................................... 223 13.12.5 Details of Project Step 13.5 .................................................................................... 224 13.12.6 Details of Project Step 13.6 .................................................................................... 224 13.12.7 Details of Project Step 13.9 .................................................................................... 225 vi Table of ConTenTs 13.12.8 Details of Project Step 13.10 .................................................................................. 225 13.12.9 Details of Project Step 13.12 .................................................................................. 226 13.12.10 Details of Project Step 13.13 ................................................................................ 226 13.12.11 Details of Project Step 13.16 ................................................................................ 227 13.12.12 Details of Project Step 13.21 ................................................................................ 227 Part III: Images �������������������������������������������������������������������������������������������229 Chapter 14: Color Pictures������������������������������������������������������������������������������������231 14.1 Modeling Color ..................................................................................................................231 14.2 Modeling Pictures .............................................................................................................232 14.3 Photographs ......................................................................................................................244 14.4 Flipping an Image .............................................................................................................248 14.5 S ummary and Solutions to Challenges .............................................................................253 Chapter 15: Pixel Transformations �����������������������������������������������������������������������259 15.1 Blood Sunset .....................................................................................................................259 15.2 A Unit Test .........................................................................................................................263 15.3 Conditional Transformations .............................................................................................264 15.4 Position-Based Transformations .......................................................................................270 15.5 S ummary and Solutions to Challenges .............................................................................274 Chapter 16: Cropping and Resizing Images����������������������������������������������������������281 16.1 Cropping ............................................................................................................................281 16.2 Improving the Unit Tests ...................................................................................................286 16.3 Shrinking an Image ...........................................................................................................287 16.4 Storing Images ..................................................................................................................294 16.5 S ummary and Solutions to Challenges .............................................................................296 Chapter 17: Project Dino ���������������������������������������������������������������������������������������301 17.1 Producing the Screen ........................................................................................................302 17.2 Photographing the Dinosaur .............................................................................................303 17.3 First Attempt at Superposition ..........................................................................................306 17.4 Letting the Background Through .......................................................................................309 vii Table of ConTenTs 17.5 Hiding the Feet ..................................................................................................................313 17.6 Summary...........................................................................................................................316 Part IV: Vision ���������������������������������������������������������������������������������������������317 Chapter 18: Overview �������������������������������������������������������������������������������������������319 18.1 A Bit More Kotlin ...............................................................................................................319 18.2 Project Structure ...............................................................................................................321 18.3 Image Slicing ....................................................................................................................321 18.4 Summary and Step Details ...............................................................................................327 18.1.1 D etails of Project Step 18.1 ...................................................................................... 327 18.2.2 Details of Project Step 18.2 ...................................................................................... 328 18.3.3 Details of Project Step 18.3 ...................................................................................... 328 Chapter 19: Finding Digits ������������������������������������������������������������������������������������331 19.1 DigitFinder.........................................................................................................................331 19.2 Thresholding the Sign Images ...........................................................................................334 19.3 Slicing the Thresholded Image ..........................................................................................337 19.4 A More General Slicing Function .......................................................................................338 19.5 Filtering the Slices ............................................................................................................341 19.6 Summary and Step Details ...............................................................................................345 19.1.1 D etails of Project Step 19.1 ...................................................................................... 345 19.2.2 D etails of Project Step 19.2 ...................................................................................... 346 19.3.3 Details of Project Step 19.3 ...................................................................................... 347 Chapter 20: Parsing the Images ���������������������������������������������������������������������������349 20.1 Terminology .......................................................................................................................350 20.2 Project Structure ...............................................................................................................351 20.3 Identifying the Digit “1” ....................................................................................................351 20.4 Identifying the Digit “2” ....................................................................................................355 20.5 Identifying “5” and “7” ......................................................................................................356 20.6 Identifying “0” ...................................................................................................................357 viii Table of ConTenTs 20.7 Summary and Step Details ...............................................................................................359 20.1.1 Details of Project Step 20.1 ...................................................................................... 359 20.2.2 Details of Project Step 20.2 ...................................................................................... 360 20.3.3 Details of Project Step 20.3 ...................................................................................... 360 20.4.4 Details of Project Step 20.4 ...................................................................................... 361 20.5.5 Details of Project Step 20.5 ...................................................................................... 362 20.6.6 Details of Project Step 20.6 ...................................................................................... 362 Chapter 21: Reading Speed Signs�������������������������������������������������������������������������365 21.1 SpeedReader .....................................................................................................................365 21.2 Base 10 Numbers ..............................................................................................................367 21.3 Putting It All Together ........................................................................................................369 21.4 Summary...........................................................................................................................369 21.5 Project Steps .....................................................................................................................370 21.5.1 D etails of Project Step 21.1 ...................................................................................... 370 21.5.2 D etails of Project Step 21.3 ...................................................................................... 371 21.5.3 D etails of Project Step 21.4 ...................................................................................... 371 Index ���������������������������������������������������������������������������������������������������������������������373 ix