Share
Explore

Swift Object Oriented Programming for iOS

Last edited 12 seconds ago by System Writer
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

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 program
print("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 class
class 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 class
class 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 objects
let pikachu = Pokemon(name: "Pikachu", type: "Electric", level: 25)
let charmander = Pokemon(name: "Charmander", type: "Fire", level: 10)

// Instantiate Trainer object
let 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 subclass
class WaterPokemon:

Pokemon {
var waterPower: Int
swiftCopy code
init(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 Methods
Task: Override methods in the Pokémon subclasses to implement type-specific behavior.

```swift
// Extend the Pokémon class to add an attack method
class Pokemon {
// ...

func attack() {
print("\(name) attacks!")
}
}

// Override the attack method in Water Pokémon subclass
class WaterPokemon: Pokemon {
// ...

override func attack() {
print("\(name) uses Water attack with power \(waterPower)!")
}
}

// Override the attack method in Fire Pokémon subclass
class FirePokemon: Pokemon {
// ...

override func attack() {
print("\(name) uses Fire attack with power \(firePower)!")
}
}

// Override the attack method in Grass Pokémon subclass
class GrassPokemon: Pokemon {
// ...

override func attack() {
print("\(name) uses Grass attack with power \(grassPower)!")
}
}

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.
class Battle {
private let trainer1: Trainer
private let trainer2: Trainer

init(trainer1: Trainer, trainer2: Trainer) {
self.trainer1 = trainer1
self.trainer2 = trainer2
}

func start() {
print("Battle between \(trainer1.name) and \(trainer2.name) starts!")
// Implement battle logic here
}
}

class Gym {
private var trainers: [Trainer]

init(trainers: [Trainer]) {
self.trainers = trainers
}

func addTrainer(trainer: Trainer) {
trainers.append(trainer)
}

func removeTrainer(trainer: Trainer) {
if let index = trainers.firstIndex(where: { $0.name == trainer.name }) {
trainers.remove(at: index)
}
}
}

Exercise 4.2: Implement Encapsulation
Task: Use encapsulation to protect the internal state of the Battle and Gym classes.
// Encapsulation is already implemented in the above code
// by using private access control for the trainer properties
// in the Battle class and the trainers array in the Gym class.

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.
class Ability {
var name: String

init(name: String) {
self.name = name
}

func execute() {
print("Executing ability: \(name)")
}
}

class ElectricAbility: Ability {

}
class FireAbility: Ability {
}
class WaterAbility: Ability {
}
class GrassAbility: Ability {
}
Exercise 5.2: Implement Polymorphism
Task: Use polymorphism to create an array of abilities and call their methods.

```swift
let thunderbolt = ElectricAbility(name: "Thunderbolt")
let flamethrower = FireAbility(name: "Flamethrower")
let hydroPump = WaterAbility(name: "Hydro Pump")
let solarBeam = GrassAbility(name: "Solar Beam")

let abilities: [Ability] = [thunderbolt, flamethrower, hydroPump, solarBeam]

for ability in abilities {
ability.execute()
}

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.
protocol Catchable {
func catchPokemon(pokemon: Pokemon)
}

protocol Tradable {
func tradePokemon(pokemon: Pokemon, with trainer: Trainer)
}

Exercise 6.2: Implement Protocols
Task: Make the Pokémon and Trainer classes conform to the Catchable and Tradable protocols.
class Trainer: Catchable, Tradable {
// ...

func catchPokemon(pokemon: Pokemon) {
print("\(name) caught a \(pokemon.name)!")
pokemons.append(pokemon)
}

func tradePokemon(pokemon: Pokemon, with trainer: Trainer) {
if let index = pokemons.firstIndex(where: { $0.name == pokemon.name }) {
pokemons.remove(at: index)
trainer.catchPokemon(pokemon: pokemon)
print("\(name) traded \(pokemon.name) with \(trainer.name)!")
}
}
}

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!
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.