From faac2fe50244da03d540d4af46e0a1778b526f55 Mon Sep 17 00:00:00 2001 From: tasneem gameel Date: Sun, 31 May 2020 23:20:15 +0200 Subject: [PATCH 1/2] Func 1st Ex. Done --- .../Contents.swift | 23 ------------------ .../{Contents.swift => Contents.txt} | 0 .../{Contents.swift => Contents.doc} | 0 .../~$ntents.doc | Bin 0 -> 162 bytes 4 files changed, 23 deletions(-) delete mode 100644 Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/9. Exercise - Type Inference and Required Values.xcplaygroundpage/Contents.swift rename Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/1. Exercise - Create Functions.xcplaygroundpage/{Contents.swift => Contents.txt} (100%) rename Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/{Contents.swift => Contents.doc} (100%) create mode 100644 Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/~$ntents.doc diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/9. Exercise - Type Inference and Required Values.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/9. Exercise - Type Inference and Required Values.xcplaygroundpage/Contents.swift deleted file mode 100644 index 5c81e84..0000000 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/9. Exercise - Type Inference and Required Values.xcplaygroundpage/Contents.swift +++ /dev/null @@ -1,23 +0,0 @@ -/*: - ## Exercise - Type Inference and Required Values - - Declare a variable called `name` of type `String`, but do not give it a value. Print `name` to the console. Does the code compile? Remove any code that will not compile. - */ - - -/*: - Now assign a value to `name`, and print it to the console. - */ - - -/*: - Declare a variable called `distanceTraveled` and set it to 0. Do not give it an explicit type. - */ - - -/*: - Now assign a value of 54.3 to `distanceTraveled`. Does the code compile? Go back and set an explicit type on `distanceTraveled` so the code will compile. - */ - - -//: [Previous](@previous) | page 9 of 10 | [Next: App Exercise - Percent Completed](@next) diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/1. Exercise - Create Functions.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/1. Exercise - Create Functions.xcplaygroundpage/Contents.txt similarity index 100% rename from Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/1. Exercise - Create Functions.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/1. Exercise - Create Functions.xcplaygroundpage/Contents.txt diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/Contents.doc similarity index 100% rename from Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/Contents.doc diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/~$ntents.doc b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/~$ntents.doc new file mode 100644 index 0000000000000000000000000000000000000000..3920d1035196ac9f7c8911cc4a3bae39f7df17f7 GIT binary patch literal 162 zcmZP%Ni5DwP0eK>39vJSFeEY*GvqO(0wGv)GEg9=KOu7~gB8Ps4XN)xsbMj(Ga-8` ngEd3P2A|(xd54CN5Otjpes@CdRt6h}9!{Y7 Date: Tue, 9 Jun 2020 23:55:26 +0200 Subject: [PATCH 2/2] Labs Done --- .../{Contents.swift => Contents.txt} | 18 +++++-- .../{Contents.swift => Contents.txt} | 17 +++++-- .../{Contents.swift => Contents.txt} | 11 +++-- .../{Contents.swift => Contents.txt} | 33 ++++++++++++- .../{Contents.swift => Contents.txt} | 18 ++++++- .../Contents.txt | 26 ++++++++++ .../Contents.doc | 45 ++++++++++++++++++ .../~$ntents.doc | Bin 162 -> 162 bytes .../{Contents.swift => Contents.txt} | 23 ++++++++- .../{Contents.swift => Contents.txt} | 30 ++++++++++++ .../{Contents.swift => Contents.txt} | 15 ++++++ .../{Contents.swift => Contents.txt} | 0 .../{Contents.swift => Contents.txt} | 24 +++++++++- .../{Contents.swift => Contents.txt} | 13 ++++- .../{Contents.swift => Contents.txt} | 40 ++++++++++++++-- .../{Contents.swift => Contents.txt} | 32 +++++++++++-- .../{Contents.swift => Contents.txt} | 20 ++++++++ .../{Contents.swift => Contents.txt} | 16 +++++++ .../{Contents.swift => Contents.txt} | 6 +++ .../{Contents.swift => Contents.txt} | 16 +++++++ .../{Contents.swift => Contents.txt} | 0 .../{Contents.swift => Contents.txt} | 40 ++++++++++++++++ .../{Contents.swift => Contents.txt} | 33 ++++++++++++- .../{Contents.swift => Contents.txt} | 34 ++++++++++++- .../{Contents.swift => Contents.txt} | 34 ++++++++++--- .../{Contents.swift => Contents.txt} | 18 ++++--- .../{Contents.swift => Contents.txt} | 27 +++++++++-- .../{Contents.swift => Contents.txt} | 17 ++++++- .../{Contents.swift => Contents.txt} | 37 ++++++++++++-- .../{Contents.swift => Contents.txt} | 14 +++++- .../{Contents.swift => Contents.txt} | 9 +++- .../{Contents.swift => Contents.txt} | 11 +++++ .../{Contents.swift => Contents.txt} | 19 ++++++++ .../{Contents.swift => Contents.txt} | 26 ++++++++++ .../{Contents.swift => Contents.txt} | 14 +++++- .../{Contents.swift => Contents.txt} | 0 .../{Contents.swift => Contents.txt} | 11 ++++- .../{Contents.swift => Contents.txt} | 0 .../{Contents.swift => Contents.txt} | 27 +++++++++++ .../{Contents.swift => Contents.txt} | 23 +++++++-- .../{Contents.swift => Contents.txt} | 45 ++++++++++++++++-- .../{Contents.swift => Contents.txt} | 35 +++++++++++++- .../{Contents.swift => Contents.txt} | 39 ++++++++++++++- .../{Contents.swift => Contents.txt} | 34 +++++++++++-- 44 files changed, 879 insertions(+), 71 deletions(-) rename Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/1. Exercise - String Basics.xcplaygroundpage/{Contents.swift => Contents.txt} (77%) rename Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/2. Exercise - Concatenation and Interpolation.xcplaygroundpage/{Contents.swift => Contents.txt} (79%) rename Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/3. App Exercise - Notifications.xcplaygroundpage/{Contents.swift => Contents.txt} (77%) rename Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/4. Exercise - String Equality and Comparison.xcplaygroundpage/{Contents.swift => Contents.txt} (76%) rename Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/5. App Exercise - Password Entry and User Search.xcplaygroundpage/{Contents.swift => Contents.txt} (89%) rename Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/3. Exercise - Parameters and Argument Labels.xcplaygroundpage/{Contents.swift => Contents.txt} (70%) rename Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/4. App Exercise - Progress Updates.xcplaygroundpage/{Contents.swift => Contents.txt} (75%) rename Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/5. Exercise - Return Values.xcplaygroundpage/{Contents.swift => Contents.txt} (66%) rename Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/6. App Exercise - Separating Functions.xcplaygroundpage/{Contents.swift => Contents.txt} (100%) rename Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/1. Exercise - Structs, Instances, and Default Values.xcplaygroundpage/{Contents.swift => Contents.txt} (65%) rename Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/2. App Exercise - Workout Tracking.xcplaygroundpage/{Contents.swift => Contents.txt} (75%) rename Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/3. Exercise - Memberwise and Custom Initializers.xcplaygroundpage/{Contents.swift => Contents.txt} (65%) rename Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/4. App Exercise - Users and Distance.xcplaygroundpage/{Contents.swift => Contents.txt} (74%) rename Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/5. Exercise - Methods.xcplaygroundpage/{Contents.swift => Contents.txt} (61%) rename Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/6. App Exercise - Workout Functions.xcplaygroundpage/{Contents.swift => Contents.txt} (70%) rename Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/7. Exercise - Computed Properties and Property Observers.xcplaygroundpage/{Contents.swift => Contents.txt} (92%) rename Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/8. App Exercise - Mile Times and Congratulations.xcplaygroundpage/{Contents.swift => Contents.txt} (83%) rename Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/9. Exercise - Type Properties and Methods.xcplaygroundpage/{Contents.swift => Contents.txt} (100%) rename Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/1. Exercise - Define a Base Class.xcplaygroundpage/{Contents.swift => Contents.txt} (74%) rename Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/2. Exercise - Create a Subclass.xcplaygroundpage/{Contents.swift => Contents.txt} (75%) rename Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/3. Exercise - Override Methods and Properties.xcplaygroundpage/{Contents.swift => Contents.txt} (73%) rename Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/4. Exercise - Class Memberwise Initializers and References.xcplaygroundpage/{Contents.swift => Contents.txt} (81%) rename Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/1. Exercise - Arrays.xcplaygroundpage/{Contents.swift => Contents.txt} (74%) rename Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/2. App Exercise - Activity Challenge.xcplaygroundpage/{Contents.swift => Contents.txt} (72%) rename Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/3. Exercise - Dictionaries.xcplaygroundpage/{Contents.swift => Contents.txt} (74%) rename Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/4. App Exercises - Pacing.xcplaygroundpage/{Contents.swift => Contents.txt} (79%) rename Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/1. Exercise - For-In Loops.xcplaygroundpage/{Contents.swift => Contents.txt} (69%) rename Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/2. App Exercise - Movements.xcplaygroundpage/{Contents.swift => Contents.txt} (86%) rename Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/3. Exercise - While Loops.xcplaygroundpage/{Contents.swift => Contents.txt} (70%) rename Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/4. App Exercise - Running Cadence.xcplaygroundpage/{Contents.swift => Contents.txt} (78%) rename Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/5. Exercise - Control Transfer Statements.xcplaygroundpage/{Contents.swift => Contents.txt} (70%) rename Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/6. App Exercise - Finding Movements.xcplaygroundpage/{Contents.swift => Contents.txt} (92%) rename Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/1. Exercise - Optionals.xcplaygroundpage/{Contents.swift => Contents.txt} (100%) rename Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/2. App Exercise - Finding a Heart Rate.xcplaygroundpage/{Contents.swift => Contents.txt} (87%) rename Student Resources/3 - Navigation and Workflows/2 - Type Casting/lab/Lab - Type Casting.playground/Pages/1. Exercise - Type Casting and Inspection.xcplaygroundpage/{Contents.swift => Contents.txt} (100%) rename Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/1. Exercise - Scope.xcplaygroundpage/{Contents.swift => Contents.txt} (67%) rename Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/2. App Exercise - Step Competition.xcplaygroundpage/{Contents.swift => Contents.txt} (90%) rename Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/1. Exercise - Enumerations.xcplaygroundpage/{Contents.swift => Contents.txt} (66%) rename Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/2. App Exercise - Swimming Workouts.xcplaygroundpage/{Contents.swift => Contents.txt} (72%) rename Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/1. Exercise - Adopt Protocols.xcplaygroundpage/{Contents.swift => Contents.txt} (60%) rename Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/2. App Exercise - Printable Workouts.xcplaygroundpage/{Contents.swift => Contents.txt} (67%) diff --git a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/1. Exercise - String Basics.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/1. Exercise - String Basics.xcplaygroundpage/Contents.txt similarity index 77% rename from Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/1. Exercise - String Basics.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/1. Exercise - String Basics.xcplaygroundpage/Contents.txt index d17ab77..ba53d8b 100644 --- a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/1. Exercise - String Basics.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/1. Exercise - String Basics.xcplaygroundpage/Contents.txt @@ -3,24 +3,34 @@ Create a `name` constant and assign it a string literal representing your name. */ - +//let name = "Tasneem" /*: Create a `favoriteQuote` constant and assign it the following string literal: - - "My favorite quote is ." + - Write in your own favorite quote where indicated, and be sure to include escaped quotation marks. When finished, print the value of `favoriteQuote`. - callout(Example): If your favorite quote is "The grass is always greener on the other side" the value of `favoriteQuote` should be such that printing `favoriteQuote` results in the following: * `My favorite quote is "The grass is always greener on the other side."` */ - +//let favoriteQuote = "My favorite quote is \" Never Give Up \"" + //print ( favoriteQuote) /*: Write an if-else statement that prints "There's nothing here" if `emptyString` is empty, and "It's not as empty as I thought" otherwise. */ -let emptyString = "" +/*let emptyString = "" +if emptyString.isEmpty +{ + print (" There's nothing here") +} +else +{ +print (" It's not as empty as I thought") +} +*/ //: page 1 of 5 | [Next: Exercise - Concatenation and Interpolation](@next) diff --git a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/2. Exercise - Concatenation and Interpolation.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/2. Exercise - Concatenation and Interpolation.xcplaygroundpage/Contents.txt similarity index 79% rename from Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/2. Exercise - Concatenation and Interpolation.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/2. Exercise - Concatenation and Interpolation.xcplaygroundpage/Contents.txt index 2bbe7f2..45f019e 100644 --- a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/2. Exercise - Concatenation and Interpolation.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/2. Exercise - Concatenation and Interpolation.xcplaygroundpage/Contents.txt @@ -4,20 +4,27 @@ Create a `city` constant and assign it a string literal representing your home city. Then create a `state` constant and assign it a string literal representing your home state. Finally, create a `home` constant and use string concatenation to assign it a string representing your home city and state (i.e. Portland, Oregon). Print the value of `home`. */ +/*let city = " Egypt" +let state = "Cairo" +let home = city + "," + state +print (home) /*: Use the compound assignment operator (`+=`) to add `home` to `introduction` below. Print the value of `introduction`. */ var introduction = "I live in" +introduction += home +print ( introduction) - +*/ /*: Declare a `name` constant and assign it your name as a string literal. Then declare an `age` constant and give it your current age as an `Int`. Then print the following phrase using string interpolation: - - - "My name is and after my next birthday I will be years old." + Insert `name` where indicated, and insert a mathematical expression that evaluates to your current age plus one where indicated. */ - - + /*et name = "Tasneem" + let age = 29 + print ("My name is \(name) and after my next birthday I will be \(age + 1 ) years old.") +*/ //: [Previous](@previous) | page 2 of 5 | [Next: App Exercise - Notifications](@next) diff --git a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/3. App Exercise - Notifications.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/3. App Exercise - Notifications.xcplaygroundpage/Contents.txt similarity index 77% rename from Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/3. App Exercise - Notifications.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/3. App Exercise - Notifications.xcplaygroundpage/Contents.txt index abb8e54..93f4cbe 100644 --- a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/3. App Exercise - Notifications.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/3. App Exercise - Notifications.xcplaygroundpage/Contents.txt @@ -7,17 +7,22 @@ Create `firstName` and `lastName` constants and assign them string literals representing a user's first name and last name, respectively. Create a `fullName` constant that uses string concatenation to combine `firstName` and `lastName`. Print the value of `fullName`. */ - +let firstName = "Tasneem" +let lastName = " Gameel" +let fullName = firstName + lastName +print ( fullName ) /*: Occasionally users of your fitness tracking app will beat previous goals or records. You may want to notify them when this happens for encouragement purposes. Create a new constant `congratulations` and assign it a string literal that uses string interpolation to create the following string: - - "Congratulations, ! You beat your previous daily high score of steps by walking steps yesterday!" + Insert `fullName`, `previousBest` and `newBest` where indicated. Print the value of `congratulations`. */ -let previousBest = 14392 + let previousBest = 14392 let newBest = 15125 + let congratulations = "Congratulations, \(fullName) You beat your previous daily high score of \(previousBest) steps by walking \(newBest) steps yesterday!" + print (congratulations) //: [Previous](@previous) | page 3 of 5 | [Next: Exercise - String Equality and Comparison](@next) diff --git a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/4. Exercise - String Equality and Comparison.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/4. Exercise - String Equality and Comparison.xcplaygroundpage/Contents.txt similarity index 76% rename from Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/4. Exercise - String Equality and Comparison.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/4. Exercise - String Equality and Comparison.xcplaygroundpage/Contents.txt index 3ee86db..2a3eae6 100644 --- a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/4. Exercise - String Equality and Comparison.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/4. Exercise - String Equality and Comparison.xcplaygroundpage/Contents.txt @@ -3,6 +3,17 @@ Create two constants, `nameInCaps` and `name`. Assign `nameInCaps` your name as a string literal with proper capitalization. Assign `name` your name as a string literal in all lowercase. Write an if-else statement that checks to see if `nameInCaps` and `name` are the same. If they are, print "The two strings are equal", otherwise print "The two strings are not equal." */ + let nameInCaps = "Tasneem Gameel" + let name = "tasneem gameel" + + if nameInCaps == name + { + print ("The two strings are equal") + } + else + { + print ( "The two strings are not equal.") + } /*: @@ -14,12 +25,23 @@ - " and are not the same." */ - +if nameInCaps.lowercased() == name.lowercased() +{ + print ("\(nameInCaps.lowercased()) and \(name.lowercased()) are the same.") +} +else +{ + print ("\(nameInCaps.lowercased()) and \(name.lowercased())are not the same.") +} /*: Imagine you are looking through a list of names to find any that end in "Jr." Write an if statement below that will check if `junior` has the suffix "Jr.". If it does, print "We found a second generation name!" */ let junior = "Cal Ripken Jr." +if junior.hasSuffix("Jr.") +{ + print ( "We found a second generation name!") +} /*: @@ -28,11 +50,18 @@ let junior = "Cal Ripken Jr." import Foundation let textToSearchThrough = "To be, or not to be--that is the question" let textToSearchFor = "to be, or not to be" - +if textToSearchThrough.lowercased().contains(textToSearchFor.lowercased()) +{ + print("I found it!") +} +else{ + print ("search for another word") +} /*: Print to the console the number of characters in your name by using the `count` property on `name`. */ +print (name.count) //: [Previous](@previous) | page 4 of 5 | [Next: App Exercise - Password Entry and User Search](@next) diff --git a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/5. App Exercise - Password Entry and User Search.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/5. App Exercise - Password Entry and User Search.xcplaygroundpage/Contents.txt similarity index 89% rename from Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/5. App Exercise - Password Entry and User Search.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/5. App Exercise - Password Entry and User Search.xcplaygroundpage/Contents.txt index e396836..46f9b90 100644 --- a/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/5. App Exercise - Password Entry and User Search.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/1 - Strings/lab/Lab - Strings.playground/Pages/5. App Exercise - Password Entry and User Search.xcplaygroundpage/Contents.txt @@ -1,4 +1,4 @@ -/*: +/*: ## App Exercise - Password Entry and User Search >These exercises reinforce Swift concepts in the context of a fitness tracking app. @@ -9,6 +9,14 @@ let storedUserName = "TheFittest11" let storedPassword = "a8H1LuK91" let enteredUserName = "thefittest11" let enteredPassword: String = "a8H1Luk9" +if storedUserName.lowercased() == enteredUserName.lowercased() && storedPassword == enteredPassword +{ + print ("You are now logged in!") +} +else +{ + print ( "Please check your user name and password and try again.") +} /*: @@ -19,6 +27,14 @@ let enteredPassword: String = "a8H1Luk9" import Foundation let userName = "StepChallenger" let searchName = "step" +if userName.lowercased().contains(searchName.lowercased()) +{ + print ("yes, contains") +} +else +{ + print ("No, doesn't contain") +} /*: diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/1. Exercise - Create Functions.xcplaygroundpage/Contents.txt b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/1. Exercise - Create Functions.xcplaygroundpage/Contents.txt index 37d509b..5a19e45 100644 --- a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/1. Exercise - Create Functions.xcplaygroundpage/Contents.txt +++ b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/1. Exercise - Create Functions.xcplaygroundpage/Contents.txt @@ -3,11 +3,37 @@ Write a function called `introduceMyself` that prints a brief introduction of yourself. Call the function and observe the printout. */ +func introduceMyself() +{ +print("I'm tasneem gameel, teaching assistant at faculty of computer and information sciences") + +} +introduceMyself() /*: Write a function called `magicEightBall` that generates a random number and then uses either a switch statement or if-else-if statements to print different responses based on the random number generated. `let randomNum = Int.random(in: 0...4)` will generate a random number from 0 to 4, after which you can print different phrases corresponding to the number generated. Call the function multiple times and observe the different printouts. */ +func magicEightBall() +{ +let randomNum = Int.random(in: 0...4) +switch (randomNum) +{ +case 0: + print ("0") + case 1: + print ("1") + case 2: + print ("2") + case 3: + print ("3") + case 4: + print ("4") +default: + print("Not 0..4 number") +} +} +magicEightBall() //: page 1 of 6 | [Next: App Exercise - A Functioning App](@next) diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/Contents.doc b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/Contents.doc index 17808ef..361c4a8 100644 --- a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/Contents.doc +++ b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/Contents.doc @@ -8,6 +8,14 @@ A reoccurring process like this is a perfect candidate for a function. Write a function called `incrementSteps` after the declaration of `steps` below that will increment `steps` by one and then print its value. Call the function multiple times and observe the printouts. */ var steps = 0 +func incrementSteps (_ steps: Int) +{ +let steps = steps+1 +print (steps) +} +incrementSteps(steps) +incrementSteps(steps) +incrementSteps(steps) /*: @@ -15,5 +23,42 @@ var steps = 0 */ let goal = 10000 +//i can't call the othercalls with the updated value everytime steps returns to 0 +var steps = 0 +func incrementSteps (_ steps: Int)-> Int +{ + let steps = steps+1 +print (steps) +return steps +} +let no1=incrementSteps(steps) +let no2=incrementSteps(no1) +let no3=incrementSteps(no2) + +let goal = 10000.0 +func progressUpdate() +{ + print (" You're off to a good start.") + let tenth = goal * 0.1 + if (Double)no3 < tenth + { + print (" You're almost halfway there!") + } + let half = goal * 0.5 + else if (Double)no3 < half + { + print (" You're over halfway there!") + } + let ninth = goal * 0.9 + else if (Double)no3 < ninth + { + print(" You're almost there!") + } + else if (Double) no3 > goal + { + print (" You beat your goal!") + } +} + //: [Previous](@previous) | page 2 of 6 | [Next: Exercise - Parameters and Argument Labels](@next) diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/~$ntents.doc b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/2. App Exercise - A Functioning App.xcplaygroundpage/~$ntents.doc index 3920d1035196ac9f7c8911cc4a3bae39f7df17f7..41f367bd90f6e891958942033d8327bfffb887a5 100644 GIT binary patch literal 162 zcmZP%Ni5DwP0eK>39vJSFeEY*GvqO(0wGv4kYEX6&;uI748(~HC2#`+7@Qfb7-ASg f8H&pmmGA$Q${@nXz|a7~JU~S-HZKxe5{V4}>SGaD delta 81 zcmZ3)xQKCrr%*COB12ApLgrQmD~1UhQr~}4nq>a&l&Fk diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/3. Exercise - Parameters and Argument Labels.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/3. Exercise - Parameters and Argument Labels.xcplaygroundpage/Contents.txt similarity index 70% rename from Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/3. Exercise - Parameters and Argument Labels.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/3. Exercise - Parameters and Argument Labels.xcplaygroundpage/Contents.txt index 33158a5..60ca26a 100644 --- a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/3. Exercise - Parameters and Argument Labels.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/3. Exercise - Parameters and Argument Labels.xcplaygroundpage/Contents.txt @@ -2,17 +2,38 @@ ## Exercise - Parameters and Argument Labels Write a new introduction function called `introduction`. It should take two `String` parameters, `name` and `home`, and one `Int` parameter, `age`. The function should print a brief introduction. I.e. if "Mary," "California," and 32 were passed into the function, it might print "Mary, 32, is from California." Call the function and observe the printout. - */ + */ func introduction (_ name: String, _ home: String, _ age: Int) +{ + +print ("Hello \(name), \(age), is from \(home)") +} + +print ("Tasneem","Egypt", 29) /*: Write a function called `almostAddition` that takes two `Int` arguments. The first argument should not require an argument label. The function should add the two arguments together, subtract 2, then print the result. Call the function and observe the printout. */ +func almostAddition(_ firstNum: Int, _ secondNum: Int) +{ +let result = (firstNum + secondNum)-2; +print("\(firstNum) + \(secondNum) is almost \(result)") +} + +almostAddition(10 , 5) /*: Write a function called `multiply` that takes two `Double` arguments. The function should multiply the two arguments and print the result. The first argument should not require a label, and the second argument should have an external label, "by", that differs from the internal label. Call the function and observe the printout. */ +func multiply(_ firstNum: Int, by secondNum: Int) +{ +let result = firstNum * secondNum +print ( result ) + +} +multiply(3, by: 2) + //: [Previous](@previous) | page 3 of 6 | [Next: App Exercise - Progress Updates](@next) diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/4. App Exercise - Progress Updates.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/4. App Exercise - Progress Updates.xcplaygroundpage/Contents.txt similarity index 75% rename from Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/4. App Exercise - Progress Updates.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/4. App Exercise - Progress Updates.xcplaygroundpage/Contents.txt index 29d57f3..e8042e9 100644 --- a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/4. App Exercise - Progress Updates.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/4. App Exercise - Progress Updates.xcplaygroundpage/Contents.txt @@ -9,7 +9,37 @@ Call the function a number of times, passing in different values of `steps` and `goal`. Observe the printouts and make sure what is printed to the console is what you would expect for the parameters passsed in. */ +func progressUpdate(_ steps: Int, _ goal: Int) +{ + print (" You're off to a good start.") + let tenth = Double (goal) * 0.1 + let half = Double (goal) * 0.5 + let ninth = Double (goal) * 0.9 + if Double (steps) <= tenth + { + print (" You're almost halfway there!") + } + + else if Double (steps) <= half + { + print (" You're over halfway there!") + } + + else if Double (steps) <= ninth + { + print(" You're almost there!") + } + else if steps > goal + { + print (" You beat your goal!") + } +} + + progressUpdate(900, 1000) + progressUpdate(1900, 1000) + +} /*: Your fitness tracking app is going to help runners stay on pace to reach their goals. Write a function called pacing that takes four `Double` parameters called `currentDistance`, `totalDistance`, `currentTime`, and `goalTime`. Your function should calculate whether or not the user is on pace to hit or beat `goalTime`. If yes, print "Keep it up!", otherwise print "You've got to push it just a bit harder!" diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/5. Exercise - Return Values.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/5. Exercise - Return Values.xcplaygroundpage/Contents.txt similarity index 66% rename from Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/5. Exercise - Return Values.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/5. Exercise - Return Values.xcplaygroundpage/Contents.txt index 2cdc442..756f494 100644 --- a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/5. Exercise - Return Values.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/5. Exercise - Return Values.xcplaygroundpage/Contents.txt @@ -3,11 +3,26 @@ Write a function called `greeting` that takes a `String` argument called name, and returns a `String` that greets the name that was passed into the function. I.e. if you pass in "Dan" the return value might be "Hi, Dan! How are you?" Use the function and print the result. */ +func greeting (_ name: String) -> String +{ +return name +} + +let returnedName = greeting ("Tasneem") +print("Hi \(returnedName) How are you?") + /*: Write a function that takes two `Int` arguments, and returns an `Int`. The function should multiply the two arguments, add 2, then return the result. Use the function and print the result. */ +func almostMultiply(_ firstNum: Int, _ secondNum: Int)-> Int +{ +let multiply = (firstNum * secondNum) + 2 +return multiply +} +let result = almostMultiply (3,3) +print(result) //: [Previous](@previous) | page 5 of 6 | [Next: App Exercise - Separating Functions](@next) diff --git a/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/6. App Exercise - Separating Functions.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/6. App Exercise - Separating Functions.xcplaygroundpage/Contents.txt similarity index 100% rename from Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/6. App Exercise - Separating Functions.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/2 - Functions/lab/Lab - Functions.playground/Pages/6. App Exercise - Separating Functions.xcplaygroundpage/Contents.txt diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/1. Exercise - Structs, Instances, and Default Values.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/1. Exercise - Structs, Instances, and Default Values.xcplaygroundpage/Contents.txt similarity index 65% rename from Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/1. Exercise - Structs, Instances, and Default Values.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/1. Exercise - Structs, Instances, and Default Values.xcplaygroundpage/Contents.txt index 61fd1f3..a68fa2f 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/1. Exercise - Structs, Instances, and Default Values.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/1. Exercise - Structs, Instances, and Default Values.xcplaygroundpage/Contents.txt @@ -8,21 +8,41 @@ /*: Create a variable instance of `GPS` called `somePlace`. It should be initialized without supplying any arguments. Print out the latitude and longitude of `somePlace`, which should be 0.0 for both. */ + struct GPS{ + var latitude = 0.0 + var longitude = 0.0 + +} + var somePlace = GPS () + print(somePlace.latitude, somePlace.longitude) /*: Change `somePlace`'s latitude to 51.514004, and the longitude to 0.125226, then print the updated values. */ + somePlace.latitude = 51.51 + somePlace.longitude = 0.125226 +print(somePlace.latitude, somePlace.longitude) /*: Now imagine you are making a social app for sharing your favorite books. Create a `Book` struct with four variable properties: `title`, `author`, `pages`, and `price`. The default values for both `title` and `author` should be an empty string. `pages` should default to 0, and `price` should default to 0.0. */ - +struct Book{ + var title : String = "" + var author : String = "" + var pages : Int = 0 + var price : Double = 0.0 +} /*: Create a variable instance of `Book` called `favoriteBook` without supplying any arguments. Print out the title of `favoriteBook`. Does it currently reflect the title of your favorite book? Probably not. Change all four properties of `favoriteBook` to reflect your favorite book. Then, using the properties of `favoriteBook`, print out facts about the book. */ - + var favoriteBook = Book () + favoriteBook.title = "Women from mars and men from venus" + favoriteBook.author = "actually don't know" + favoriteBook.pages = 100 + favoriteBook.price = 50.5 + print("My favoriteBook is \(favoriteBook.title) written by \(favoriteBook.author), which is \(favoriteBook.pages) pages and its price is \(favoriteBook.price)") //: page 1 of 10 | [Next: App Exercise - Workout Tracking](@next) diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/2. App Exercise - Workout Tracking.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/2. App Exercise - Workout Tracking.xcplaygroundpage/Contents.txt similarity index 75% rename from Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/2. App Exercise - Workout Tracking.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/2. App Exercise - Workout Tracking.xcplaygroundpage/Contents.txt index 87c64af..c43fe23 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/2. App Exercise - Workout Tracking.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/2. App Exercise - Workout Tracking.xcplaygroundpage/Contents.txt @@ -7,16 +7,25 @@ Create a `RunningWorkout` struct. It should have variables properties for `distance`, `time`, and `elevation`. All three properties should have default values of 0.0. */ - +struct RunningWorkout{ + var distance : Double = 0.0 + var time : Double = 0.0 + var elevation : Double = 0.0 +} /*: Create a variable instance of `RunningWorkout` called `firstRun` without supplying any arguments. Print out all three properties of `firstRun`. This is a good example of when using default values is appropriate, seeing as all running workouts start with a distance, time, and elevation change of 0. */ - +var firstRun = RunningWorkout () +print (firstRun.distance , firstRun.time , firstRun.elevation) /*: Now imagine that throughout the course of the run, you go a distance of 2,396 meters in 15.3 minutes, and gain 94 meters of elevation. Update the values of `firstRun`'s properties accordingly. Print a statement about your run using the values of each property. */ + firstRun.distance = 2.396 + firstRun.time = 15.3 + firstRun.elevation = 94 +print ("you go a distance of \(firstRun.distance) meters in \(firstRun.time) minutes, and gain \(firstRun.elevation) meters of elevation.") //: [Previous](@previous) | page 2 of 10 | [Next: Exercise - Memberwise and Custom Initializers](@next) diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/3. Exercise - Memberwise and Custom Initializers.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/3. Exercise - Memberwise and Custom Initializers.xcplaygroundpage/Contents.txt similarity index 65% rename from Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/3. Exercise - Memberwise and Custom Initializers.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/3. Exercise - Memberwise and Custom Initializers.xcplaygroundpage/Contents.txt index 6afc9c6..b10a015 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/3. Exercise - Memberwise and Custom Initializers.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/3. Exercise - Memberwise and Custom Initializers.xcplaygroundpage/Contents.txt @@ -3,16 +3,30 @@ If you completed the exercise Structs, Instances, and Default Values, you created a `GPS` struct with default values for properties of `latitude` and `longitude`. Create your `GPS` struct again, but this time do not provide default values. Both properties should be of type `Double`. */ - +struct GPS +{ + var latitude : Double + var longitude : Double +} /*: Now create a constant instance of `GPS` called `somePlace`, and use the memberwise initializer to set `latitude` to 51.514004, and `longitude` to 0.125226. Print the values of `somePlace`'s properties. */ - +var somePlace = GPS ( latitude: 51.514004 , longitude: 0.125226 ) +print(somePlace.latitude , somePlace.longitude) /*: In Structs, Instance, and Default Values, you also created a `Book` struct with properties `title`, `author`, `pages`, and `price`. Create this struct again without default values. Give each property the appropriate type. Declare your `favoriteBook` instance and pass in the values of your favorite book using the memberwise initializer. Print a statement about your favorite book using `favoriteBook`'s properties. */ + struct Book{ + var title : String + var author : String + var pages : Int + var price : Double +} +var favoriteBook = Book ( title :"Women from mars and men from venus", author : "actually don't know" , pages : 100 , price : 50.5 ) + + print("My favoriteBook is \(favoriteBook.title) written by \(favoriteBook.author), which is \(favoriteBook.pages) pages and its price is \(favoriteBook.price)") /*: @@ -22,16 +36,32 @@ - Example: If you use the initializer for inches to pass in a height of 65, the initializer should set `heightInInches` to 65 and `heightInCentimeters` to 165.1. */ - +struct Height +{ + var heightInInches : Double + var heightInCentimeters : Double + init (heightInInches : Double) + { + self.heightInInches = heightInInches + heightInCentimeters = heightInInches * 2.54 + } + init (heightInCentimeters : Double) + { + self.heightInCentimeters = heightInCentimeters + heightInInches = heightInCentimeters / 2.54 + } +} /*: Now create a variable instance of `Height` called `someonesHeight`. Use the initializer for inches to set the height to 65. Print out the property for height in centimeters and verify that it is equal to 165.1. */ - +var someonesHeight = Height (heightInInches : 65) +print (someonesHeight.heightInCentimeters) /*: Now create a variable instance of `Height` called `myHeight` and initialize it with your own height. Verify that both `heightInInches` and `heightInCentimeters` are accurate. */ - +var myHeight = Height (heightInCentimeters : 170) +print ("my height is \(myHeight.heightInCentimeters) centimeters equals \(myHeight.heightInInches) inches") //: [Previous](@previous) | page 3 of 10 | [Next: App Exercise - Users and Distance](@next) diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/4. App Exercise - Users and Distance.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/4. App Exercise - Users and Distance.xcplaygroundpage/Contents.txt similarity index 74% rename from Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/4. App Exercise - Users and Distance.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/4. App Exercise - Users and Distance.xcplaygroundpage/Contents.txt index 751d413..a1fef76 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/4. App Exercise - Users and Distance.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/4. App Exercise - Users and Distance.xcplaygroundpage/Contents.txt @@ -5,28 +5,54 @@ For most apps you'll need to have a data structure to hold information about a user. Create a `User` struct that has properties for basic information about a user. At a minimum, it should have properties to represent a user's name, age, height, weight, and activity level. You could do this by having `name` be a `String`, `age` be an `Int`, `height` and `weight` be of type `Double`, and `activityLevel` be an `Int` that will represent a scoring 1-10 of how active they are. Implement this now. */ + struct User + { +var name : String +var age : Int +var height : Double +var weight : Double +var activityLevel : Int + } /*: Create a variable instance of `User` and call it your name. Use the memberwise initializer to pass in information about yourself. Then print out a description of your `User` instance using the instance's properties. */ - +var tasneem = User (name : "Tasneem" , age : 29 , height : 170 , weight : 50 , activityLevel : 5) +print (tasneem.name, tasneem.age, tasneem.height, tasneem.weight, tasneem.activityLevel) /*: In previous app exercises, you've worked with distance in the fitness tracking app example as a simple number. However, distance can be represented using a variety of units of measurement. Create a `Distance` struct that will represent distance in various units of measurement. At a minimum, it should have a `meters` property and a `feet` property. Create a custom initializer corresponding to each property (i.e. if you only have the two properties for meters and feet you will then have two initializers) that will take in a distance in one unit of measurement and assign the correct value to both units of measurements. Hint: *1 meter = 3.28084 feet*. - Example: If you use the initializer for meters and pass in a distance of 1600, the initializer should set `meters` to 1600 and `feet` to 5249.344. */ + struct distance + { + var meters : Double + var feet : Double + init (meters : Double) + { + self.meters = meters + feet = meters * 3.28084 + } + init(feet: Double) + { + self.feet = feet + meters = feet / 3.28084 + } + } /*: Now create an instance of `Distance` called `mile`. Use the initializer for meters to set the distance to 1600. Print out the property for feet and verify that it is equal to 5249.344. */ - +var mile = distance (meters : 1600) +print (mile.feet) /*: Now create another instance of `Distance` and give it some other distance. Ensure that both properties are set correctly. */ - +var mymile = distance (meters : 2000) +print ("\(mymile.meters) meters equals \(mymile.feet) feet") //: [Previous](@previous) | page 4 of 10 | [Next: Exercise - Methods](@next) diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/5. Exercise - Methods.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/5. Exercise - Methods.xcplaygroundpage/Contents.txt similarity index 61% rename from Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/5. Exercise - Methods.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/5. Exercise - Methods.xcplaygroundpage/Contents.txt index d90b425..be8141c 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/5. Exercise - Methods.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/5. Exercise - Methods.xcplaygroundpage/Contents.txt @@ -8,7 +8,13 @@ struct Book { var author: String var pages: Int var price: Double + func description () + { + print ("\(title) written by \(author) has \(pages) pages and prciced \(price)") + } } +var favoriteBook = Book ( title :"Women from mars and men from venus", author : "actually don't know" , pages : 100 , price : 50.5 ) +favoriteBook.description() /*: A `Post` struct has been created for you below, representing a generic social media post. Add a mutating method on `Post` called `like` that will increment `likes` by one. Then create an instance of `Post` and call `like()` on it. Print out the `likes` property before and after calling the method to see whether or not the value was incremented. */ @@ -16,5 +22,19 @@ struct Post { var message: String var likes: Int var numberOfComments: Int + init (message : String) + { + self.message = message + likes = 0 + numberOfComments = 0 + } + mutating func like () + { + likes += 1 + } } +var likedPost = Post ( message : "Good Morning") +print (likedPost.likes) +likedPost.like() +print (likedPost.likes) //: [Previous](@previous) | page 5 of 10 | [Next: App Exercise - Workout Functions](@next) diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/6. App Exercise - Workout Functions.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/6. App Exercise - Workout Functions.xcplaygroundpage/Contents.txt similarity index 70% rename from Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/6. App Exercise - Workout Functions.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/6. App Exercise - Workout Functions.xcplaygroundpage/Contents.txt index 774deca..f36cbca 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/6. App Exercise - Workout Functions.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/6. App Exercise - Workout Functions.xcplaygroundpage/Contents.txt @@ -9,12 +9,28 @@ struct RunningWorkout { var distance: Double var time: Double var elevation: Double + func postWorkoutStats () + { + print ("you went a distance of \(distance) meters in \(time) minutes, and gain \(elevation) meters of elevation.") + } } +var myWorkout = RunningWorkout (distance : 100 , time : 30.5 , elevation : 2000) +myWorkout.postWorkoutStats() /*: A `Steps` struct has been created for you below, representing the day's step-tracking data. It has the goal number of steps for the day and the number of steps taken so far. Create a method on `Steps` called `takeStep` that increments the value of `steps` by one. Then create an instance of `Steps` and call `takeStep()`. Print the value of the instance's `steps` property before and after the method call. */ struct Steps { var steps: Int var goal: Int + + mutating func takeStep () + { + steps += 1 + } } +var mySteps = Steps (steps : 0 , goal : 100) +print( mySteps.steps) +mySteps.takeStep() +print( mySteps.steps) + //: [Previous](@previous) | page 6 of 10 | [Next: Exercise - Computed Properties and Property Observers](@next) diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/7. Exercise - Computed Properties and Property Observers.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/7. Exercise - Computed Properties and Property Observers.xcplaygroundpage/Contents.txt similarity index 92% rename from Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/7. Exercise - Computed Properties and Property Observers.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/7. Exercise - Computed Properties and Property Observers.xcplaygroundpage/Contents.txt index cb8e9f0..a40cfd2 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/7. Exercise - Computed Properties and Property Observers.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/7. Exercise - Computed Properties and Property Observers.xcplaygroundpage/Contents.txt @@ -6,7 +6,13 @@ struct Rectangle { var width: Int var height: Int + var area : Int + { + return height * width + } } +var rec = Rectangle (width : 10 , height : 20) +print (rec.area) /*: In the `Height` struct below, height is represented in both inches and centimeters. However, if `heightInInches` is changed, `heightInCentimeters` should also adjust to match it. Add a `didSet` to each property that will check if the other property is what it should be, and if not, sets the proper value. If you set the value of the other property even though it already has the right value, you will end up with an infinite loop of each property setting the other. diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/8. App Exercise - Mile Times and Congratulations.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/8. App Exercise - Mile Times and Congratulations.xcplaygroundpage/Contents.txt similarity index 83% rename from Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/8. App Exercise - Mile Times and Congratulations.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/8. App Exercise - Mile Times and Congratulations.xcplaygroundpage/Contents.txt index c53c59b..02ef6e5 100644 --- a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/8. App Exercise - Mile Times and Congratulations.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/8. App Exercise - Mile Times and Congratulations.xcplaygroundpage/Contents.txt @@ -11,6 +11,11 @@ struct RunningWorkout { var distance: Double var time: Double var elevation: Double + var averageMileTime : Double { + + return distance / 1600 + + } } /*: In other app exercises, you've provided encouraging messages to the user based on how many steps they've completed. A great place to check whether or not you should display something to the user is in a property observer. @@ -19,10 +24,21 @@ struct RunningWorkout { */ struct Steps { var steps: Int + { + willSet{ + if newValue == goal + { + print ("Congratulations! you beat ur goal") + } + } + + } var goal: Int mutating func takeStep() { steps += 1 } } +var mySteps = Steps (steps : 9999 , goal : 10000) +mySteps.takeStep() //: [Previous](@previous) | page 8 of 10 | [Next: Exercise - Type Properties and Methods](@next) diff --git a/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/9. Exercise - Type Properties and Methods.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/9. Exercise - Type Properties and Methods.xcplaygroundpage/Contents.txt similarity index 100% rename from Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/9. Exercise - Type Properties and Methods.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/3 - Structures/lab/Lab - Structures.playground/Pages/9. Exercise - Type Properties and Methods.xcplaygroundpage/Contents.txt diff --git a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/1. Exercise - Define a Base Class.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/1. Exercise - Define a Base Class.xcplaygroundpage/Contents.txt similarity index 74% rename from Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/1. Exercise - Define a Base Class.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/1. Exercise - Define a Base Class.xcplaygroundpage/Contents.txt index 8576732..f73bc4d 100644 --- a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/1. Exercise - Define a Base Class.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/1. Exercise - Define a Base Class.xcplaygroundpage/Contents.txt @@ -5,11 +5,51 @@ Create a `Spaceship` class with three variable properties: `name`, `health`, and `position`. The default value of `name` should be an empty string and `health` should be 0. `position` will be represented by an `Int` where negative numbers place the ship further to the left and positive numbers place the ship further to the right. The default value of `position` should be 0. */ +class Spaceship { + var name : String + var health : Int + var position : Int + init (name : String) + { + self.name = name + health = 0 + position = 0 + } + func moveLeft () + { + position += 1 + } + func moveRight () + { + position -= 1 + } + func wasHit () + { + if (health <= 0) + { + print("Sorry. Your ship was hit one too many times. Do you want to play again?") + } + else + { + health -= 5 + } + } + +} /*: Create a `let` constant called `falcon` and assign it to an instance of `Spaceship`. After initialization, set `name` to "Falcon". */ +let falcon = Spaceship ( name : "Falcon") +falcon.moveLeft() +print (falcon.position) +falcon.moveLeft() +print (falcon.position) +falcon.moveRight() +print (falcon.position) +falcon.wasHit() + /*: diff --git a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/2. Exercise - Create a Subclass.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/2. Exercise - Create a Subclass.xcplaygroundpage/Contents.txt similarity index 75% rename from Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/2. Exercise - Create a Subclass.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/2. Exercise - Create a Subclass.xcplaygroundpage/Contents.txt index 69a1117..d82de7e 100644 --- a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/2. Exercise - Create a Subclass.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/2. Exercise - Create a Subclass.xcplaygroundpage/Contents.txt @@ -23,11 +23,39 @@ class Spaceship { } } } +class Fighter : Spaceship +{ + var weapon : String = "" + var remainingFirePower : Int = 5 + func fire () + { + if remainingFirePower > 0 + { + remainingFirePower -= 1 + } + else + { + print ("You have no more fire power.") + } + + } +} /*: Define a new class `Fighter` that inherits from `Spaceship`. Add a variable property `weapon` that defaults to an empty string and a variable property `remainingFirePower` that defaults to 5. */ - - +let destroyer = Fighter () +destroyer.weapon = "laser" +destroyer.remainingFirePower = 2 +destroyer.name = "Destroyer" +print (destroyer.position) +destroyer.moveRight() +print (destroyer.position) +destroyer.fire() +print (destroyer.remainingFirePower) +destroyer.fire() +print (destroyer.remainingFirePower) +destroyer.fire() +print (destroyer.remainingFirePower) /*: Create a new instance of `Fighter` called `destroyer`. A `Fighter` will be able to shoot incoming objects to avoid colliding with them. After initialization, set `weapon` to "Laser" and `remainingFirePower` to 10. Note that since `Fighter` inherits from `Spaceship`, it also has properties for `name`, `health`, and `position`, and has methods for `moveLeft()`, `moveRight()`, and `wasHit()` even though you did not specifically add them to the declaration of `Fighter`. Knowing that, set `name` to "Destroyer," print `position`, then call `moveRight()` and print `position` again. */ @@ -36,6 +64,7 @@ class Spaceship { /*: Try to print `weapon` on `falcon`. Why doesn't this work? Provide your answer in a comment or a print statement below, and remove any code you added that doesn't compile. */ + print ("weapon` on `falcon won't compile , Because falcon is an instance from the base class type that doesn't have weapon property ") /*: diff --git a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/3. Exercise - Override Methods and Properties.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/3. Exercise - Override Methods and Properties.xcplaygroundpage/Contents.txt similarity index 73% rename from Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/3. Exercise - Override Methods and Properties.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/3. Exercise - Override Methods and Properties.xcplaygroundpage/Contents.txt index a25449d..ccb9b6c 100644 --- a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/3. Exercise - Override Methods and Properties.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/3. Exercise - Override Methods and Properties.xcplaygroundpage/Contents.txt @@ -39,8 +39,40 @@ class Fighter: Spaceship { /*: Define a new class `ShieldedShip` that inherits from `Fighter`. Add a variable property `shieldStrength` that defaults to 25. Create a new instance of `ShieldedShip` called `defender`. Set `name` to "Defender" and `weapon` to "Cannon." Call `moveRight()` and print `position`, then call `fire()` and print `remainingFirePower`. */ +class ShieldedShip : Fighter { + var shieldStrength : Int = 25 + override func wasHit () + { + if shieldStrength > 0 + { + shieldStrength -= 5 - + } + else + { + super.wasHit() + } + } +} + let defender = ShieldedShip () + defender.name = "Defender" + defender.weapon = "Cannon" + defender.moveRight () + print (defender.position) + defender.fire() + print (defender.remainingFirePower) + defender.wasHit() + print (defender.shieldStrength , defender.health) + defender.wasHit() + print (defender.shieldStrength , defender.health) + defender.wasHit() + print (defender.shieldStrength , defender.health) + defender.wasHit() + print (defender.shieldStrength , defender.health) + defender.wasHit() + print (defender.shieldStrength , defender.health) + defender.wasHit() + print (defender.shieldStrength , defender.health) /*: Go back to your declaration of `ShieldedShip` and override `wasHit()`. In the body of the method, check to see if `shieldStrength` is greater than 0. If it is, decrement `shieldStrength` by 5. Otherwise, decrement `health` by 5. Call `wasHit()` on `defender` and print `shieldStrength` and `health`. */ diff --git a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/4. Exercise - Class Memberwise Initializers and References.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/4. Exercise - Class Memberwise Initializers and References.xcplaygroundpage/Contents.txt similarity index 81% rename from Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/4. Exercise - Class Memberwise Initializers and References.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/4. Exercise - Class Memberwise Initializers and References.xcplaygroundpage/Contents.txt index 5a9f7fa..cb3cda5 100644 --- a/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/4. Exercise - Class Memberwise Initializers and References.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/4 - Classes and Inheritance/lab/Lab - Classes.playground/Pages/4. Exercise - Class Memberwise Initializers and References.xcplaygroundpage/Contents.txt @@ -1,4 +1,4 @@ -/*: +/*: ## Exercise - Class Memberwise Initializers and References - Note: The exercises below are based on a game where a spaceship avoids obstacles in space. The ship is positioned at the bottom of a coordinate system and can only move left and right while obstacles "fall" from top to bottom. The base class `Spaceship` and subclasses `Fighter` and `ShieldedShip` have been provided for you below. You will use these to complete the exercises. @@ -7,7 +7,12 @@ class Spaceship { let name: String var health: Int var position: Int - +init ( name: String , health: Int , position: Int) +{ + self.name = name + self.health = health + self.position = position +} func moveLeft() { position -= 1 } @@ -27,6 +32,13 @@ class Spaceship { class Fighter: Spaceship { let weapon: String var remainingFirePower: Int + init ( name: String , health: Int , position: Int , weapon: String , remainingFirePower: Int) + { + super.init(name: String , health: Int , position: Int) + self.weapon = weapon + self.remainingFirePower = remainingFirePower + } + func fire() { if remainingFirePower > 0 { @@ -39,7 +51,11 @@ class Fighter: Spaceship { class ShieldedShip: Fighter { var shieldStrength: Int - +init ( name: String , health: Int , position: Int , weapon: String , remainingFirePower: Int , shieldStrength: Int) +{ + super.init(weapon , remainingFirePower) + self.shieldStrength = shieldStrength +} override func wasHit() { if shieldStrength > 0 { shieldStrength -= 5 @@ -53,6 +69,7 @@ class ShieldedShip: Fighter { Then create an instance of `Spaceship` below called `falcon`. Use the memberwise initializer you just created. The ship's name should be "Falcon." */ + let falcon = Spaceship (name : "Falcon", health : 0 , position : 0) /*: @@ -60,18 +77,23 @@ class ShieldedShip: Fighter { Then create an instance of `Fighter` below called `destroyer`. Use the memberwise initializer you just created. The ship's name should be "Destroyer." */ - +let destroyer = Fighter ( name: "Destroyer" , health: 0 , position: 0 , weapon: "laser" , remainingFirePower: 25) /*: Now go add an initializer to `ShieldedShip` that takes an argument for each property on `ShieldedShip`, `Fighter`, and `Spaceship`, and sets the properties accordingly. Remember that you can call through to the initializer on `Fighter` using `super.init`. Then create an instance of `ShieldedShip` below called `defender`. Use the memberwise initializer you just created. The ship's name should be "Defender." */ - - +let defender = ShieldedShip(name: "Defender" , health: 0 , position: 0 , weapon: "test" , remainingFirePower: 30 , shieldStrength: 10) + /*: Create a new instance of `Spaceship` called `sameShip` and set it equal to `falcon`. Print out the position of `sameShip` and `falcon`, then call `moveLeft()` on `sameShip` and print out the position of `sameShip` and `falcon` again. Did both positions change? Why? If both were structs instead of classes, would it be the same? Why or why not? Provide your answer in a comment or print statement below. */ + let sameShip = falcon + print (sameShip.position , falcon.position) + sameShip.moveLeft() + print (sameShip.position , falcon.position) + /*: diff --git a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/1. Exercise - Arrays.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/1. Exercise - Arrays.xcplaygroundpage/Contents.txt similarity index 74% rename from Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/1. Exercise - Arrays.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/1. Exercise - Arrays.xcplaygroundpage/Contents.txt index fbcf943..1c8b5f6 100644 --- a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/1. Exercise - Arrays.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/1. Exercise - Arrays.xcplaygroundpage/Contents.txt @@ -3,31 +3,37 @@ Assume you are an event coordinator for a community charity event and are keeping a list of who has registered. Create a variable `registrationList` that will hold strings. It should be empty after initialization. */ - +var registrationList = [String] () /*: Your friend Sara is the first to register for the event. Add her name to `registrationList` using the `append(_:)` method. Print the contents of the collection. */ - +registrationList.append ("Sara") +print (registrationList) /*: Add four additional names into the array using the `+=` operator. All of the names should be added in one step. Print the contents of the collection. */ - +registrationList += ["Tasneem" , "Ibrahim" , "Kenzy" , "Yousra"] +print (registrationList) /*: Use the `insert(_:at:)` method to add `Charlie` into the array as the second element. Print the contents of the collection. */ - +registrationList.insert("Charlie" , at : 1) +print (registrationList) /*: Someone had a conflict and decided to transfer her registration to someone else. Use array subscripting to change the sixth element to `Rebecca`. Print the contents of the collection. */ - +registrationList[5] = "Rebecca" +print (registrationList) /*: Call `removeLast()` on `registrationList`. If done correctly, this should remove `Rebecca` from the collection. Store the result of `removeLast()` into a new constant `deletedItem`, then print `deletedItem`. */ - +let deletedItem = registrationList.removeLast() +print (deletedItem) +print (registrationList) //: page 1 of 4 | [Next: App Exercise - Activity Challenge](@next) diff --git a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/2. App Exercise - Activity Challenge.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/2. App Exercise - Activity Challenge.xcplaygroundpage/Contents.txt similarity index 72% rename from Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/2. App Exercise - Activity Challenge.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/2. App Exercise - Activity Challenge.xcplaygroundpage/Contents.txt index d9e7443..400ccc6 100644 --- a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/2. App Exercise - Activity Challenge.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/2. App Exercise - Activity Challenge.xcplaygroundpage/Contents.txt @@ -7,26 +7,43 @@ Using arrays of type `String`, create at least two lists, one for walking challenges, and one for running challenges. Each should have at least two challenges and should be initialized using an array literal. Feel free to create more lists for different activities. */ +var walkingChallenges = ["Walk 3 mins" , "Walk 30 minutes"] +var runningChallenges = ["Run 3 mins" , "Run 30 minutes"] /*: In your app you want to show all of these lists on the same screen grouped into sections. Create a `challenges` array that holds each of the lists you have created (it will be an array of arrays). Using `challenges`, print the first element in the second challenge list. */ - +var Challenges = [walkingChallenges , runningChallenges] +print (Challenges) +print (Challenges[1][0]) /*: All of the challenges will reset at the end of the month. Use the `removeAll` to remove everything from `challenges`. Print `challenges`. */ - +Challenges.removeAll() +print (Challenges) /*: Create a new array of type `String` that will represent challenges a user has committed to instead of available challenges. It can be an empty array or have a few items in it. */ - - +//var committedChallenges = [String] () +//var committedChallenges = ["Run for 3 mins"] +var committedChallenges = ["Run for 3 mins", "Walk for 30 mins"] /*: Write an if statement that will use `isEmpty` to check if there is anything in the array. If there is not, print a statement asking the user to commit to a challenge. Add an else-if statement that will print "The challenge you have chosen is " if the array count is exactly 1. Then add an else statement that will print "You have chosen multiple challenges." */ - +if committedChallenges.isEmpty +{ + print ("Please commit to a challenge") +} +else if committedChallenges.count == 1 +{ + print ( "The challenge you have chosen is \(committedChallenges[0])") +} +else +{ + print ("You have chosen multiple challenges.") +} //: [Previous](@previous) | page 2 of 4 | [Next: Exercise - Dictionaries](@next) diff --git a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/3. Exercise - Dictionaries.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/3. Exercise - Dictionaries.xcplaygroundpage/Contents.txt similarity index 74% rename from Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/3. Exercise - Dictionaries.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/3. Exercise - Dictionaries.xcplaygroundpage/Contents.txt index e3d52d8..3409881 100644 --- a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/3. Exercise - Dictionaries.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/3. Exercise - Dictionaries.xcplaygroundpage/Contents.txt @@ -3,21 +3,32 @@ Create a variable `[String: Int]` dictionary that can be used to look up the number of days in a particular month. Use a dictionary literal to initialize it with January, February, and March. January contains 31 days, February has 28, and March has 31. Print the dictionary. */ +var calender = ["January" : 31 , "Feb" : 28 , "March" : 31] +print (calender) /*: Using subscripting syntax to add April to the collection with a value of 30. Print the dictionary. */ - +calender ["April"] = 30 +print (calender) /*: It's a leap year! Update the number of days in February to 29 using the `updateValue(_:, forKey:)` method. Print the dictionary. */ +calender.updateValue(29 , forKey: "Feb") +print (calender) + /*: Use if-let syntax to retrieve the number of days under "January". If the value is there, print "January has 31 days", where 31 is the value retrieved from the dictionary. */ +if let oldValue = calender["January"] +{ + print( "January has 31 days") +} + /*: @@ -25,11 +36,15 @@ */ let shapesArray = ["Circle", "Square", "Triangle"] let colorsArray = ["Red", "Green", "Blue"] +var shapesArray : [String , [String]] = [ shapesArray[0] : colorsArray , shapesArray [1] : colorsArray , shapesArray[2] : colorsArray ] +print (shapesArray) /*: Print the last element of `colorsArray`, accessing it through the dictionary you've created. You'll have to use if-let syntax or the force unwrap operator to unwrap what is returned from the dictionary before you can access an element of the array. */ +let colors = Array(shapesArray.values) +print (colors[0][3]) //: [Previous](@previous) | page 3 of 4 | [Next: App Exercise - Pacing](@next) diff --git a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/4. App Exercises - Pacing.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/4. App Exercises - Pacing.xcplaygroundpage/Contents.txt similarity index 79% rename from Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/4. App Exercises - Pacing.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/4. App Exercises - Pacing.xcplaygroundpage/Contents.txt index 6b5ba08..5ad7ee4 100644 --- a/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/4. App Exercises - Pacing.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/5 - Collections/lab/Lab - Collections.playground/Pages/4. App Exercises - Pacing.xcplaygroundpage/Contents.txt @@ -1,4 +1,4 @@ -/*: +/*: ## App Exercise - Pacing >These exercises reinforce Swift concepts in the context of a fitness tracking app. @@ -7,26 +7,57 @@ Create a dictionary `paces` of type [String: Double] and assign it a dictionary literal with "Easy", "Medium", and "Fast" keys corresponding to values of 10.0, 8.0, and 6.0. These numbers correspond to mile pace in minutes. Print the dictionary. */ - +var paces : [String : Double] = [ "Easy" : 10.0 , "Medium" : 8.0 , "Hard" : 6.0] +for (level , pace ) in paces +{ + print (level , pace) +} /*: Add a new key/value pair to the dictionary. The key should be "Sprint" and the value should be 4.0. Print the dictionary. */ +paces ["Sprint"] = 4.0 +for (level , pace ) in paces +{ + print (level , pace) +} /*: Imagine the user in question gets faster over time and decides to update his/her pacing on runs. Update the values of "Medium" and "Fast" to 7.5 and 5.8, respectively. Print the dictionary. */ + paces.updateValue (7.5 , forKey : "Medium") + paces.updateValue (5.8 , forKey : "Fast") +//print ( oldValue) +for (level , pace ) in paces +{ + print (level , pace) +} /*: Imagine the user in question decides not to store "Sprint" as one his/her regular paces. Remove "Sprint" from the dictionary. Print the dictionary. */ - +paces ["Sprint"] = nil +for (level , pace ) in paces +{ + print (level , pace) +} /*: When a user chooses a pace, you want the app to print a statement stating that it will keep him/her on pace. Imagine a user chooses "Medium." Accessing the value from the dictionary, print a statement saying "Okay! I'll keep you at a minute mile pace." */ + var choice = "Medium" + for (level , _ ) in paces +{ + if choice == level + { + print ("Okay! I'll keep you at a \(paces[ "Medium"]) minute mile pace.") + break + } + +} + /*: diff --git a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/1. Exercise - For-In Loops.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/1. Exercise - For-In Loops.xcplaygroundpage/Contents.txt similarity index 69% rename from Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/1. Exercise - For-In Loops.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/1. Exercise - For-In Loops.xcplaygroundpage/Contents.txt index a019d00..d95c9f7 100644 --- a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/1. Exercise - For-In Loops.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/1. Exercise - For-In Loops.xcplaygroundpage/Contents.txt @@ -3,17 +3,29 @@ Create a for-in loop that loops through values 1 to 100, and prints each of the values. */ + for value in 1...100 + { + print(value) + } /*: Create a for-in loop that loops through each of the characters in the `alphabet` string below, and prints each of the values alongside the index. */ let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +for (index,letter) in alphabet.enumerated() +{ + print("Index \(index) is \(letter)") +} /*: Create a `[String: String]` dictionary, where the keys are names of states and the values are their capitals. Include at least three key/value pairs in your collection, then use a for-in loop to iterate over the pairs and print out the keys and values in a sentence. */ - +var states = ["Egypt" : "Cairo" , "KSA" : "Riadah" , "USA" : "Washintgon"] +for (state , capital ) in states +{ + print ("\(state) 's capital is \(capital)") +} //: page 1 of 6 | [Next: App Exercise - Movements](@next) diff --git a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/2. App Exercise - Movements.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/2. App Exercise - Movements.xcplaygroundpage/Contents.txt similarity index 86% rename from Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/2. App Exercise - Movements.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/2. App Exercise - Movements.xcplaygroundpage/Contents.txt index 20d0853..aed2ee9 100644 --- a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/2. App Exercise - Movements.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/2. App Exercise - Movements.xcplaygroundpage/Contents.txt @@ -6,12 +6,19 @@ Suppose your app contains a list of different movements that can be tracked. You want to display each item in the list to the user. Use a for-in loop to loop through `movements` below and print each movement. */ let movements: [String] = ["Walking", "Running", "Swimming", "Cycling", "Skiing", "Climbing"] +for movement in movements +{ + print (movement) +} /*: Now suppose your app uses a dictionary to keep track of your average heart rate during each of the movements in `movements`. The keys correspond to the movements listed above, and the values correspond to the average heart rate that your fitness tracker has monitored during the given movement. Loop through `movementHeartRates` below, printing statements telling the user his/her average heart rate during each exercise. */ var movementHeartRates: [String: Int] = ["Walking": 85, "Running": 120, "Swimming": 130, "Cycling": 128, "Skiing": 114, "Climbing": 129] - +for (Movement , average) in movementHeartRates +{ + print ("you avearge heart rate through \(Movement) is \(average)") +} //: [Previous](@previous) | page 2 of 6 | [Next: Exercise - While Loops](@next) diff --git a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/3. Exercise - While Loops.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/3. Exercise - While Loops.xcplaygroundpage/Contents.txt similarity index 70% rename from Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/3. Exercise - While Loops.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/3. Exercise - While Loops.xcplaygroundpage/Contents.txt index e03b910..ad2e5d5 100644 --- a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/3. Exercise - While Loops.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/3. Exercise - While Loops.xcplaygroundpage/Contents.txt @@ -4,6 +4,17 @@ import Foundation Create a while loop that simulates rolling a 6-sided dice repeatedly until a 1 is rolled. After each roll, print the value. (Hint: use `Int.random(in: 1...6)` to generate a random number between 1 and 6). */ +var notOne : Bool = false +while !notOne +{ + let Num : Int = Int.random(in: 1...6) + print (Num) + if Num == 1 + { + notOne = true + break; + } +} //: [Previous](@previous) | page 3 of 6 | [Next: App Exercise - While Loops](@next) diff --git a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/4. App Exercise - Running Cadence.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/4. App Exercise - Running Cadence.xcplaygroundpage/Contents.txt similarity index 78% rename from Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/4. App Exercise - Running Cadence.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/4. App Exercise - Running Cadence.xcplaygroundpage/Contents.txt index 120197b..aff390e 100644 --- a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/4. App Exercise - Running Cadence.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/4. App Exercise - Running Cadence.xcplaygroundpage/Contents.txt @@ -10,12 +10,31 @@ import Foundation */ let cadence: Double = 180 var testSteps = 0 +while testSteps != 10 +{ + print ("Take a step") + testSteps += 1 + Thread.sleep(forTimeInterval: 60/cadence) +} +//Thread.sleep(forTimeInterval: 60/cadence) /*: Recreate the above cadence example using a repeat-while loop. */ testSteps = 0 +var again = true +while again +{ +while testSteps != 10 +{ + print ("Take a step") + testSteps += 1 + Thread.sleep(forTimeInterval: 60/cadence) +} +testSteps = 0 +} + //: [Previous](@previous) | page 4 of 6 | [Next: Exercise - Control Transfer Statements](@next) diff --git a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/5. Exercise - Control Transfer Statements.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/5. Exercise - Control Transfer Statements.xcplaygroundpage/Contents.txt similarity index 70% rename from Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/5. Exercise - Control Transfer Statements.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/5. Exercise - Control Transfer Statements.xcplaygroundpage/Contents.txt index bc9d7c4..e8bf205 100644 --- a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/5. Exercise - Control Transfer Statements.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/5. Exercise - Control Transfer Statements.xcplaygroundpage/Contents.txt @@ -4,11 +4,37 @@ Create a for-in loop that will loop through `alphabet`. Inside the loop, print every other letter by continuing to the next iteration if you are on a letter you do not wish to print. (Hint: You can use the modulo operator to only print even indexed characters). */ let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +for (index , letter) in alphabet.enumerated() +{ + if index % 2 == 0 + { + print (letter) + } + else + { + continue + } +} /*: Create a `[String: String]` dictionary where the keys are names of states and the values are their capitals. Include at least three key/value pairs in your collection, with one of them being your home state. Now loop through this dictionary again, printing out the keys and values in a sentence, but add an if statement that will check if the current iteration is your home state. If it is, print("I found my home!") and break out of the loop. */ + var states = [ "KSA" : "Riadah" , "Egypt" : "Cairo" , "USA" : "Washintgon"] +for (state , capital ) in states +{ + print ("\(state) 's capital is \(capital)") + if state == "Egypt" + { + print("I found my home!") + break + + } + else + { + continue + } +} //: [Previous](@previous) | page 5 of 6 | [Next: App Exercise - Finding Movements](@next) diff --git a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/6. App Exercise - Finding Movements.xcplaygroundpage/Contents.swift b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/6. App Exercise - Finding Movements.xcplaygroundpage/Contents.txt similarity index 92% rename from Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/6. App Exercise - Finding Movements.xcplaygroundpage/Contents.swift rename to Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/6. App Exercise - Finding Movements.xcplaygroundpage/Contents.txt index 1649e6d..419d8e3 100644 --- a/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/6. App Exercise - Finding Movements.xcplaygroundpage/Contents.swift +++ b/Student Resources/2 - Introduction to UIKit/6 - Loops/lab/Lab - Loops.playground/Pages/6. App Exercise - Finding Movements.xcplaygroundpage/Contents.txt @@ -1,4 +1,4 @@ -/*: +/*: ## App Exercise - Finding Movements >These exercises reinforce Swift concepts in the context of a fitness tracking app. @@ -11,7 +11,17 @@ let lowHR = 110 let highHR = 125 var movementHeartRates: [String: Int] = ["Walking": 85, "Running": 120, "Swimming": 130, "Cycling": 128, "Skiing": 114, "Climbing": 129] - +for (Movement , average) in movementHeartRates +{ + if average <= lowHR || average >= highHR + { + continue + } + else + { + print ("you could go \(Movement)") + } +} /*: diff --git a/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/1. Exercise - Optionals.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/1. Exercise - Optionals.xcplaygroundpage/Contents.txt similarity index 100% rename from Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/1. Exercise - Optionals.xcplaygroundpage/Contents.swift rename to Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/1. Exercise - Optionals.xcplaygroundpage/Contents.txt diff --git a/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/2. App Exercise - Finding a Heart Rate.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/2. App Exercise - Finding a Heart Rate.xcplaygroundpage/Contents.txt similarity index 87% rename from Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/2. App Exercise - Finding a Heart Rate.xcplaygroundpage/Contents.swift rename to Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/2. App Exercise - Finding a Heart Rate.xcplaygroundpage/Contents.txt index 746b8fc..59859a4 100644 --- a/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/2. App Exercise - Finding a Heart Rate.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/1 - Optionals/lab/Lab - Optionals.playground/Pages/2. App Exercise - Finding a Heart Rate.xcplaygroundpage/Contents.txt @@ -7,11 +7,14 @@ Declare a variable `heartRate` of type `Int?` and set it to `nil`. Print the value. */ - +var heartRate : Int? = nil +print(heartRate) /*: In this example, if the user fixes the positioning of the heart rate monitor, the app may get a proper heart rate reading. Below, update the value of `heartRate` to 74. Print the value. */ +heartRate = 74 +print(heartRate) /*: @@ -21,6 +24,12 @@ let oldHR1 = 80 let oldHR2 = 76 let oldHR3 = 79 let oldHR4 = 70 +if let hRate = heartRate! +{ + heartRate = hRate +} +var hrAverage = (oldHR1 + oldHR2 + oldHR3 + oldHR4 + heartRate) / 5 +print (hrAverage) /*: If you didn't unwrap the value of `heartRate`, you've probably noticed that you cannot perform mathematical operations on an optional value. You will first need to unwrap `heartRate`. diff --git a/Student Resources/3 - Navigation and Workflows/2 - Type Casting/lab/Lab - Type Casting.playground/Pages/1. Exercise - Type Casting and Inspection.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/2 - Type Casting/lab/Lab - Type Casting.playground/Pages/1. Exercise - Type Casting and Inspection.xcplaygroundpage/Contents.txt similarity index 100% rename from Student Resources/3 - Navigation and Workflows/2 - Type Casting/lab/Lab - Type Casting.playground/Pages/1. Exercise - Type Casting and Inspection.xcplaygroundpage/Contents.swift rename to Student Resources/3 - Navigation and Workflows/2 - Type Casting/lab/Lab - Type Casting.playground/Pages/1. Exercise - Type Casting and Inspection.xcplaygroundpage/Contents.txt diff --git a/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/1. Exercise - Scope.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/1. Exercise - Scope.xcplaygroundpage/Contents.txt similarity index 67% rename from Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/1. Exercise - Scope.xcplaygroundpage/Contents.swift rename to Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/1. Exercise - Scope.xcplaygroundpage/Contents.txt index fb002fb..fee8adb 100644 --- a/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/1. Exercise - Scope.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/1. Exercise - Scope.xcplaygroundpage/Contents.txt @@ -8,6 +8,7 @@ for _ in 0..<10 { print("The value of foo is \(foo)") } //print("The value of foo is \(foo)") +//Because foo is declared inside the for loop so its a local ariable available only inside the loop. /*: @@ -19,17 +20,43 @@ for _ in 0..<10 { print("The value of x is \(x)") } print("The final value of x is \(x)") +// because x is declared ouside the loop so its aglobal variable that can be used anywhere inside the code , x defined global and modified to be local inside the loop + + /*: In the body of the function `greeting` below, use variable shadowing when unwrapping `greeting`. If `greeting` is successfully unwrapped, print a statement that uses the given greeting to greet the given name (i.e. if `greeting` successfully unwraps to have the value "Hi there" and `name` is `Sara`, print "Hi there, Sara."). Otherwise, use "Hello" to print a statement greeting the given name. Call the function twice, once passing in a value for greeting, and once passing in `nil`. */ func greeting(greeting: String?, name: String) { + if let greeting = greeting + { + print("\(greeting), \(name)") + } + else + { + print ("Hello \(name)") + } } +greeting (greeting : "Hi there" , name : "Tasneem") +//greeting ( name : "Tasneem"), can't do it how to call a function missing argument ? + /*: Create a class called `Car`. It should have properties for `make`, `model`, and `year` that are of type `String`, `String`, and `Int`, respectively. Since this is a class, you'll need to write your own memberwise initializer. Use shadowing when naming parameters in your initializer. */ + class Car + { + var make : String + var model : String + var year : Int + init ( make : String , model : String , year : Int) + { + self.make = make + self.model = model + self.year = year + } + } //: page 1 of 2 | [Next: App Exercise - Step Competition](@next) diff --git a/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/2. App Exercise - Step Competition.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/2. App Exercise - Step Competition.xcplaygroundpage/Contents.txt similarity index 90% rename from Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/2. App Exercise - Step Competition.xcplaygroundpage/Contents.swift rename to Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/2. App Exercise - Step Competition.xcplaygroundpage/Contents.txt index a4c5c73..c732acb 100644 --- a/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/2. App Exercise - Step Competition.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/4 - Scope/lab/Lab - Scope.playground/Pages/2. App Exercise - Step Competition.xcplaygroundpage/Contents.txt @@ -1,4 +1,4 @@ -/*: +/*: ## App Exercise - Step Competition >These exercises reinforce Swift concepts in the context of a fitness tracking app. @@ -8,6 +8,21 @@ struct User { var name: String var stepsToday: Int + init ( name: String , stepsToday: Int) + { + self.name = name + self.stepsToday = stepsToday + } + init ( name: String? , stepsToday: Int?) + { + guard let name = name || stepsToday = stepsToday + { + return nil + } + + self.name = name + self.stepsToday = stepsToday + } } let stepMaster = User(name: "StepMaster", stepsToday: 8394) @@ -21,16 +36,18 @@ let competitors = [stepMaster, activeSitter, monsterWalker] At that point, the goal is to assign the user with the higher score to `topCompetitor`. However, the code generates a compiler error because, due to improper variable shadowing, `topCompetitor` has a narrower scope than it should if it is going to be reassigned. Fix the compiler error below and call `getWinner(competitors:)`, passing in the array `competitors`. Print the `name` property of the returned `User` object. You'll know that you fixed the function properly if the user returned is `activeSitter`. */ func getWinner(competitors: [User]) -> User? { - var topCompetitor: User? + for competitor in competitors { + var topCompetitor: User? = competitor if let topCompetitor = topCompetitor { if competitor.stepsToday > topCompetitor.stepsToday { topCompetitor = competitor } - } else { + else { topCompetitor = competitor } + } } return topCompetitor } diff --git a/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/1. Exercise - Enumerations.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/1. Exercise - Enumerations.xcplaygroundpage/Contents.txt similarity index 66% rename from Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/1. Exercise - Enumerations.xcplaygroundpage/Contents.swift rename to Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/1. Exercise - Enumerations.xcplaygroundpage/Contents.txt index dc7d938..4c9e6c4 100644 --- a/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/1. Exercise - Enumerations.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/1. Exercise - Enumerations.xcplaygroundpage/Contents.txt @@ -3,31 +3,68 @@ Define a `Suit` enum with four possible cases: `clubs`, `spades`, `diamonds`, and `hearts`. */ - +enum Suit { + case clubs + case spades + case diamonds + case hearts +} /*: Imagine you are being shown a card trick and have to draw a card and remember the suit. Create a variable instance of `Suit` called `cardInHand` and assign it to the `hearts` case. Print out the instance. */ - +var cardInHand : Suit = .hearts +print (cardInHand) /*: Now imagine you have to put back the card you drew and draw a different card. Update the variable to be a spade instead of a heart. */ - +cardInHand = .spades /*: Imagine you are writing an app that will display a fun fortune (i.e. something like "You will soon find what you seek.") based on cards drawn. Write a function called `getFortune(cardSuit:)` that takes a parameter of type `Suit`. Inside the body of the function, write a switch statement based on the value of `cardSuit`. Print a different fortune for each `Suit` value. Call the function a few times, passing in different values for `cardSuit` each time. */ +func getFortune (cardSuit: Suit) +{ + switch (cardSuit) + { + case .clubs : + print (" You will soon find what you seek.") + + case .hearts : + + print ("u'll get married") + case .spades : + + print ("u'll get promoted") + case .diamonds : + + print ("u'll pass the course") + } +} +var testCards : Suit = .hearts +getFortune(cardSuit : testCards) +getFortune(cardSuit : cardInHand) /*: Create a `Card` struct below. It should have two properties, one for `suit` of type `Suit` and another for `value` of type `Int`. */ + enum Value + { + case four , five , six , seven , eight , nine , ten , jack , queen , king + } + struct Card + { + var suit : Suit + var value : Value + } /*: How many values can playing cards have? How many values can `Int` be? It would be safer to have an enum for the card's value as well. Inside the struct above, create an enum for `Value`. It should have cases for `ace`, `two`, `three`, `four`, `five`, `six`, `seven`, `eight`, `nine`, `ten`, `jack`, `queen`, `king`. Change the type of `value` from `Int` to `Value`. Initialize two `Card` objects and print a statement for each that details the card's value and suit. */ - +var cardOne = Card (suit : .hearts , value : .seven) +print (cardOne.suit , cardOne.value) //: page 1 of 2 | [Next: App Exercise - Swimming Workouts](@next) diff --git a/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/2. App Exercise - Swimming Workouts.xcplaygroundpage/Contents.swift b/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/2. App Exercise - Swimming Workouts.xcplaygroundpage/Contents.txt similarity index 72% rename from Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/2. App Exercise - Swimming Workouts.xcplaygroundpage/Contents.swift rename to Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/2. App Exercise - Swimming Workouts.xcplaygroundpage/Contents.txt index de34f32..a0ee063 100644 --- a/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/2. App Exercise - Swimming Workouts.xcplaygroundpage/Contents.swift +++ b/Student Resources/3 - Navigation and Workflows/5 - Enumerations/lab/Lab - Enumerations.playground/Pages/2. App Exercise - Swimming Workouts.xcplaygroundpage/Contents.txt @@ -1,16 +1,46 @@ -/*: +/*: ## App Exercise - Swimming Workouts >These exercises reinforce Swift concepts in the context of a fitness tracking app. Previous app exercises have introduced the idea that your fitness tracking app may allow users to track swimming workouts. Create a `SwimmingWorkout` struct below with properties for `distance`, `time`, and `stroke`. `distance` and `time` should be of type `Double` and will represent distance in meters and time in seconds, and `stroke` should be of type `String`. */ + struct SwimmingWorkout + { + enum Stroke + { case freestyle , butterfly , backstroke , breaststroke} + var distance : Double + var time : Double + var stroke : Stroke + var freestyleWorkouts = [SwimmingWorkout]() + /*var butterflyWorkouts = [SwimmingWorkout]() + var backstrokeWorkouts = [SwimmingWorkout]() + var breaststrokeWorkouts = [SwimmingWorkout]() + */ + func save () + { + switch stroke + { + case .freestyle : + freestyleWorkouts.append (stroke) + case .butterfly : + butterflyWorkouts.append (stroke) + case .backstroke : + backstrokeWorkouts.append (stroke) + case .breaststroke : + breaststrokeWorkouts.append (stroke) + } + } + + } /*: Allowing `stroke` to be of type `String` isn't very type-safe. Inside the `SwimmingWorkout` struct, create an enum called `Stroke` that has cases for `freestyle`, `butterfly`, `backstroke`, and `breaststroke`. Change the type of `stroke` from `String` to `Stroke`. Create two instances of `SwimmingWorkout` objects. */ - +var workoutOne = SwimmingWorkout(distance : 100.1 , time : 30 , stroke : .freestyle, freestyleWorkouts : ) +var workoutTwo = SwimmingWorkout(distance : 2.1 , time : 2 , stroke : .backstroke) +//print (workoutOne.freestyleWorkouts ) /*: Now imagine you want to log swimming workouts separately based on the swimming stroke. You might use arrays as static variables on `SwimmingWorkout` for this. Add four static variables, `freestyleWorkouts`, `butterflyWorkouts`, `backstrokeWorkouts`, and `breaststrokeWorkouts`, to `SwimmingWorkout` above. Each should be of type `[SwimmingWorkout]` and should default to empty arrays. @@ -20,6 +50,7 @@ /*: Now add an instance method to `SwimmingWorkout` called `save()` that takes no parameters and has no return value. This method will add its instance to the static array on `SwimmingWorkout` that corresponds to its swimming stroke. Inside `save()` write a switch statement that switches on the instance's `stroke` property, and appends `self` to the proper array. Call save on the two instances of `SwimmingWorkout` that you created above, and then print the array(s) to which they should have been added to see if your `save` method works properly. */ + // i'd like to declare the arrays optionals so i don't have to initialize them in the instance but i can't do it /*: diff --git a/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/1. Exercise - Adopt Protocols.xcplaygroundpage/Contents.swift b/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/1. Exercise - Adopt Protocols.xcplaygroundpage/Contents.txt similarity index 60% rename from Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/1. Exercise - Adopt Protocols.xcplaygroundpage/Contents.swift rename to Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/1. Exercise - Adopt Protocols.xcplaygroundpage/Contents.txt index f54a018..b8fc170 100644 --- a/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/1. Exercise - Adopt Protocols.xcplaygroundpage/Contents.swift +++ b/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/1. Exercise - Adopt Protocols.xcplaygroundpage/Contents.txt @@ -3,22 +3,57 @@ Create a `Human` class with two properties: `name` of type `String`, and `age` of type `Int`. You'll need to create a memberwise initializer for the class. Initialize two `Human` instances. */ +class Human : CustomStringConvertible , Equatable , Comparable +{ + var name : String + var age : Int + init (name : String , age : Int) + {self.name = name + self.age = age} + var description : String + { + return "Human (name : \(name) , age : \(age))" + } + static func == ( lhs : Human , rhs : Human)-> Bool + { + return lhs.name == rhs.name && lhs.age == rhs.age + } + static func < ( lhs : Human , rhs : Human)-> Bool + { + return lhs.age < rhs.age + } +} +var Humanone = Human (name : "Tasneem" , age : 29) +var Humantwo = Human (name : "Ibrahim" , age : 34) /*: Make the `Human` class adopt the `CustomStringConvertible`. Print both of your previously initialized `Human` objects. */ - +print (Humanone) +print (Humantwo) /*: Make the `Human` class adopt the `Equatable` protocol. Two instances of `Human` should be considered equal if their names and ages are identical to one another. Print the result of a boolean expression evaluating whether or not your two previously initialized `Human` objects are equal to eachother (using `==`). Then print the result of a boolean expression evaluating whether or not your two previously initialized `Human` objects are not equal to eachother (using `!=`). */ +let check : Bool = Humanone == Humantwo +let checkno : Bool = Humanone != Humantwo +print (check) +print (checkno) /*: Make the `Human` class adopt the `Comparable` protocol. Sorting should be based on age. Create another three instances of a `Human`, then create an array called `people` of type `[Human]` with all of the `Human` objects that you have initialized. Create a new array called `sortedPeople` of type `[Human]` that is the `people` array sorted by age. */ - +var Humanthree = Human (name : "Kenzy" , age : 2) +var Humanfour = Human (name : "Mohamed" , age : 24) +var Humanfive = Human (name : "Ahmed" , age : 39) + var people : [Human] = [ Humanone , Humantwo , Humanthree , Humanfour , Humanfive ] + let sortedPeople = people.sorted(by: < ) +for person in sortedPeople +{ + print (person.name) +} /*: Make the `Human` class adopt the `Codable` protocol. Create a `JSONEncoder` and use it to encode as data one of the `Human` objects you have initialized. Then use that `Data` object to initialize a `String` representing the data that is stored, and print it to the console. diff --git a/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/2. App Exercise - Printable Workouts.xcplaygroundpage/Contents.swift b/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/2. App Exercise - Printable Workouts.xcplaygroundpage/Contents.txt similarity index 67% rename from Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/2. App Exercise - Printable Workouts.xcplaygroundpage/Contents.swift rename to Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/2. App Exercise - Printable Workouts.xcplaygroundpage/Contents.txt index db03100..5effb23 100644 --- a/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/2. App Exercise - Printable Workouts.xcplaygroundpage/Contents.swift +++ b/Student Resources/4 - Tables and Persistence/1 - Protocols/lab/Lab - Protocols.playground/Pages/2. App Exercise - Printable Workouts.xcplaygroundpage/Contents.txt @@ -1,3 +1,4 @@ + /*: ## App Exercise - Printable Workouts @@ -5,7 +6,7 @@ The `Workout` objects you have created so far in app exercises don't show a whole lot of useful information when printed to the console. They also aren't very easy to compare or sort. Throughout these exercises, you'll make the `Workout` class below adopt certain protocols that will solve these issues. */ -class Workout { +class Workout : CustomStringConvertible , Equatable , Comparable { var distance: Double var time: Double var identifier: Int @@ -15,23 +16,50 @@ class Workout { self.time = time self.identifier = identifier } + var description : String + { + return "Workout (distance: \(distance) , time: \(time) , identifier: \(identifier))" + } + static func == (lhs : Workout , rhs : Workout)-> Bool + { + return lhs.identifier == rhs.identifier + } + static func < (lhs : Workout , rhs : Workout)-> Bool + { + return lhs.identifier < rhs.identifier + } + } /*: Make the `Workout` class above conform to the `CustomStringConvertible` protocol so that printing an instance of `Workout` will provide useful information in the console. Create an instance of `Workout`, give it an identifier of 1, and print it to the console. */ - +var WorkoutOne = Workout ( distance: 100.2 , time: 2.4 , identifier: 1) +print (WorkoutOne) /*: Make the `Workout` class above conform to the `Equatable` protocol. Two `Workout` objects should be considered equal if they have the same identifier. Create another instance of `Workout`, giving it an identifier of 2, and print a boolean expression that evaluates to whether or not it is equal to the first `Workout` instance you created. */ - + var WorkoutTwo = Workout ( distance: 200.2 , time: 4.4 , identifier: 2) +print (WorkoutTwo) +//print (WorkoutOne == WorkoutTwo) +print (WorkoutOne != WorkoutTwo) /*: Make the `Workout` class above conform to the `Comparable` protocol so that you can easily sort multiple instances of `Workout`. `Workout` objects should be sorted based on their identifier. Create three more `Workout` objects, giving them identifiers of 3, 4, and 5, respectively. Then create an array called `workouts` of type `[Workout]` and assign it an array literal with all five `Workout` objects you have created. Place these objects in the array out of order. Then create another array called `sortedWorkouts` of type `[Workout]` that is the `workouts` array sorted by identifier. */ + var WorkoutThree = Workout ( distance: 200.2 , time: 4.4 , identifier: 3) + var WorkoutFour = Workout ( distance: 200.2 , time: 4.4 , identifier: 4) + var WorkoutFive = Workout ( distance: 200.2 , time: 4.4 , identifier: 5) + var workouts : [Workout] = [ WorkoutOne , WorkoutTwo, WorkoutThree , WorkoutFour , WorkoutFive ] +let sortedWorkOuts = workouts.sorted (by: <) +for workout in sortedWorkOuts +{ + print (workout.identifier) +} + /*: