Share
Explore

Swift Object Oriented Programming for iOS

Last edited 170 days ago by Peter Sigurdson
Welcome to the Pokémon Swift Object Oriented Programming for iOS lab workbook!
image.png
This is class activity for March 27.
This will train up your skills to do the Monopoly Game for the Project.
When we studied the requirements for the monopoly game - we saw that we have a number of elements we we need to model:
The Game
Game contains:
The Bank : which contains all the money and property titles NOT OWNED BY Players
The Player Objects are controlled by The Game. The Game object runs and controls the Players
The Board OBJECT
Contains and Controls:
The Properties
The SQUARES (Property Squares and Other)
The Community Chest Cards and the CHANCE Cards (So you need Classes and Objects for these)
What else? This is your project: think about it!
Should CASH be Modeled as a Separate Class - or is it just an ATTRIBUTE of Bank or a Player
Think about anything else you may need to MODEL.
This workbook is designed to teach you essential Swift Object Oriented Programming concepts with a fun and engaging Pokémon theme.
You will plough the concepts you learn here into doing your MONOPOLY Game.
Learning Outcomes:
Swift programming
Classes
Inheritance
Protocols
OO Design Heuristics, for example: when to model a business domain element as its own Class, or as an Attribute of another Class
Let's begin our journey to become a Pokémon Swift Master!

Table of Contents:

Note all of these topics are going to be on your Lab Test 2 and Final Exam

Introduction to Swift and Object-Oriented Programming
Classes and Objects: Pokémon Trainers and Pokémon
Inheritance: Pokémon Types
Encapsulation: Battle and Gym Systems
Polymorphism: Pokémon Abilities
Protocols: Catchable and Tradable
Conclusion
Introduction to Swift and Object-Oriented Programming
Objective: Understand the basics of Swift programming and Object-Oriented Programming concepts.

Exercise 1.1: Basic Swift Syntax

Exercise 1.1 : Write a simple Swift program that prints

"Hello, Pokémon Swift Trainer!"
Exercise 1.2: Object-Oriented Programming Terminology
Task: Define the following terms: class, object, inheritance, encapsulation, and polymorphism.
Classes and Objects: Pokémon Trainers and Pokémon
Objective: Learn about classes, objects, and properties using Pokémon Trainers and Pokémon as examples.
Exercise 2.1: Create a Pokémon Class
Reference materials
Loading…

Loading…
Loading…
Loading…
Task: Define a Pokémon class with properties such as name, type, and level.
Exercise 2.2: Create a Trainer Class
Task: Define a Trainer class with properties such as name, gender, and a collection of Pokémon.
Exercise 2.3: Instantiate Objects
Task: Create objects of the Trainer and Pokémon classes and assign values to their properties.
Inheritance: Pokémon Types
Objective: Understand inheritance and how to create subclasses in Swift.
Exercise 3.1: Create Pokémon Subclasses
Task: Create subclasses for different Pokémon types (e.g., Water, Fire, Grass) that inherit from the Pokémon class.
Exercise 3.2: Override Methods
Task: Override methods in the Pokémon subclasses to implement type-specific behavior.
Encapsulation: Battle and Gym Systems
Objective: Learn about encapsulation and how to create methods to manage access to object data.
Exercise 4.1: Create Battle and Gym Classes
Task: Create Battle and Gym classes that manage Pokémon battles and gym interactions.
Exercise 4.2: Implement Encapsulation
Task: Use encapsulation to protect the internal state of the Battle and Gym classes.
Polymorphism: Pokémon Abilities
Objective: Explore polymorphism and how to use it in Swift.
Exercise 5.1: Define Abilities
Task: Create an Ability class and subclasses for different Pokémon abilities.
Exercise 5.2: Implement Polymorphism
Task: Use polymorphism to create an array of abilities and call their methods.
Protocols: Catchable and Tradable
Objective: Learn about protocols and how to define and implement them in Swift.
Exercise 6.1: Create Catchable and Tradable Protocols
Task: Define the Catchable and Tradable protocols for Pokémon and Trainers.
Exercise 6.2: Implement Protocols
Task: Make the Pokémon and Trainer classes conform to the Catchable and Tradable protocols.
Conclusion
Congratulations! You have completed the Pokémon Swift Object Oriented Programming for iOS lab workbook. By now, you should have a solid understanding of Swift programming concepts like classes, inheritance, encapsulation, polymorphism, and protocols. Keep practicing, and continue your journey to become a Pokémon Swift Master!

Code Solutions:

Title: Pokémon Swift Object Oriented Programming for iOS Lab Workbook

Introduction
Welcome to the Pokémon Swift Object Oriented Programming for iOS lab workbook! This workbook is designed to teach you essential Swift Object Oriented Programming concepts with a fun and engaging Pokémon theme. We'll be covering Swift programming concepts like classes, inheritance, protocols, and other related topics through practical, hands-on exercises. Let's begin our journey to become a Pokémon Swift Master!
Table of Contents
Introduction to Swift and Object-Oriented Programming
Classes and Objects: Pokémon Trainers and Pokémon
Inheritance: Pokémon Types
Encapsulation: Battle and Gym Systems
Polymorphism: Pokémon Abilities
Protocols: Catchable and Tradable
Conclusion
Introduction to Swift and Object-Oriented Programming
Objective: Understand the basics of Swift programming and Object-Oriented Programming concepts.
Exercise 1.1: Basic Swift Syntax
Task: Write a simple Swift program that prints "Hello, Pokémon Swift Trainer!"
// Simple Swift programprint("Hello, Pokémon Swift Trainer!")
Exercise 1.2: Object-Oriented Programming Terminology
Task: Define the following terms: class, object, inheritance, encapsulation, and polymorphism.
Class: A blueprint for creating objects with specific properties and methods.
Object: An instance of a class.
Inheritance: The ability of a class to inherit properties and methods from a parent class.
Encapsulation: The bundling of data and methods that operate on that data within one unit, restricting access to some of the object's components.
Polymorphism: The ability of a single function or method to operate on different types of data or objects.
Classes and Objects: Pokémon Trainers and Pokémon
Objective: Learn about classes, objects, and properties using Pokémon Trainers and Pokémon as examples.
Exercise 2.1: Create a Pokémon Class
Task: Define a Pokémon class with properties such as name, type, and level.
// Pokémon classclass Pokemon { var name: String var type: String var level: Int
init(name: String, type: String, level: Int) { self.name = name self.type = type self.level = level }}
Exercise 2.2: Create a Trainer Class
Task: Define a Trainer class with properties such as name, gender, and a collection of Pokémon.
// Trainer classclass Trainer { var name: String var gender: String var pokemons: [Pokemon]
init(name: String, gender: String, pokemons: [Pokemon]) { self.name = name self.gender = gender self.pokemons = pokemons }}
Exercise 2.3: Instantiate Objects
Task: Create objects of the Trainer and Pokémon classes and assign values to their properties.
swiftCopy code// Instantiate Pokémon objectslet pikachu = Pokemon(name: "Pikachu", type: "Electric", level: 25)let charmander = Pokemon(name: "Charmander", type: "Fire", level: 10)
// Instantiate Trainer objectlet ash = Trainer(name: "Ash", gender: "Male", pokemons: [pikachu, charmander])
Inheritance: Pokémon Types
Objective: Understand inheritance and how to create subclasses in Swift.
Exercise 3.1: Create Pokémon Subclasses
Task: Create subclasses for different Pokémon types (e.g., Water, Fire, Grass) that inherit from the Pokémon class.

// Water Pokémon subclassclass WaterPokemon:
Pokemon {
var waterPower: Int
swiftCopy codeinit(name: String, level: Int, waterPower: Int) { self.waterPower = waterPower super.init(name: name, type: "Water", level: level)}
}
// Fire Pokémon subclass
class FirePokemon: Pokemon {
var firePower: Int
init(name: String, level: Int, firePower: Int) { self.firePower = firePower super.init(name: name, type: "Fire", level: level)}
}
// Grass Pokémon subclass
class GrassPokemon: Pokemon {
var grassPower: Int
init(name: String, level: Int, grassPower: Int) { self.grassPower = grassPower super.init(name: name, type: "Grass", level: level)}
}
Exercise 3.2: Override MethodsTask: Override methods in the Pokémon subclasses to implement type-specific behavior.
```swift// Extend the Pokémon class to add an attack methodclass Pokemon { // ...
func attack() { print("\(name) attacks!")
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.