mirror of
https://git.tonybark.com/tonytins/swiftlyfox.git
synced 2026-05-12 08:53:32 -04:00
Added Square and Equilateral Triangle classes
This commit is contained in:
parent
1c40a688e4
commit
53ba21d774
9 changed files with 154 additions and 45 deletions
|
|
@ -27,13 +27,68 @@ func calcStats(scores: [Int]) -> (min: Int, max: Int, sum: Int) {
|
|||
let stats = calcStats(scores: [5, 3, 100, 3, 9])
|
||||
let sum = stats.sum // 120
|
||||
|
||||
class Shape {
|
||||
class NamedShape {
|
||||
var numberOfSides = 0
|
||||
var name: String
|
||||
|
||||
init(name: String = "Heptagon") {
|
||||
self.name = name
|
||||
}
|
||||
|
||||
func simpleDescription() -> String {
|
||||
return "A shape with \(numberOfSides) sides."
|
||||
}
|
||||
}
|
||||
|
||||
var shape = Shape()
|
||||
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
|
||||
var shapeDesc = shape.simpleDescription()
|
||||
shape.simpleDescription()
|
||||
|
||||
var square = Square(sideLength: 5.2)
|
||||
square.area()
|
||||
square.simpleDescription()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue