In: Computer Science
Use a swift language in xcode software for the following code?
20 pts Create a class called Polygon, this class will have:
a. Properties:
i. Number of sides
ii. Interior angles
b. Method
i. sides(), this will print to the console how many sides the polygon has
ii. interiorAngle(), this will calculate the interior angles and set it to the interior angles property
Create another class named Triangle that inherits from Polygon. This class will have to:
a. Properties:
i. Area
ii. Side length
b. Method i. calculateArea
class Polygon
{
var NoOfSides : Int = 0
var InteriorAngle : Int = 0
func sides(NoOfSides:Int)
{
print("Number of sides of polygon are :\(NoOfSides)")
}
func interiorAngle()
//calculate sum of interior angles of a polygon
{
InteriorAngle=(NoOfSides-2)*(-180)
print("Sum of interior angles of polygon are
:\(InteriorAngle)")
}
}
let counter = Polygon()
counter.sides(NoOfSides:4)
//input the number of sides of the polygon example:4
counter.interiorAngle()
class Triangle : Polygon
//inheritance
{
var Area : Float = 0
var SideLength1 : Float = 0
var SideLength2: Float = 0
var SideLength3 :Float = 0
func
calculateArea(SideLength1:Float,SideLength2:Float,SideLength3:Float)
//function to calculate the area of Triangle
{
var p:Float=0
var s:Float=0
p=(0.5)*(SideLength1+SideLength2+SideLength3)
s=p*(p-SideLength1)*(p-SideLength2)*(p-SideLength3)
Area=s.squareRoot()
print("Area of Triangle is :\(Area)")
}
}
let show = Triangle()
show.calculateArea(SideLength1:3,SideLength2:4,SideLength3:5)
//input the values of the sides of the triangle example:3,4,5