Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //: Playground - noun: a place where people can play
- import UIKit
- //var str = "Hello, playground"
- //The class
- class Person {
- var height=0 //in inches
- var age=0
- var name="No Name"
- //initializer
- init(height: Int, age: Int, name: String) {
- self.height = height
- self.age = age
- self.name = name
- }
- //When a person legally changes their name
- func changeName(_ name: String)
- {
- Swift.print("\(self.name) changed his name to \(name).")
- self.name=name
- }
- //When a person grows x inches
- func grow(_ inches: Int)
- {
- Swift.print("\(self.name) just grew \(inches) inches.")
- self.height+=inches
- }
- //When a person's birthday occurs
- func birthdayed()
- {
- Swift.print("Happy Birthday \(self.name)!!!")
- self.age+=1
- }
- func print()
- {
- //Swift.print("")
- Swift.print("My name is \(name) and I am \(age) years old. I am \(height) inches tall.")
- }
- }
- class ForeignPerson: Person
- {
- var chanceForVisa=0.0
- var Visa=false
- //It is a well known fact that Non Americans do not follow the Imperial system so we must make a metric system for them
- override func grow(_ centimeters: Int) {
- self.height = centimeters
- }
- func applyForVisa(_ chanceForVisa: Double)
- {
- self.chanceForVisa = chanceForVisa
- }
- func getVisa()
- {
- let times = (Int)(chanceForVisa*100.0)
- //Swift.print(times)
- for _ in 1...times
- {
- let number = arc4random_uniform(101)
- if(number>99)
- {
- Visa=true
- Swift.print("\(name) recieved a Visa")
- }
- }
- }
- }
- //main part of program
- var Jéff = ForeignPerson(height: 168, age: 16, name:"Jéff Blánco")
- Jéff.applyForVisa(0.8)
- Jéff.getVisa()
- print("")
- print("")
- print("")
- print("")
- var Akash = Person(height: 68, age: 16, name:"Akash Vemulapalli")
- var Tanay = Person(height: 63, age: 16, name:"Tanay Menezes")
- var Marcus = Person(height: 74, age: 17, name:"Marcus Ma")
- var Mr_Lindemann = Person(height: 69, age: 41, name:"Brad Lindemann")
- var Obama = Person(height: 73, age: 57, name:"Barack Obama")
- var Dwayne = Person(height: 77, age: 46, name:"Dwayne Johnson")
- var Brady = Person(height: 76, age: 41, name:"Tom Brady")
- //Manipulating methods
- Akash.print()
- Tanay.print()
- Marcus.print()
- Mr_Lindemann.print()
- Obama.print()
- Dwayne.print()
- Brady.print()
- print("")
- print("")
- print("")
- Dwayne.changeName("The Rock")
- Brady.birthdayed()
- Obama.grow(2)
- Akash.changeName("Bakash Vemulapalli")
- Tanay.birthdayed()
- Marcus.grow(10) //Marcus grows very tall
- Mr_Lindemann.grow(3)
- print("")
- print("")
- print("")
- Akash.print()
- Tanay.print()
- Marcus.print()
- Mr_Lindemann.print()
- Obama.print()
- Dwayne.print()
- Brady.print()
- //Equality Testing
- var Trump = Person(height: 75, age: 72, name:"Donald Trump")
- var Secret_Agent_Trump = Trump
- Trump.print()
- Secret_Agent_Trump.print()
- print()
- if(Trump===Secret_Agent_Trump)
- {
- print("Trump and Secret Agent Trump are actually the same person")
- }
- Secret_Agent_Trump.grow(1)
- Trump.print()
- Secret_Agent_Trump.print()
- if(Trump===Secret_Agent_Trump)
- {
- print("Trump and Secret Agent Trump are actually the same person even though Secret Agent Trump grew an inch")
- }
- Trump.changeName("The_Donald")
- Trump.print()
- Secret_Agent_Trump.print()
- if(Trump===Secret_Agent_Trump)
- {
- print("Trump and Secret Agent Trump are actually the same person even after Trump changed his name")
- }
- Secret_Agent_Trump.changeName("Donald Trump")
- if(Trump===Secret_Agent_Trump)
- {
- print("Trump and Secret Agent Trump are actually the same person even after they both changed names")
- }
- print("")
- print("Observation: When we copy by reference in Swift, changing one variable changes the other so equality will exist.")
- print("")
- print("")
- print("")
- var Hillary = Person(height: 67, age: 71, name:"Hillary Clinton")
- var Doppelganger_Clinton = Person(height: 67, age: 71, name:"Hillary Clinton")
- if(Hillary===Doppelganger_Clinton)
- {
- print("Hillary and Doppelganger Hillary are actually the same person")
- }
- else
- {
- print("Hillary and Doppelganger Hillary are different people")
- }
- print("")
- print("Observation: When we copy by value in Swift, the two objects will not equal each other, even if they have the same values.")
- //Dictionary work
- print("")
- print("")
- print("")
- print("")
- print("")
- print("")
- //How to create a dictionary of presidents up to the civil war
- var Presidents: [String:Person] = [
- "one" : Person(height: 74, age: 67, name:"George Washington"),
- "two" : Person(height: 67, age: 90, name:"John Adams"),
- "three" : Person(height: 75, age: 83, name:"Thomas Jefferson"),
- "four" : Person(height: 64, age: 85, name:"James Madison"),
- "five" : Person(height: 72, age: 73, name:"James Monroe"),
- "six" : Person(height: 68, age: 80, name:"John Quincy Adams"),
- "seven" : Person(height: 73, age: 78, name:"Andrew Jackson"),
- "eight" : Person(height: 66, age: 79, name:"Martin Van Buren"),
- "nine" : Person(height: 68, age: 68, name:"William Henry Harrison"),
- "ten" : Person(height: 72, age: 71, name:"John Tyler"),
- "eleven" : Person(height: 68, age: 53, name:"James K. Polk"),
- "twelve" : Person(height: 68, age: 65, name:"Zachary Taylor"),
- "thirteen" : Person(height: 69, age: 74, name:"Millard Fillmore"),
- "fourteen" : Person(height: 70, age: 64, name:"Franklin Pierce"),
- "fifteen" : Person(height: 72, age: 77, name:"James Buchanan"),
- "sixteen" : Person(height: 76, age: 56, name:"Abraham Lincoln"),
- ]
- //How to access dictionary elements
- //Swift toString is really bad so I just used my own print method to display the values
- for (key, value) in Presidents {
- print("President number \(key):")
- print("\(value.print())")
- print("")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement