7

Create a Triangle class with properties to store the length of each side. Triangles are called scalene when all three sides are of different lengths, isosceles when two sides have the same length, or equilateral when all three sides have the same length.

Create an initializer for your class that takes three arguments and properly sets the class properties. Next, create a second initializer to use when your triangle is equilateral. Remember that all three sides will be the same length, so this method should only take one argument, but should still set all three properties. Try writing this initializer as a designated initializer first and then convert it to a convenience initializer. Finally, we want an initializer for isosceles triangles that takes two arguments. Think about how you should set up the external names for your parameters to make it clear which value will be used for two sides

This question confuses me so much. My question is: How am I supposed to create an init of isosceles and scalene in a single class? Or am I supposed to create another class? I need help. I'm new to Swift. Here's what I've got so far.

class Triangle {

    var sideA: Int
    var sideB: Int
    var sideC: Int

    init(sideA: Int, sideB: Int, sideC: Int) {

        self.sideA = sideA
        self.sideB = sideB
        self.sideC = sideC
    }

    convenience init(equilateralWithEdge edge:Int) {
        self.init(sideA: edge, sideB: edge, sideC:edge)
    }

}
beryllium
  • 29,669
  • 15
  • 106
  • 125
AllocSystems
  • 1,049
  • 2
  • 11
  • 16

2 Answers2

8

Here's how you can add another initializer to the same class that will use two parameters to create an Isosceles triangle:

class Triangle {

    var sideA: Int
    var sideB: Int
    var sideC: Int

    init(sideA: Int, sideB: Int, sideC: Int) {

        self.sideA = sideA
        self.sideB = sideB
        self.sideC = sideA
    }

    init(sideA: Int, sideB: Int) {

        self.sideA = sideA
        self.sideB = sideB
        self.sideC = sideB
    }

    convenience init(equilateralWithEdge edge:Int) {
        self.init(sideA: edge, sideB: edge, sideC:edge)
    }

}
Andy Obusek
  • 12,614
  • 4
  • 41
  • 62
  • 5
    Why not make the 2nd `init` a `convenience` that calls the 1st? – rmaddy Dec 12 '16 at 18:39
  • Thanks for the help. Please, don't think that way. The question is from a book called "sams teach yourself swift in 24 hrs". It's not a homework, I'm thinking of going into ios programming, so I want to broaden my knowledge. I've always been a c++ person. – AllocSystems Dec 12 '16 at 18:39
  • Ok, my bad for the assumption, I'll remove the snark from my answer. – Andy Obusek Dec 12 '16 at 18:44
  • @rmaddy: He could have made the 2nd init a convenience initializer too, but I'm sure he wanted to show you an example of using a second designated initializer and another example showing the use of a convenience initializer. Just remember, whichever you choose, you must make sure that all properties are initialized and none are left nil. And all convenience inits must point to a designated init. Most of the time shoot for having only one designated init, and use convenience inits for the other inits, but at the end of the day it comes down to your preference. – Adam Oct 01 '20 at 17:21
-2
class Your_ViewController : UIViewController {

    var your_property_1 : String?
    var your_property_2 : String?

    init(your_property_1: String) {
        self.your_property_1 = your_property_1
        self.your_property_2 = nil
        super.init(nibName: nil, bundle: nil)
    }

init(your_property_2: String) {
    self.your_property_2 = your_property_2
    self.your_property_1 = nil
    super.init(nibName: nil, bundle: nil)
}

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) is not supported")
    }
}
Sasan Soroush
  • 857
  • 8
  • 16