mirror of
https://git.tonybark.com/tonytins/swiftlyfox.git
synced 2026-05-12 05:43:30 -04:00
94 lines
2 KiB
Swift
94 lines
2 KiB
Swift
import Cocoa
|
|
|
|
func greet(_ person: String, on day: String) -> String {
|
|
return "Hello \(person), today is \(day)."
|
|
}
|
|
|
|
// "Hello Tony Bark, today is Tuesday."
|
|
greet("Tony Bark", on: "Tuesday")
|
|
|
|
func calcStats(scores: [Int]) -> (min: Int, max: Int, sum: Int) {
|
|
var min = scores[0]
|
|
var max = scores[0]
|
|
var sum = 0
|
|
|
|
for score in scores {
|
|
if score > max {
|
|
max = score
|
|
} else if score < min {
|
|
min = score
|
|
}
|
|
sum += score
|
|
}
|
|
|
|
return (min, max, sum)
|
|
}
|
|
|
|
let stats = calcStats(scores: [5, 3, 100, 3, 9])
|
|
let sum = stats.sum // 120
|
|
|
|
class NamedShape {
|
|
var numberOfSides = 0
|
|
var name: String
|
|
|
|
init(name: String = "Heptagon") {
|
|
self.name = name
|
|
}
|
|
|
|
func simpleDescription() -> String {
|
|
return "A shape with \(numberOfSides) sides."
|
|
}
|
|
}
|
|
|
|
class Square: NamedShape {
|
|
var sideLength: Double
|
|
|
|
init(sideLength: Double, name: String = "Square") {
|
|
self.sideLength = sideLength
|
|
super.init(name: name)
|
|
}
|
|
|
|
func area() -> Double {
|
|
return sideLength * sideLength
|
|
}
|
|
|
|
override func simpleDescription() -> String {
|
|
return "A Square with \(numberOfSides) sides."
|
|
}
|
|
}
|
|
|
|
class EquilateralTriangle: NamedShape {
|
|
var sideLength: Double
|
|
|
|
var perimeter: Double {
|
|
get {
|
|
return 3.0 * sideLength
|
|
}
|
|
|
|
set {
|
|
sideLength = newValue / 3.0
|
|
}
|
|
}
|
|
|
|
init(sideLength: Double, name: String = "Square") {
|
|
self.sideLength = sideLength
|
|
super.init(name: name)
|
|
numberOfSides = 3
|
|
}
|
|
|
|
func area() -> Double {
|
|
return sideLength * sideLength
|
|
}
|
|
|
|
override func simpleDescription() -> String {
|
|
return "A Equilateral Triangle with \(numberOfSides) sides."
|
|
}
|
|
}
|
|
|
|
var shape = NamedShape()
|
|
shape.numberOfSides = 7
|
|
shape.simpleDescription()
|
|
|
|
var square = Square(sideLength: 5.2)
|
|
square.area()
|
|
square.simpleDescription()
|