Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialtytyty
4,974 Pointsfinal exam calculate area of square
// final exam OOP
import UIKit
class Shape {
var sides: Double
var name: String
init(sides: Double, name: String) {γ//designated init for Shape
self.sides = sides
self.name = name
}
}
class Square: Shape {
var sideLength: Double = 0.0
var area: Double {
get {
return sideLength * sideLength
}
set {
sideLength = sqrt(newValue)
}
}
init(sides: Double, name: String, sideLength: Double) {
self.sideLength = sideLength
super.init(sides: sides, name: name)
}
convenience init (sideLength: Double) {
self.init(sideLength: sideLength)
}
}
let calc = Square(sides: 20, name: "Square", sideLength: 20) //
calc.sideLength
calc.area
calc.name
calc.sides
My convenience init was a bit different and I used vars but no errors and all outputted values were correct. How did it succeed when it's not exactly correct?
Assignment:
1.)Create a base class called Shape with 2 properties: sides and name 2.)Create a subclass called Square with 2 properties called: sideLength and area. 3.)The area property will be a computed property with get/set 4.)Add a designated initializer to Square which accept: sides, name and sideLength 5.)Add a convenience initializer to Square that will accept only the sideLength and provide default values "4" for sides and βSquareβ for name. 6.)Create an instance of Square using the convenience initializer
1 Answer
Dan Chambers
6,057 PointsIt checks out cause its valid, but you're not getting any benefit from your convenience initialiser in that you had to pass the parameters 'name' and 'sides' to create your instance. The purpose of your convenience initialiser is to allow the creation of an instance only by supplying the 'sideLength' parameter.
To see what I mean, remove the parameters for 'sides' and 'name' when you create your 'calc' instance and Xcode will complain. However, if you define default values in your convenience initialiser then it wont.
I know it may sound obvious, but as we are creating squares, its fair to assume that every instance can have the name "Square" and will have 4 sides. So the convenience initialiser means we don't have to declare these every time we create a 'Square'.
Hope that helps.
tytyty
4,974 Pointstytyty
4,974 PointsSo given these instructions where would I pass "4" and "square"?
(5.)Add a convenience initializer to Square that will accept only the sideLength and provide default values "4" for sides and βSquareβ for name. (6.)Create an instance of Square using the convenience initializer.
Dan Chambers
6,057 PointsDan Chambers
6,057 PointsIt should look something like this:
and then when you create your instance you would only now need to do this:
let myFirstSquare = Square(sideLength: 12.5)
Its the job of the convenience initialiser to set these for you, if you don't pass anything for them.