import math class StringToNumber: def convert(self, string): if string == "one": return 1 elif string == "two": return 2 elif string == "three": return 3 elif string == "four": return 4 elif string == "five": return 5 elif string == "six": return 6 elif string == "seven": return 7 elif string == "eight": return 8 elif string == "nine": return 9 else: return string class Shape(): #1 Mark for using default values to overload the method def __init__(self, val1, val2 = None, val3 = None): strToNum = StringToNumber() #1 Mark for handling both numeric values and string values val1 = strToNum.convert(val1) val2 = strToNum.convert(val2) val3 = strToNum.convert(val3) #1 Mark for condition that checks against default values to determine what action to take if val2 == None: self.__shape = Circle(val1) elif val3 == None: self.__shape = Rectangle(val1, val2) else: self.__shape = Triangle(val1, val2, val3) def perimeter(self): self.__shape.perimeter() def area(self): self.__shape.area() class Triangle: def __init__(self, a, b, c): self.__a = a self.__b = b self.__c = c def perimeter(self): perimeter = (self.__a + self.__b + self.__c) print("This triangle has a perimeter of " + str(perimeter)) def area(self): perimeter = (self.__a + self.__b + self.__c) p = perimeter/2 area = math.sqrt(p*(p-self.__a)*(p-self.__b)*(p-self.__c)) print("This triangle has an area of " + str(area)) class Rectangle: def __init__(self, width, height): self.__width = width self.__height = height def perimeter(self): perimeter = 2 * (self.__width + self.__height) print("This rectangle has a perimeter of " + str(perimeter)) def area(self): area = (self.__width*self.__height) print("This rectangle has an area of " + str(area)) class Circle: def __init__(self, radius): self.__radius = radius def perimeter(self): perimeter = 2*math.pi*self.__radius print("This circle has a perimeter of " + str(perimeter)) def area(self): area = math.pi*(self.__radius**2) print("This circle has an area of " + str(area)) def main(): #1 Mark if a circle is created by each of these constructors circle1 = Shape(2) circle2 = Shape("three") #1 Mark if a rectangle is created by each of these constructors rectangle1 = Shape(5, 3) rectangle2 = Shape("seven", "two") #1 Mark if a triangle is created by each of these constructors triangle1 = Shape("four", "six", "nine") triangle2 = Shape(3, 6, 5) circle1.perimeter() #1 Mark if this line prints "This circle has a perimeter of 12.5663706..." circle1.area() #1 Mark if this line prints "This circle has an area of 12.5663706..." circle2.perimeter() #1 Mark if this line prints "This circle has a perimeter of 18.84955592..." circle2.area() #1 Mark if this line prints "This circle has an area of 28.27433388..." rectangle1.perimeter() #1 Mark if this line prints "This rectangle has a perimeter of 16" rectangle1.area() #1 Mark if this line prints "This rectangle has an area of 15" rectangle2.perimeter() #1 Mark if this line prints "This rectangle has a perimeter of 18" rectangle2.area() #1 Mark if this line prints "This rectangle has an area of 14" triangle1.perimeter() #1 Mark if this line prints "This triangle has a perimeter of 19" triangle1.area() #1 Mark if this line prints "This triangle has an area of 9.5622957..." triangle2.perimeter() #1 Mark if this line prints "This triangle has a perimeter of 14" triangle2.area() #1 Mark if this line prints "This triangle has an area of 7.48331477..." input() if __name__ == '__main__': main()