Advertisement
Guest User

Akash - Swift Asg6: Student subclass

a guest
Jan 22nd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.16 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2.  
  3. import UIKit
  4.  
  5. //var str = "Hello, playground"
  6.  
  7.  
  8.  
  9.  
  10. //The class
  11. class Person {
  12.     var height=0 //in inches
  13.     var age=0
  14.     var name="No Name"
  15.    
  16.     //initializer
  17.     init(height: Int, age: Int, name: String) {
  18.         self.height = height
  19.         self.age = age
  20.         self.name = name
  21.     }
  22.    
  23.     //When a person legally changes their name
  24.     func changeName(_ name: String)
  25.     {
  26.         Swift.print("\(self.name) changed his name to \(name).")
  27.         self.name=name
  28.     }
  29.    
  30.     //When a person grows x inches
  31.     func grow(_ inches: Int)
  32.     {
  33.         Swift.print("\(self.name) just grew \(inches) inches.")
  34.  
  35.         self.height+=inches
  36.     }
  37.    
  38.     //When a person's birthday occurs
  39.     func birthdayed()
  40.     {
  41.         Swift.print("Happy Birthday \(self.name)!!!")
  42.  
  43.         self.age+=1
  44.     }
  45.    
  46.     func print()
  47.     {
  48.         //Swift.print("")
  49.         Swift.print("My name is \(name) and I am \(age) years old. I am \(height) inches tall.")
  50.     }
  51.    
  52.    
  53. }
  54.  
  55. class ForeignPerson: Person
  56. {
  57.    
  58.    
  59.     var chanceForVisa=0.0
  60.     var Visa=false
  61.    
  62.     //It is a well known fact that Non Americans do not follow the Imperial system so we must make a metric system for them
  63.    
  64.     override func grow(_ centimeters: Int) {
  65.         self.height = centimeters
  66.     }
  67.    
  68.     func applyForVisa(_ chanceForVisa: Double)
  69.     {
  70.         self.chanceForVisa = chanceForVisa
  71.        
  72.        
  73.        
  74.     }
  75.    
  76.     func getVisa()
  77.     {
  78.         let times = (Int)(chanceForVisa*100.0)
  79.         //Swift.print(times)
  80.         for _ in 1...times
  81.         {
  82.             let number = arc4random_uniform(101)
  83.             if(number>99)
  84.             {
  85.                 Visa=true
  86.                 Swift.print("\(name) recieved a Visa")
  87.             }
  88.         }
  89.        
  90.        
  91.     }
  92.    
  93.  
  94.  
  95.  
  96. }
  97.  
  98.  
  99. //main part of program
  100.  
  101. var Jéff = ForeignPerson(height: 168, age: 16, name:"Jéff Blánco")
  102. Jéff.applyForVisa(0.8)
  103. Jéff.getVisa()
  104. print("")
  105. print("")
  106. print("")
  107. print("")
  108.  
  109.  
  110. var Akash = Person(height: 68, age: 16, name:"Akash Vemulapalli")
  111. var Tanay = Person(height: 63, age: 16, name:"Tanay Menezes")
  112. var Marcus = Person(height: 74, age: 17, name:"Marcus Ma")
  113. var Mr_Lindemann = Person(height: 69, age: 41, name:"Brad Lindemann")
  114.  
  115. var Obama = Person(height: 73, age: 57, name:"Barack Obama")
  116. var Dwayne = Person(height: 77, age: 46, name:"Dwayne Johnson")
  117. var Brady = Person(height: 76, age: 41, name:"Tom Brady")
  118.  
  119.  
  120.  
  121. //Manipulating methods
  122.  
  123.  
  124. Akash.print()
  125. Tanay.print()
  126. Marcus.print()
  127. Mr_Lindemann.print()
  128. Obama.print()
  129. Dwayne.print()
  130. Brady.print()
  131.  
  132.  
  133.  
  134. print("")
  135. print("")
  136. print("")
  137.  
  138. Dwayne.changeName("The Rock")
  139. Brady.birthdayed()
  140. Obama.grow(2)
  141.  
  142. Akash.changeName("Bakash Vemulapalli")
  143. Tanay.birthdayed()
  144. Marcus.grow(10) //Marcus grows very tall
  145. Mr_Lindemann.grow(3)
  146.  
  147. print("")
  148. print("")
  149. print("")
  150.  
  151. Akash.print()
  152. Tanay.print()
  153. Marcus.print()
  154. Mr_Lindemann.print()
  155. Obama.print()
  156. Dwayne.print()
  157. Brady.print()
  158.  
  159.  
  160.  
  161.  
  162.  
  163. //Equality Testing
  164.  
  165. var Trump = Person(height: 75, age: 72, name:"Donald Trump")
  166. var Secret_Agent_Trump = Trump
  167.  
  168.  
  169. Trump.print()
  170. Secret_Agent_Trump.print()
  171.  
  172. print()
  173.  
  174. if(Trump===Secret_Agent_Trump)
  175. {
  176.     print("Trump and Secret Agent Trump are actually the same person")
  177. }
  178.  
  179.  
  180. Secret_Agent_Trump.grow(1)
  181. Trump.print()
  182. Secret_Agent_Trump.print()
  183.  
  184. if(Trump===Secret_Agent_Trump)
  185. {
  186.     print("Trump and Secret Agent Trump are actually the same person even though Secret Agent Trump grew an inch")
  187. }
  188.  
  189.  
  190.  
  191. Trump.changeName("The_Donald")
  192. Trump.print()
  193. Secret_Agent_Trump.print()
  194.  
  195. if(Trump===Secret_Agent_Trump)
  196. {
  197.     print("Trump and Secret Agent Trump are actually the same person even after Trump changed his name")
  198. }
  199.  
  200.  
  201. Secret_Agent_Trump.changeName("Donald Trump")
  202.  
  203.  
  204. if(Trump===Secret_Agent_Trump)
  205. {
  206.     print("Trump and Secret Agent Trump are actually the same person even after they both changed names")
  207. }
  208.  
  209.  
  210.  
  211. print("")
  212. print("Observation: When we copy by reference in Swift, changing one variable changes the other so equality will exist.")
  213.  
  214.  
  215. print("")
  216. print("")
  217. print("")
  218.  
  219.  
  220.  
  221. var Hillary = Person(height: 67, age: 71, name:"Hillary Clinton")
  222. var Doppelganger_Clinton = Person(height: 67, age: 71, name:"Hillary Clinton")
  223.  
  224. if(Hillary===Doppelganger_Clinton)
  225. {
  226.     print("Hillary and Doppelganger Hillary are actually the same person")
  227. }
  228. else
  229. {
  230.     print("Hillary and Doppelganger Hillary are different people")
  231.  
  232. }
  233. print("")
  234. print("Observation: When we copy by value in Swift, the two objects will not equal each other, even if they have the same values.")
  235.  
  236.  
  237.  
  238.  
  239. //Dictionary work
  240.  
  241. print("")
  242. print("")
  243. print("")
  244. print("")
  245. print("")
  246. print("")
  247.  
  248.  
  249.  
  250. //How to create a dictionary of presidents up to the civil war
  251. var Presidents: [String:Person] = [
  252.     "one" : Person(height: 74, age: 67, name:"George Washington"),
  253.     "two" : Person(height: 67, age: 90, name:"John Adams"),
  254.     "three" : Person(height: 75, age: 83, name:"Thomas Jefferson"),
  255.     "four" : Person(height: 64, age: 85, name:"James Madison"),
  256.     "five" : Person(height: 72, age: 73, name:"James Monroe"),
  257.     "six" : Person(height: 68, age: 80, name:"John Quincy Adams"),
  258.     "seven" : Person(height: 73, age: 78, name:"Andrew Jackson"),
  259.     "eight" : Person(height: 66, age: 79, name:"Martin Van Buren"),
  260.     "nine" : Person(height: 68, age: 68, name:"William Henry Harrison"),
  261.     "ten" : Person(height: 72, age: 71, name:"John Tyler"),
  262.     "eleven" : Person(height: 68, age: 53, name:"James K. Polk"),
  263.     "twelve" : Person(height: 68, age: 65, name:"Zachary Taylor"),
  264.     "thirteen" : Person(height: 69, age: 74, name:"Millard Fillmore"),
  265.     "fourteen" : Person(height: 70, age: 64, name:"Franklin Pierce"),
  266.     "fifteen" : Person(height: 72, age: 77, name:"James Buchanan"),
  267.     "sixteen" : Person(height: 76, age: 56, name:"Abraham Lincoln"),
  268. ]
  269.  
  270. //How to access dictionary elements
  271.  
  272.  
  273. //Swift toString is really bad so I just used my own print method to display the values
  274.  
  275. for (key, value) in Presidents {
  276.     print("President number \(key):")
  277.     print("\(value.print())")
  278.     print("")
  279.    
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement