100% found this document useful (1 vote)
14 views

Object Oriented Programming with Swift 2 1st Edition Gastón C Hillar - The ebook in PDF format with all chapters is ready for download

The document provides information about the book 'Object-Oriented Programming with Swift 2' by Gastón C. Hillar, which covers essential concepts of object-oriented programming using the Swift language. It includes chapters on real-world object representation, class structures, data encapsulation, inheritance, protocols, and maximizing code reuse. Additionally, it offers links to various other eBooks available for instant download on ebookmeta.com.

Uploaded by

saqibavryl
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
14 views

Object Oriented Programming with Swift 2 1st Edition Gastón C Hillar - The ebook in PDF format with all chapters is ready for download

The document provides information about the book 'Object-Oriented Programming with Swift 2' by Gastón C. Hillar, which covers essential concepts of object-oriented programming using the Swift language. It includes chapters on real-world object representation, class structures, data encapsulation, inheritance, protocols, and maximizing code reuse. Additionally, it offers links to various other eBooks available for instant download on ebookmeta.com.

Uploaded by

saqibavryl
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Read Anytime Anywhere Easy Ebook Downloads at ebookmeta.

com

Object Oriented Programming with Swift 2 1st


Edition Gastón C Hillar

https://ebookmeta.com/product/object-oriented-programming-
with-swift-2-1st-edition-gaston-c-hillar/

OR CLICK HERE

DOWLOAD EBOOK

Visit and Get More Ebook Downloads Instantly at https://ebookmeta.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Real-Time C++: Efficient Object-Oriented and Template


Microcontroller Programming Christopher Kormanyos

https://ebookmeta.com/product/real-time-c-efficient-object-oriented-
and-template-microcontroller-programming-christopher-kormanyos/

ebookmeta.com

Python 3 Object oriented Programming Building robust and


maintainable software with object oriented design patterns
in Python 2nd Edition Phillips
https://ebookmeta.com/product/python-3-object-oriented-programming-
building-robust-and-maintainable-software-with-object-oriented-design-
patterns-in-python-2nd-edition-phillips/
ebookmeta.com

Swift 3 Protocol Oriented Programming Hoffman Jon

https://ebookmeta.com/product/swift-3-protocol-oriented-programming-
hoffman-jon/

ebookmeta.com

Dominant Desire Decadent Sins 2 1st Edition Linzi Basset

https://ebookmeta.com/product/dominant-desire-decadent-sins-2-1st-
edition-linzi-basset/

ebookmeta.com
The Portrait and the Book Illustration and Literary
Culture in Early America 1st Edition Walsh

https://ebookmeta.com/product/the-portrait-and-the-book-illustration-
and-literary-culture-in-early-america-1st-edition-walsh/

ebookmeta.com

Practical Channel-Aware Resource Allocation: With MATLAB


and Python Code Michael Ghorbanzadeh

https://ebookmeta.com/product/practical-channel-aware-resource-
allocation-with-matlab-and-python-code-michael-ghorbanzadeh/

ebookmeta.com

Adsorption: Fundamental Processes and Applications (Volume


33) (Interface Science and Technology, Volume 33) 1st
Edition Mehrorang Ghaedi
https://ebookmeta.com/product/adsorption-fundamental-processes-and-
applications-volume-33-interface-science-and-technology-volume-33-1st-
edition-mehrorang-ghaedi/
ebookmeta.com

The Tiger s Mail Order Bride Shifter Mail Order Bride


Agency 5 1st Edition Ruby Knoxx

https://ebookmeta.com/product/the-tiger-s-mail-order-bride-shifter-
mail-order-bride-agency-5-1st-edition-ruby-knoxx/

ebookmeta.com

Sapientia Naturae: The Wisdom Of Nature Cory Edmund


Endrulat

https://ebookmeta.com/product/sapientia-naturae-the-wisdom-of-nature-
cory-edmund-endrulat/

ebookmeta.com
Physically Based Rendering fourth edition From Theory to
Implementation Matt Pharr

https://ebookmeta.com/product/physically-based-rendering-fourth-
edition-from-theory-to-implementation-matt-pharr/

ebookmeta.com
Table of Contents
Object–Oriented Programming with Swift 2
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
eBooks, discount offers, and more
Questions
1. Objects from the Real World to Playground
Installing the required software
Capturing objects from the real world
Generating classes to create objects
Recognizing variables and constants to create properties
Recognizing actions to create methods
Organizing classes with UML diagrams
Working with API objects in the Xcode Playground
Exercises
Test your knowledge
Summary
2. Structures, Classes, and Instances
Understanding structures, classes, and instances
Understanding initialization and its customization
Understanding deinitialization and its customization
Understanding automatic reference counting
Declaring classes
Customizing initialization
Customizing deinitialization
Creating the instances of classes
Exercises
Test your knowledge
Summary
3. Encapsulation of Data with Properties
Understanding the elements that compose a class
Declaring stored properties
Generating computed properties with setters and getters
Combining setters, getters, and a related property
Understanding property observers
Transforming values with setters and getters
Using type properties to create values shared by all the instances of a
class
Creating mutable classes
Building immutable classes
Exercises
Test your knowledge
Summary
4. Inheritance, Abstraction, and Specialization
Creating class hierarchies to abstract and specialize behavior
Understanding inheritance
Declaring classes that inherit from another class
Overriding and overloading methods
Overriding properties
Controlling whether subclasses can or cannot override members
Working with typecasting and polymorphism
Taking advantage of operator overloading
Declaring operator functions for specific subclasses
Exercises
Test your knowledge
Summary
5. Contract Programming with Protocols
Understanding how protocols work in combination with classes
Declaring protocols
Declaring classes that adopt protocols
Taking advantage of the multiple inheritance of protocols
Combining inheritance and protocols
Working with methods that receive protocols as arguments
Downcasting with protocols and classes
Treating instances of a protocol type as a different subclass
Specifying requirements for properties
Specifying requirements for methods
Combining class inheritance with protocol inheritance
Exercises
Test your knowledge
Summary
6. Maximization of Code Reuse with Generic Code
Understanding parametric polymorphism and generic code
Declaring a protocol to be used as a constraint
Declaring a class that conforms to multiple protocols
Declaring subclasses that inherit the conformance to protocols
Declaring a class that works with a constrained generic type
Using a generic class for multiple types
Combining initializer requirements in protocols with generic types
Declaring associated types in protocols
Creating shortcuts with subscripts
Declaring a class that works with two constrained generic types
Using a generic class with two generic type parameters
Inheriting and adding associated types in protocols
Generalizing existing classes with generics
Extending base types to conform to custom protocols
Test your knowledge
Exercises
Summary
7. Object-Oriented Programming and Functional Programming
Refactoring code to take advantage of object-oriented programming
Understanding functions as first-class citizens
Working with function types within classes
Creating a functional version of array filtering
Writing equivalent closures with simplified code
Creating a data repository with generics and protocols
Filtering arrays with complex conditions
Using map to transform values
Combining map with reduce
Chaining filter, map, and reduce
Solving algorithms with reduce
Exercises
Test your knowledge
Summary
8. Extending and Building Object-Oriented Code
Putting together all the pieces of the object-oriented puzzle
Adding methods with extensions
Adding computed properties to a base type with extensions
Declaring new convenience initializers with extensions
Defining subscripts with extensions
Working with object-oriented code in apps
Adding an object-oriented data repository to a project
Interacting with an object-oriented data repository through Picker View
Exercises
Test your knowledge
Summary
A. Exercise Answers
Chapter 1, Objects from the Real World to Playground
Chapter 2, Structures, Classes, and Instances
Chapter 3, Encapsulation of Data with Properties
Chapter 4, Inheritance, Abstraction, and Specialization
Chapter 5, Contract Programming with Protocols
Chapter 6, Maximization of Code Reuse with Generic Code
Chapter 7, Object-Oriented Programming and Functional Programming
Chapter 8, Extending and Building Object-Oriented Code
Index
Object–Oriented
Programming with Swift 2
Object–Oriented
Programming with Swift 2
Copyright © 2016 Packt Publishing All rights reserved.
No part of this book may be reproduced, stored in a
retrieval system, or transmitted in any form or by any
means, without the prior written permission of the
publisher, except in the case of brief quotations
embedded in critical articles or reviews.

Every effort has been made in the preparation of this


book to ensure the accuracy of the information
presented. However, the information contained in this
book is sold without warranty, either express or implied.
Neither the author, nor Packt Publishing, and its dealers
and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by
this book.

Packt Publishing has endeavored to provide trademark


information about all of the companies and products
mentioned in this book by the appropriate use of
capitals. However, Packt Publishing cannot guarantee
the accuracy of this information.

First published: January 2016

Production reference: 1220116


Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78588-569-3

www.packtpub.com
Credits
Author

Gastón C. Hillar Reviewers

Vinod Madigeri

Hugo Solis

Commissioning Editor

Amarabha Banerjee Acquisition Editors

Nadeem Bagban

Reshma Raman

Content Development Editor

Divij Kotian

Technical Editor

Parag Topre

Copy Editor

Shruti Iyer
Project Coordinator

Nikhil Nair

Proofreader

Safis Editing

Indexer

Monica Ajmera Mehta Graphics

Disha Haria

Production Coordinator

Nilesh Mohite

Cover Work

Nilesh Mohite
About the Author
Gastón C. Hillar is an Italian and has been working with
computers since he was 8 years old. In the early 80s, he
began programming with the legendary Texas TI-99/4A
and Commodore 64 home computers. Gaston has a
bachelor's degree in computer science and graduated
with honors. He also holds an MBA in which he
graduated with an outstanding thesis. At present, Gaston
is an independent IT consultant and a freelance author
who is always looking for new adventures around the
world.

He has been a senior contributing editor at Dr. Dobb's


and has written more than a hundred articles on software
development topics. Gatson was also a former Microsoft
MVP in technical computing. He has received the
prestigious Intel® Black Belt Software Developer award
seven times.

He is a guest blogger at Intel® Software Network


(http://software.intel.com). You can reach him at
<gastonhillar@hotmail.com> and follow him on
Twitter at http://twitter.com/gastonhillar. Gastón's blog is
http://csharpmulticore.blogspot.com.

He lives with his wife, Vanesa, and his two sons, Kevin
and Brandon.
About the Reviewers
Vinod Madigeri is a curious developer with a particular
interest in object-oriented programming. He has worked
in different industries (telecommunication, game
technologies, and consumer electronics) writing software
in C, C++, Objective-C, Swift, and C#.

Vinod is a passionate software engineer who writes code


for fun. He has been doing this professionally for some 6
years and had been goofing with computers for 10 years
before that.

Hugo Solis is an assistant professor in the physics


department at University of Costa Rica. His current
research interests are computational cosmology,
complexity, and the influence of hydrogen on material
properties. Hugo has wide experience with languages
such as C/C++ and Python for scientific programming
and visualization. He is a member of Free Software
Foundation and has contributed code to a few free
software projects. Hugo has also been a technical
reviewer for Mastering Object-Oriented Python, Learning
Object-Oriented Programming and Kivy: Interactive
Applications in Python and the author of Kivy Cookbook,
Packt Publishing. Currently, he is in charge of IFT, a
Costa Rican scientific nonprofit organization for the
multidisciplinary practice of physics (http://iftucr.org).
I'd like to thank my beloved mother, Katty Sanchez, for her support and vanguard thoughts.
www.PacktPub.com
Support files, eBooks,
discount offers, and more
For support files and downloads related to your book,
please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every


book published, with PDF and ePub files available? You
can upgrade to the eBook version at www.PacktPub.com
and as a print book customer, you are entitled to a
discount on the eBook copy. Get in touch with us at
<service@packtpub.com> for more details.

At www.PacktPub.com, you can also read a collection of


free technical articles, sign up for a range of free
newsletters and receive exclusive discounts and offers
on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions?


PacktLib is Packt's online digital book library. Here, you
can search, access, and read Packt's entire library of
books.

Why subscribe?
Fully searchable across every book published by Packt

Copy and paste, print, and bookmark content

On demand and accessible via a web browser

Free access for Packt account


holders
If you have an account with Packt at
www.PacktPub.com, you can use this to access
PacktLib today and view 9 entirely free books. Simply
use your login credentials for immediate access.

To my sons, Kevin and Brandon, and my wife, Vanesa


Preface
Object-oriented programming, also known as OOP, is a
required skill in any modern software developer job. It
makes a lot of sense because object-oriented
programming allows you to maximize code reuse and
minimize maintenance costs. However, learning object-
oriented programming is challenging because it includes
too many abstract concepts that require real-life
examples to be easy to understand. In addition, object-
oriented code that doesn't follow best practices can
easily become a maintenance nightmare.

Swift is a multi-paradigm programming language, and


one of its most important paradigms is OOP. If you want
to create great applications and apps for Mac, iPhone,
iPad, Apple TV, and Apple Watch, you need to master
OOP in Swift. In addition, as Swift also grabs the nice
features found in functional programming languages, it is
convenient to know how to mix OOP code with functional
programming code.

This book will allow you to develop high-quality reusable


object-oriented code in Swift 2.2. You will learn the
object-oriented programming principles and how Swift
implements them. You will learn how to capture objects
from real-world elements and create object-oriented
code that represents them. You will understand Swift's
approach towards object-oriented code. You will
maximize code reuse and reduce maintenance costs.
Your code will be easy to understand, and it will work
with representations of real-life elements.
What this book covers
Chapter 1, Objects from the Real World to Playground,
teaches you the principles of object-oriented paradigms.
We will discuss how real-world objects can become part
of the fundamental elements of code. We will translate
elements into the different components of the object-
oriented paradigm supported in Swift: classes, protocols,
properties, methods, and instances.

Chapter 2, Structures, Classes, and Instances, starts


generating blueprints to create objects. You will learn
about an object's life cycle, and we will work with many
examples to understand how object initializers and
deinitializers work.

Chapter 3, Encapsulation of Data with Properties,


introduces you to organizing data in the blueprints that
generate objects. We will understand the different
members of a class and how they are reflected by
members of the instances generated from a class. You
will learn the difference between mutable and immutable
classes.

Chapter 4, Inheritance, Abstraction, and Specialization,


introduces you to creating a hierarchy of blueprints that
generate objects. We will take advantage of inheritance
and many related features to specialize behavior.
Chapter 5, Contract Programming with Protocols,
discusses how Swift works with protocols in combination
with classes. We will declare and combine multiple
blueprints to generate a single instance. We will declare
protocols with different types of requirements, and then
we will create classes that conform to these protocols.

Chapter 6, Maximization of Code Reuse with Generic


Code, teaches you how to maximize code reuse by
writing code capable of working with objects of different
types—that is, instances of classes that conform to
specific protocols or whose class hierarchy includes
specific superclasses. We will work with protocols and
generics.

Chapter 7, Object-Oriented Programming and Functional


Programming, teaches you how to refactor existing code
to take full advantage of object-oriented code. We will
prepare the code for future requirements, reduce
maintenance costs, and maximize code reuse. We will
also work with many functional programming features
included in Swift combined with object-oriented
programming.

Chapter 8, Extending and Building Object-Oriented


Code, puts together all the pieces of the object-oriented
puzzle. We will take advantage of extensions to add
features to types, classes, and protocols in which we
don't have access to the source code. We will make sure
that the code exposes only the things that it has to
expose, and you will learn how everything you learned
about object-oriented programming is useful in any kind
of app we might create.
What you need for this book
In order to work with Xcode and the Swift Playground,
you will need a Mac OS computer capable of running OS
X 10.10.5 or later with 8 GB of RAM.

In order to work with the Swift open source version on


the Linux platform, you will need any computer capable
of running Ubuntu 14.04 or later or Ubuntu 15.10 or later.
These are the Linux distributions where the Swift open
source binaries are built and tested. It is also possible to
run the Swift compiler and utilities on other Linux
distributions. You must take a look at the latest available
documentation at the Swift open source website,
https://swift.org.
Who this book is for
If you are an IOS developer who has a basic idea of
OOP and want to incorporate OOP concepts with Swift
to optimize your application's performance, then this
book is for you. This is a very useful resource for
developers who want to shift from Objective C, C#, Java,
Python, JavaScript, or other object-oriented languages to
Swift.
Conventions
In this book, you will find a number of text styles that
distinguish between different kinds of information. Here
are some examples of these styles and an explanation of
their meaning.

Code words in text, database table names, folder


names, filenames, file extensions, pathnames, dummy
URLs, user input, and Twitter handles are shown as
follows: "We can assign 20 to
regularHexagon1.lengthOfSide and 50 to
regularHexagon2.lengthOfSide."

A block of code is set as follows:

let degCUnitFromStr = HKUnit(fromString:


"degC")
let degFUnitFromStr = HKUnit(fromString:
"degF")

When we wish to draw your attention to a particular part


of a code block, the relevant lines or items are set in
bold:

Animal created
Mammal created
DomesticMammal created
Dog created
TerrierDog created
SmoothFoxTerrier created
I am 7 years old.
I am 14 years old.
I am 21 years old.
I am 4 years old.
I am 5 years old.

New terms and important words are shown in bold.


Words that you see on the screen, for example, in
menus or dialog boxes, appear in the text like this: "Start
Xcode, navigate to File | New | Playground…."

NOTE
Warnings or important notes appear in a box like this.

TIP
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us
know what you think about this book—what you liked or
disliked. Reader feedback is important for us as it helps
us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail


<feedback@packtpub.com>, and mention the book's
title in the subject of your message.

If there is a topic that you have expertise in and you are


interested in either writing or contributing to a book, see
our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we
have a number of things to help you to get the most from
your purchase.

Downloading the example code


You can download the example code files from your
account at http://www.packtpub.com for all the Packt
Publishing books you have purchased. If you purchased
this book elsewhere, you can visit
http://www.packtpub.com/support and register to have
the files emailed directly to you.

Errata
Although we have taken every care to ensure the
accuracy of our content, mistakes do happen. If you find
a mistake in one of our books—maybe a mistake in the
text or the code—we would be grateful if you could
report this to us. By doing so, you can save other
readers from frustration and help us improve subsequent
versions of this book. If you find any errata, please report
them by visiting http://www.packtpub.com/submit-errata,
selecting your book, clicking on the Errata Submission
Form link, and entering the details of your errata. Once
your errata are verified, your submission will be accepted
and the errata will be uploaded to our website or added
to any list of existing errata under the Errata section of
that title.

To view the previously submitted errata, go to


https://www.packtpub.com/books/content/support and
enter the name of the book in the search field. The
required information will appear under the Errata
section.

Piracy
Piracy of copyrighted material on the Internet is an
ongoing problem across all media. At Packt, we take the
protection of our copyright and licenses very seriously. If
you come across any illegal copies of our works in any
form on the Internet, please provide us with the location
address or website name immediately so that we can
pursue a remedy.

Please contact us at <copyright@packtpub.com>


with a link to the suspected pirated material.

We appreciate your help in protecting our authors and


our ability to bring you valuable content.

eBooks, discount offers, and more


Did you know that Packt offers eBook versions of every
book published, with PDF and ePub files available? You
can upgrade to the eBook version at www.PacktPub.com
and as a print book customer, you are entitled to a
discount on the eBook copy. Get in touch with us at
<customercare@packtpub.com> for more details.

At www.PacktPub.com, you can also read a collection of


free technical articles, sign up for a range of free
newsletters, and receive exclusive discounts and offers
on Packt books and eBooks.

Questions
If you have a problem with any aspect of this book, you
can contact us at <questions@packtpub.com>, and
we will do our best to address the problem.
Chapter 1. Objects from the
Real World to Playground
Whenever you have to solve a problem in the real world,
you use elements and interact with them. For example,
when you are thirsty, you take a glass, you fill it up with
water, soda, or your favorite juice, and then you drink.
Similarly, you can easily recognize elements, known as
objects, from real-world actions and then translate them
into object-oriented code. In this chapter, we will start
learning the principles of object-oriented programming to
use them in the Swift programming language to develop
apps and applications.

Installing the required


software
In this book, you will learn to take advantage of all the
object-oriented features included in the Swift
programming language version 2.2. Most of the
examples are compatible with previous Swift versions,
such as 1.0, 1.1, 1.2, 2.0, and 2.1, but it is convenient to
use Swift 2.0 or later because it has added error-
handling features and many performance and stability
improvements as compared to its predecessors.

We will use Xcode as our IDE (Integrated


Development Environment). All the examples work
with Xcode version 7 or higher. The latest versions of the
IDE include Swift 2.2 as one of the supported
programming languages to build iOS apps, watchOS
apps, and Mac OS X applications. It is important to note
that Xcode only runs on Mac OS X, and all the
instructions provided in this chapter consider that we are
running this operating system on a Mac computer.
However, after Apple launched Swift 2.2, it made the
language open source and added a port to Linux. Thus,
we can apply everything we learn about object-oriented
programming with Swift when targeting other platforms
to which the language is ported.

TIP
In case you want to work with the Swift open source release, you can download the latest
development snapshot in the Downloads section at http://swift.org. You can run all the
code examples included in the book in the Swift Read Evaluate Print Loop command-line
environment instead of working with Xcode Playground. The Swift Read Evaluate Print
Loop command-line environment is also known as Swift REPL.

In order to install Xcode, you just need to launch Mac


App Store, enter Xcode in the search box, click on the
Xcode application icon shown in the results, and make
sure that it is the application developed by Apple and not
an Xcode helper application. The following screenshot
shows the details of the Xcode application in Mac App
Store:
Then, click on GET and wait until Mac App Store
downloads Xcode. Note that it is necessary to download
a few GBs, and therefore, it may take some time to finish
the download process. Once the download is finished,
click on INSTALL APP and follow the necessary steps to
complete the application's installation process. Finally,
you will be able to launch the Xcode application as you
would execute any other application in your Mac OS X
operating system.

Apple usually launches Xcode beta versions before


releasing the final stable versions. It is highly
recommended to avoid working with beta versions to test
the examples included in the book because beta
versions are unstable, and some examples might crash
or generate unexpected outputs. Mac App Store only
offers the latest stable version of Xcode, and therefore,
there is no risk of installing a beta version by mistake
when following the previously explained steps.

In case we have any Xcode beta version installed on the


same computer in which we will run the book samples,
we have to make sure that the configuration for the
stable Xcode version uses the appropriate command-line
tools. We won't work with the command-line tools, but
we will take advantage of Playground, and this feature
uses the command-line tools under the hood.

Launch Xcode and navigate to Xcode | Preferences…


and click on Locations. Make sure that the Command
Line Tools drop-down menu displays the stable Xcode
version that you installed as the selected option. The
following screenshot shows Xcode 7.2 (7C68) as the
selected version for Command Line Tools:
TIP
We don't need an iOS Developer Program membership to run the examples included in this
book. However, in case we want to distribute the apps or applications coded in Swift to any
App Store or activate certain capabilities in Xcode, we will require an active membership.

We don't need any previous experience with the Swift


programming language to work with the examples in the book
and learn how to model and create object-oriented code with
Swift. If we have some experience with Objective-C, Java, C#,
Python, Ruby, or JavaScript, we will be able to easily learn
Swift's syntax and understand the examples. Swift borrows
many features from these and other modern programming
languages, and therefore, any knowledge of these languages
will be extremely useful.
Capturing objects from the
real world
Now, let's forget about Xcode and Swift for a while.
Imagine that we have to develop a new universal iOS
app that targets the iPad, iPhone, and iPod touch
devices. We will have different UIs (User Interfaces)
and UXs (User eXperiences) because these devices
have diverse screen sizes and resolutions. However, no
matter the device in which the app runs, it will have the
same goal.

Imagine that Vanessa is a very popular YouTuber,


painter, and craftswoman who usually uploads videos on
a YouTube channel. She has more than a million
followers, and one of her latest videos had a huge
impact on social networking sites. In this video, she
sketched basic shapes and then painted them with
acrylic paint to build patterns. She worked with very
attractive colors, and many famous Hollywood actresses
uploaded pictures on Instagram sharing their creations
with the technique demonstrated by Vanessa and with
the revolutionary special colors developed by a specific
acrylic paint manufacturer.

Obviously, the acrylic paint manufacturer wants to take


full advantage of this situation, so he specifies the
requirements for an app. The app must provide a set of
predefined 2D shapes that the user can drag and drop in
a document to build a pattern so that he/she can change
both the 2D position and size. It is important to note that
the shapes cannot intersect, and users cannot change
the line widths because they are the basic requirements
of the technique introduced by Vanessa. A user can
select the desired line and fill colors for each shape. At
any time, the user can tap a button, and the app must
display a list of the acrylic paint tubes, bottles, or jars
that the user must buy to paint the drawn pattern. Finally,
the user can easily place an online order to request the
suggested acrylic paint tubes, bottles, or jars. The app
also generates a tutorial to explain to the user how to
generate each of the final colors for the lines and fills by
thinning the appropriate amount of acrylic paint with
water, based on the colors that the user has specified.

The following image shows an example of a pattern.


Note that it is extremely simple to describe the objects
that compose the pattern: four 2D shapes—specifically,
two rectangles and two circles. If we measure the
shapes, we would easily realize that they aren't two
squares and two ellipses; they are two rectangles and
two circles.
We can easily recognize objects; we understand that the
pattern is composed of many 2D geometric shapes.
Now, let's focus on the core requirement for the app,
which is calculating the required amounts of acrylic paint.
We have to take into account the following data for each
shape included in the pattern in order to calculate the
amount of acrylic paint:

The perimeter

The area

The line color

The fill color


The app allows users to use a specific color for the line
that draws the borders of each shape. Thus, we have to
calculate the perimeter in order to use it as one of the
values that will allow us to estimate the amount of acrylic
paint that the user must buy to paint each shape's
border. Then, we have to calculate the area to use it as
one of the values that will allow us to estimate the
amount of acrylic paint that the user must buy to fill each
shape's area.

We have to start working on the backend code that


calculates areas and perimeters. The app will follow
Vanessa's guidelines to create the patterns, and it will
only support the following six shapes:

Squares

Equilateral triangles

Rectangles

Circles

Ellipses

Regular hexagons

We can start writing Swift code—specifically, six


functions that calculate the areas of the previously
enumerated shapes and another six to calculate their
perimeters. Note that we are talking about functions, and
we stopped thinking about objects; therefore, we will
face some problems with this path, which we will solve
with an object-oriented approach from scratch.

For example, if we start thinking about functions to solve


the problem, one possible solution is to code the
following 12 functions to do the job:

calculateSquareArea

calculateEquilateralTriangleArea

calculateRectangleArea

calculateCircleArea

calculateEllipseArea

calculateRegularHexagonArea

calculateSquarePerimeter

calculateEquilateralTrianglePerimeter

calculateRectanglePerimeter

calculateCirclePerimeter

calculateEllipsePerimeter

calculateRegularHexagonPerimeter

Each of the previously enumerated functions has to


receive the necessary parameters of each shape and
return either its calculated area or perimeter.

Now, let's forget about functions for a bit. Let's recognize


the real-world objects from the application's
requirements that we were assigned. We have to
calculate the areas and perimeters of six elements,
which are six nouns in the requirements that represent
real-life objects—specifically 2D shapes. Our list of real-
world objects is exactly the same that Vanessa's
specification uses to determine the shapes allowed to be
used to create patterns. Take a look at the list:
Squares

Equilateral triangles

Rectangles

Circles

Ellipses

Regular hexagons

After recognizing the real-life objects, we can start


designing our application by following an object-oriented
paradigm. Instead of creating a set of functions that
perform the required tasks, we can create software
objects that represent the state and behavior of a
square, equilateral triangle, rectangle, circle, ellipse, and
regular hexagon. This way, the different objects mimic
the real-world 2D shapes. We can work with the objects
to specify the different attributes required to calculate the
area and perimeter. Then, we can extend these objects
to include the additional data required to calculate other
required values, such as the quantity of acrylic paint
required to paint the borders.

Now, let's move to the real world and think about each of
the previously enumerated six shapes. Imagine that we
have to draw each of the shapes on paper and calculate
their areas and perimeters. After we draw each shape,
which values will we use to calculate their areas and
perimeters? Which formulas will we use?

TIP
We started working on an object-oriented design before we started coding, and therefore,
we will work as if we didn't know many concepts of geometry. For example, we can easily
generalize the formulas that we use to calculate the perimeters and areas of regular
polygons. However, we will analyze the requirements in most cases; we still aren't experts
on the subject, and we need to dive deeper into the subject before we can group classes
and generalize their behavior.

The following figure shows a drawn square and the


formulas that we will use to calculate the perimeter and
area. We just need the length of side value, usually
identified as a.

The following figure shows a drawn equilateral triangle


and the formulas that we will use to calculate the
perimeter and area. This type of triangle has equal sides,
and the three internal angles are equal to 60 degrees.
We just need the length of side value, usually identified
as a.
The following figure shows a drawn rectangle and the
formulas that we will use to calculate the perimeter and
area. We need the width and height values.
Another Random Document on
Scribd Without Any Related Topics
The Project Gutenberg eBook of The chronicles
of Enguerrand de Monstrelet, Vol. 07 [of 13]
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.

Title: The chronicles of Enguerrand de Monstrelet, Vol. 07 [of 13]


containing an account of the cruel civil wars between the
houses of Orleans and Burgundy, of the possession of Paris
and Normandy by the English, their expulsion thence, and
of other memorable events that happened in the kingdom
of France, as well as in other countries

Author: Enguerrand de Monstrelet

Translator: Thomas Johnes

Release date: October 12, 2018 [eBook #58083]

Language: English

Credits: Produced by Graeme Mackreth andThe Online Distributed


Proofreading Team at http://www.pgdp.net (This file was
produced from images generously made available by The
Internet Archive)

*** START OF THE PROJECT GUTENBERG EBOOK THE CHRONICLES


OF ENGUERRAND DE MONSTRELET, VOL. 07 [OF 13] ***
THE

CHRONICLES
OF

ENGUERRAND DE MONSTRELET.

H. Bryer, Printer, Bridge Street, Blackfriars, London.


THE

CHRONICLES
OF

ENGUERRAND DE MONSTRELET;
CONTAINING

AN ACCOUNT OF THE CRUEL CIVIL WARS BETWEEN THE HOUSES


OF

ORLEANS AND BURGUNDY;


OF THE POSSESSION OF

PARIS AND NORMANDY BY THE ENGLISH;


THEIR EXPULSION THENCE;

AND OF OTHER

MEMORABLE EVENTS THAT HAPPENED IN THE KINGDOM OF


FRANCE,

AS WELL AS IN OTHER COUNTRIES.

A HISTORY OF FAIR EXAMPLE, AND OF GREAT PROFIT TO THE

FRENCH,
Beginning at the Year MCCCC. where that of Sir JOHN FROISSART finishes, and
ending
at the Year MCCCCLXVII. and continued by others to the Year MDXVI.
TRANSLATED

BY THOMAS JOHNES, ESQ.

IN THIRTEEN VOLUMES ... VOL. VII.

LONDON:

PRINTED FOR LONGMAN, HURST, REES, ORME, AND BROWN, PATERNOSTER-


ROW;
AND J. WHITE AND CO. FLEET-STREET.

1810.

CONTENTS
OF

THE SEVENTH VOLUME.


PAGE
CHAP. I.
Some captains attached to sir John
de Luxembourg surprise the castle
of St Martin, wherein they are all
taken and slain 1
CHAP. II.
Poton de Saintrailles and sir Louis
de Vaucourt are made prisoners by
the English 4
CHAP. III.
Maillotin de Bours and sir Hector de
Flavy fight together in the town of
Arras 5
CHAP. IV.
Some of king Charles's captains
make an attempt on Corbie 12
CHAP. V.
The lord de Barbasan lays siege to
the castle of Anglure, held by the
Burgundians 13
CHAP. VI.
The maid of Orleans is condemned
to be put to death and burnt at
Rouen 15
CHAP. VII.
The general council is continued at
Basil, by the solicitations of the
emperor 22
CHAP. VIII.
The duke of Bar enters the county
of Vaudemont to conquer it by force 29
CHAP. IX.
The duke of Bar is combated by the
count de Vaudemont and defeated 35
CHAP. X.
The young king Henry comes from
England, with a grand attendance,
to Paris, to be crowned king of
France 44
CHAP. XI.
The detachment the duke of Bar
had left to blockade Vaudemont
march away on hearing of the ill
success of the battle 51
CHAP. XII.
Sir John de Luxembourg assembles
men at arms and marches into
Champagne against the French,
from whom he conquers several
castles. Other matters 53
CHAP. XIII.
The duke d'Alençon makes the
chancellor of Brittany prisoner 57
CHAP. XIV.
The French are near taking the
castle of Rouen 59
CHAP. XV.
The French take the castle of
Dommart in Ponthieu, and carry off
the lord de Dommart prisoner 64
CHAP. XVI.
Sir Thomas Kiriel, an Englishman, is
appointed governor of the castle of
Clermont in the Beauvoisis 66
CHAP. XVII.
The inhabitants of Chauny-sur-Oise
destroy the castle of their town 68
CHAP. XVIII.
The city of Chartres is conquered by
king Charles's party 70
CHAP. XIX.
The cardinal of Santa Croce is sent
by the pope to France, to
endeavour to make peace between
the contending parties 76
CHAP. XX.
The English conquer the bulwark at
Lagny-sur-Marne 78
CHAP. XXI.
Philibert de Vaudray, governor of
Tonnerre, and the lord d'Amont wait
on the duke of Bedford to serve him 81
CHAP. XXII.
The duke of Bedford marches a
large force to Lagny-sur-Marne, to
support the English and
Burgundians who had remained
there, but retires without making
any conquest 83
CHAP. XXIII.
The commonalty of Ghent rise
against their magistrates 89
CHAP. XXIV.
Sir John bastard of St Pol and the
lord de Humieres are taken
prisoners by the French 91
CHAP. XXV.
Great disorders are committed by
the French in the Amiennois,
Santerre and Vimeu 92
CHAP. XXVI.
The heir of Commercy takes the 94
town of Ligny in the Barrois,
belonging to sir John de
Luxembourg
CHAP. XXVII.
The Burgundians, under pretence of
being English, gain the castle of La
Boue, near to Laon. Other matters 96
CHAP. XXVIII.
Friar Thomas goes to Rome.—He is
burnt there 98
CHAP. XXIX.
The death of the duchess of
Bedford 100
CHAP. XXX.
Some of the French captains cross
the river Somme and overrun Artois 101
CHAP. XXXI.
A benedictine monk attempts to
gain the castle of St Angelo at
Rome 102
CHAP. XXXII.
A peace is concluded between the
duke of Bar and the count de
Vaudemont 105
CHAP. XXXIII.
The duchess of Burgundy is brought
to bed of a son in the town of
Ghent 106
CHAP. XXXIV.
A peace concluded between the
duke of Bar and the counts de St
Pol and de Ligny 107
CHAP. XXXV.
A war takes place between sir John
and sir Anthony du Vergy and the
lord de Chasteau-Vilain 109
CHAP. XXXVI.
A treaty of peace is concluded
between the duke of Burgundy and
the Liegeois 112
CHAP. XXXVII.
The duke of Bedford, who styled
himself regent of France, marries
the daughter of the count de St Pol 113
CHAP. XXXVIII.
The town of St Valery, in Ponthieu,
is won by the French 115
CHAP. XXXIX.
The death of John de Toisy bishop
of Tournay. Great dissentions
respecting the promotion to the
vacant bishoprick The dukes of
Bedford and of Burgundy go to
Saint Omer 116
CHAP. XL.
The death of John de Toisy bishop
of Tournay. Great dissentions
respecting the promotion to the
vacant bishoprick 118
CHAP. XLI.
The French make many conquests
on the confines of Burgundy 123
CHAP. XLII.
The duke of Burgundy reconquers
several places which the French had
won in Burgundy 128
CHAP. XLIII.
Gilles de Postelles is accused of
treason to the duke of Burgundy,
and beheaded 129
CHAP. XLIV.
The French win by scalado the town
of Crespy in the Valois. Other
matters 130
CHAP. XLV.
The duke of Burgundy keeps his
appointment before Passy. He
besieges the town and castle of
Avalon 132
CHAP. XLVI.
Pierre de Luxembourg, count de St
Pol, besieges the town of St Valery.
The death of the count de St Pol 134
CHAP. XLVII.
The lord de la Trimouille is arrested
in the king's palace, and made to
surrender his prisoner the viscount
de Thouars 137
CHAP. XLVIII.
William de Coroam puts to flight
John Beaurain. Sir John de
Luxembourg reconquers the castle
of Haphincourt 139
CHAP. XLIX.
The counts de Ligny and de St Pol
keep the appointed day at Villiers le
Carbonel, and afterward defeat the
French from the Garrison of Laon 141
CHAP. L.
La Hire and other French captains
overrun Artois and Cambresis 145
CHAP. LI.
The duke of Burgundy holds the
anniversary feast of the golden
fleece in the city of Dijon. He
attends the marriage of the duke of
Savoy's son 148
CHAP. LII.
A general council is held at Basil 150
CHAP. LIII.
The town and castle of Provins in
Brie are won by the English and
Burgundians. The French reconquer
the town and castle of St Valery 152
CHAP. LIV.
The duke of Burgundy returns from
Burgundy to Flanders and Artois,
having with him John son to the
count de Nevers. Other matters 154
CHAP. LV.
John de Nevers is ordered to lay
siege to Moreuil. He has the county
of Estampes given to him 156
CHAP. LVI.
A quarrel between the Romans and
pope Eugenius, whom they wanted
to detain at Rome against his will 158
CHAP. LVII.
The abbey of St Vincent near Laon
is demolished. Many castles are
conquered by the Burgundians 159
CHAP. LVIII.
The lord Talbot returns to France,
and conquers many towns and
castles 161
CHAP. LIX.
The count d'Estampes reconquers
the town of St Valery 164
CHAP. LX.
The French gain the town of
Hamme on the Somme, in the
Vermandois 166
CHAP. LXI.
The town and castle of Chasteau-
Vilain submits to the obedience of
the duke of Burgundy 168
CHAP. LXII.
Heavy taxes laid on the countries of
Artois and those adjoining, on
account of this war 169
CHAP. LXIII.
The duke of Burgundy's captains
appear before Villefranche, wherein
was the duke of Bourbon. They
afterward besiege Belleville, which
surrenders to them 171
CHAP. LXIV.
The lord Willoughby and Mathagon
lay siege to St Severin, where the
English are at first victorious, but
are afterwards defeated by the
French 174
CHAP. LXV.
La Hire treacherously makes the
lord d'Auffemont a prisoner 177
CHAP. LXVI.
The common people of Normandy
rise against the English garrisons 178
CHAP. LXVII.
La Hire gains the castle of Breteuil,
in Beauvoisis, by storm 180
CHAP. LXVIII.
The dukes of Burgundy and of
Bourbon meet in the city of Nevers,
and agree on terms for a peace 181
CHAP. LXIX.
Amadeus duke of Savoy turns
hermit, and resides at Ripaille 187
CHAP. LXX.
The common people of Normandy
assemble in large bodies before
Caen 191
CHAP. LXXI.
The duke and duchess of Burgundy
return from that country to Flanders
and Artois 193
CHAP. LXXII.
The French gain the town of Rue
from the English 195
CHAP. LXXIII.
La Hire, Poton, Philip de la Tour, and
the lord de Fontaines, defeat the
earl of Arundel before the castle of
Gerberoy 197
CHAP. LXXIV.
The duke of Burgundy is displeased
with the inhabitants of Antwerp 203
CHAP. LXXV.
The French conquer the towns of St
Denis from the English 205
CHAP. LXXVI.
The French, after having agreed to
a truce with the Burgundians on the
frontiers of the Beauvoisis, overrun
the Boulonnois and other parts 208
CHAP. LXXVII.
The cardinals of Santa Croce and of
Cyprus come to Arras, to attend the
convention 211
CHAP. LXXVIII.
Louis de Luxembourg, count of St
Pol, espouses Joan of Bar, countess
of Marle and of Soissons 213
CHAP. LXXIX.
The French are defeated near to
Rethel, by the bastard de Humieres 214
CHAP. LXXX.
Ambassadors from the king of
England arrive at Arras to attend
the convention 215
CHAP. LXXXI.
Ambassadors from France arrive at
Arras to attend this convention 217
CHAP. LXXXII.
Sir John de Mello, a knight of Spain,
and the lord de Chargny, combat
each other in the presence of the
duke of Burgundy at Arras 223
CHAP. LXXXIII.
The French and Burgundians are on
very amicable terms in Arras 230
CHAP. LXXXIV.
The cardinal of Winchester comes
to Arras to attend the convention 232
CHAP. LXXXV.
During the meeting of the
convention at Arras, La Hire and
Poton overrun and forage the
country of the duke of Burgundy 234
CHAP. LXXXVI.
The kings of Arragon and Navarre
are defeated, and made prisoners,
before Gaieta, by the army of the
duke of Milan 237
CHAP. LXXXVII.
The cardinal of Winchester and the
whole of the English embassy leave
Arras. Other ambassadors arrive
there 240
CHAP. LXXXVIII.
A peace is concluded between
Charles king of France and the duke
of Burgundy, in the city of Arras
241
CHAP. LXXXIX.
The English lay siege to the town of
St Denis, which in the end
surrenders to them by capitulation 280
XC
Isabella, queen of France, dies in
the city of Paris 285
CHAP. XCI.
The cardinals, and the ambassadors
from the council, leave Arras. The
duke of Burgundy appoints different
officers to the towns and fortresses
that had been conceded to him by
the peace 286
CHAP. XCII.
In consequence of the peace of
Arras, the duke of Burgundy sends
some of his council, and heralds, to
the king of England, to remonstrate
and explain the causes of the peace
he had concluded with the king of
France 288
CHAP. XCIII.
The populace of Amiens rise against
the levying of some taxes which
were intended to be laid on them 294
CHAP. XCIV.
The French overrun and pillage the
country of the duke of Burgundy
after the peace of Arras. The
marshal de Rieux takes many towns
and castles from the English in
Normandy 300
CHAP. XCV.
The English suspect the
Burgundians who are waging war
with them against the king of
France: they no longer converse or
keep company with them. Other
matters briefly spoken of 306
CHAP. XCVI.
King Henry sends letters to the
Hollanders, to draw them to his
party. A copy of these letters 310
CHAP. XCVII.
The duke of Burgundy determines
to make war on the English 313
CHAP. XCVIII.
The duke of Burgundy, by the
advice of his privy counsellors,
resolves to make an attempt to
conquer Calais 318
CHAP. XCIX.
The city of Paris is reduced to the
obedience of Charles king of France 324
CHAP. C.
Arthur count de Richemont,
constable of France, makes war on
the heir of Commercy 330
CHAP. CI.
The bishop of Liege and the
Liegeois destroy Bousseuvre, and
other forts that had made war
against them 334
CHAP. CII.
The town and castle of Orchimont
are destroyed by Everard de la
Marche 340
CHAP. CIII.
The English make excursions from
Calais toward Boulogne and
Gravelines. La Hire conquers Gisors,
and loses it soon afterwards 342
CHAP. CIV.
The men of Ghent, and the
Flemings, make great preparations
for the siege of Calais 344
CHAP. CV.
Sir John de Croy, bailiff of Hainault,
in conjunction with other captains,
attack the English and are
discomfited by them 348
CHAP. CVI.
The Flemings march to the siege of
Calais—and march back again 352
CHAP. CVII.
Sir Florimont de Brimeu, seneschal
of Ponthieu, conquers the town of
Crotoy 382
CHAP. CVIII.
Humphry duke of Glocester arrives
at Calais with a large armament. He
enters Flanders, Artois, and other
territories of the duke of Burgundy,
and does much damage to them 385
CHAP. CIX.
The Flemings again take up arms,
after their retreat from Calais to
their towns 388
CHAP. CX.
La Hire conquers the town and
castle of Soissons. Other matters 395
CHAP. CXI.
The duchess of Bedford, sister to 397
the count de St Pol, re-marries of
her own free will. The king of Sicily
negotiates with the duke of
Burgundy for his liberty. The English
recover the town of Pontoise

HERE BEGINNETH

THE SEVENTH VOLUME


OF THE

CHRONICLES
OF

ENGUERRAND DE MONSTRELET.

[A.D. 1431.]

CHAP. I.
SOME CAPTAINS ATTACHED TO SIR JOHN DE LUXEMBOURG
SURPRISE THE CASTLE OF ST MARTIN, WHEREIN THEY ARE ALL
TAKEN AND SLAIN.
At the commencement of this year, some of the captains attached to
sir John de Luxembourg, such as sir Simon de Lalain, Bertrand de
Manicain, Enguerrand de Crequi, Enguerrand de Gribauval marched
from the borders of the Laonnois with four hundred combatants to
the abbey of St Vincent, near Laon, wherein were a body of French.
They gained it by surprise, and on their entrance they set up a loud
shout, which awakened part of the enemy within a strong gateway,
who instantly defended themselves with vigour; and, during this, the
lord de Pennesac, then in Laon, was told what had happened. He
immediately collected a force to succour those in the gate, who were
gallantly defending themselves; and his men at arms, enraged to
find the enemy so near, lost no time in putting on their armour.
They soon marched out of Laon to the assistance of their friends
then fighting; but a part of the Burgundians, without finishing their
enterprise, or providing for what might happen, had quitted the
combat to plunder the abbey. They were, therefore, unexpectedly
attacked by these men at arms, and with such vigour that they were
totally defeated, and sixty of the principal were left dead on the
spot: in the number were Bertrand de Manicain and Enguerrand de
Gribauval. The last offered a large ransom for his life; but it was
refused, by reason of the great hatred the common people bore him
for the very many mischiefs he had long before done them.
Sir Simon de Lalain was made prisoner, and had his life spared
through the means of a gallant youth of the garrison named
Archanciel, who was much beloved by the commonalty. Enguerrand
de Crequi was taken at the same time with sir Simon and a few
others; but the remainder, witnessing their ill success, retreated to
the places whence they had come.
Sir John de Luxembourg was much afflicted at this event, and not
without cause, for he had lost in the affair some of his ablest
captains. The brother of the lord de Pennesac, called James, was
killed.
At the same time, the castle of Rambures, belonging to the lord de
Rambures, then a prisoner in England, was won by the French,
under the command of Charles des Marests, who took it by scalado.
Ferry de Mailly was the governor of it for king Henry. The French, by
this capture, opened a free communication with the country of
Vimeu and those adjoining, as shall hereafter be shewn.
CHAP. II.
POTON DE SAINTRAILLES AND SIR LOUIS DE VAUCOURT ARE
MADE PRISONERS BY THE ENGLISH.
In this year, the marshal de Bousac, Poton de Saintrailles, sir Louis
de Vaucourt, and others of king Charles's captains, set out from
Beauvais with about eight hundred combatants to seek adventures,
and to forage the country near to Gournay. With them was a very
young shepherd's boy, who was desirous to raise his name in the
same way that the Maid had done.
The earl of Warwick had notice of their march, and collected with all
haste about six hundred fighting men, whom he led toward Beauvais
to meet the enemy. He came up with them, unexpectedly, near to
Gournay, and commenced a sharp conflict, in which so little
resistance was made by the French that they were soon put to the
rout, and Poton de Saintrailles, sir Louis de Vaucourt, and about sixty
combatants, were made prisoners. The rest, with the exception of
eight or ten who were slain, made their escape with the marshal to
Beauvais.
The English pursued them to the walls of that town, when the earl of
Warwick, assembling his men, returned to Gournay, happy at his
good success; and thence he went to the duke of Bedford in Rouen,
by whom he was joyfully congratulated on his victory.

CHAP. III.
MAILLOTIN DE BOURS AND SIR HECTOR DE FLAVY FIGHT
TOGETHER IN THE TOWN OF ARRAS.
On the 20th day of June in this year, a combat took place in the
town of Arras, and in the presence of the duke of Burgundy,
between Maillotin de Bours, appellant, and sir Hector de Flavy,

You might also like