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 trial

iOS Object-Oriented Swift Value vs Reference Types Final Exam - Solution

final 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

It 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.

So 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.

init(sides: Double, name: String, sideLength: Double) {
        self.sideLength = sideLength
        super.init(sides: sides, name: name)
  }

        convenience init (sideLength: Double) {
            self.init(sideLength: sideLength)

    }

It should look something like this:

 init(sides: Int, name: String, sideLength: Float) {
        self.sideLength = sideLength
        super.init(sides: sides, name: name)
    }

    convenience init(sideLength: Float) {
        self.init(sides: 4, name: "Square", sideLength: sideLength)
    }

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.