Python
import unittest import inspect ### OOP #2 Inheritance ### The file name should be renamed to your Student ID (00XXXXXXX_hw5.py) ### Each question in section 1 and 2 is worth 25 points. ### Your programs should pass the provided test cases. ### When you want to test your program, uncomment the appropriate test case from bottom, run the program. ### Depending on the test results, modify the code or move to the next question. ### The questions are separate, and the earlier results do not affect your ability to complete other question. ### Do not modify the structure of the code. ### Start of HW4 # Class class Shape: def __init__(self): pass def area(self): return 0 # Q1A # Goal: # Create a child class or a subclass of Shape, called Circle, with an init function which takes radius and pie_value as arguments. # Create a method called get_circumference that calculates and returns the circumference of a circle. # Formula to calculate the circumference of a circle: 2 x pie_value x radius # Input: # None # Output: # Return the class Circle. def q1a(): class Circle(Shape): pass # Your code here! return Circle # Q1B # Goal: # Create a child class or a subclass of Shape, called Square, with an init function which takes a length as an argument. # Override the area method to calculate and return the area of square. # Formula to calculate the area of a square: length x length. # Input: # None # Output: # Return the class Square def q1b(): class Square(Shape): pass # Your code here :) return Square # Q1C # Goal: # Create a child class or a subclass of Shape, called Polygon, with an init function that takes a side_length, number_of_sides, and apothem. # Apothem is the length of a line segment that connects the center of the polygon and the midpoint of a side. # Create a method, called perimeter, to calculate and return the perimeter of the polygon given the number of sides. # Formula to calculate perimeter is side_length x number_of_sides. # Override the area method to reflect the formula for calculating the area of a polygon. # Formula to calculate the area of a regular polygon: (1/2) x apothem x perimeter # Return the class Polygon def q1c(): class Polygon(Shape): pass # Your code here return Polygon # Part 2 Class class Two_dimension(): constructor_called = False def __init__(self, width, length): self.width = width self.length = length self.constructor_called = True # Q2A # Goal: # Create a child class or a subclass of Two_dimension, called Three_dimension, with an init function that takes a width, length, and height. # Create a method, called volume, to calculate and return the volume. # Formula to calculate volume is width x length x height. # Return the class Three_dimension def q2a(): class Three_dimension(Two_dimension): pass # Your code here return Three_dimension # Test # Tells you which test case the program has failed in terminal. # These are examples of test cases provided for you to test your program during the homework. # All test cases should pass. # Uncomment the class when you want to test your program! # class TestQuestion1A(unittest.TestCase): # def test_one(self): # class_received = q1a() # assert '__init__' in dir(class_received), 'Class has no constructor' # assert 'self' in inspect.getfullargspec(class_received.__init__).args, 'self should be an argument for the constructor' # assert 'radius' in inspect.getfullargspec(class_received.__init__).args, 'radius should be an argument for the constructor' # assert 'pie_value' in inspect.getfullargspec(class_received.__init__).args, 'pie_value should be an argument for the constructor' # assert len(inspect.getfullargspec(class_received.__init__).args) == 3, 'the constructor should have exactly three arguments!' # circle_obj = class_received(3, 3.14) # assert 'radius' in dir(circle_obj), 'radius is not a property of the object' # assert 'pie_value' in dir(circle_obj), 'pie_value is not a property of the object' # self.assertEqual(circle_obj.get_circumference(), 18.84) # def test_two(self): # class_received = q1a() # assert '__init__' in dir(class_received), 'Class has no constructor' # assert 'self' in inspect.getfullargspec(class_received.__init__).args, 'self should be an argument for the constructor' # assert 'radius' in inspect.getfullargspec(class_received.__init__).args, 'radius should be an argument for the constructor' # assert 'pie_value' in inspect.getfullargspec(class_received.__init__).args, 'pie_value should be an argument for the constructor' # assert len(inspect.getfullargspec(class_received.__init__).args) == 3, 'the constructor should have exactly three arguments!' # circle_obj = class_received(2, 3.14) # assert 'radius' in dir(circle_obj), 'radius is not a property of the object' # assert 'pie_value' in dir(circle_obj), 'pie_value is not a property of the object' # self.assertEqual(circle_obj.get_circumference(), 12.56) # class TestQuestion1B(unittest.TestCase): # def test_one(self): # class_received = q1b() # assert '__init__' in dir(class_received), 'Class has no constructor' # assert 'self' in inspect.getfullargspec(class_received.__init__).args, 'self should be an argument for the constructor' # assert 'length' in inspect.getfullargspec(class_received.__init__).args, 'length should be an argument for the constructor' # assert len(inspect.getfullargspec(class_received.__init__).args) == 2, 'the constructor should have exactly two arguments!' # square_obj = class_received(5) # assert 'length' in dir(square_obj), 'length is not a property of the object' # self.assertEqual(square_obj.area(), 25) # def test_two(self): # class_received = q1b() # assert '__init__' in dir(class_received), 'Class has no constructor' # assert 'self' in inspect.getfullargspec(class_received.__init__).args, 'self should be an argument for the constructor' # assert 'length' in inspect.getfullargspec(class_received.__init__).args, 'length should be an argument for the constructor' # assert len(inspect.getfullargspec(class_received.__init__).args) == 2, 'the constructor should have exactly two arguments!' # square_obj = class_received(3) # assert 'length' in dir(square_obj), 'length is not a property of the object' # self.assertEqual(square_obj.area(), 9) # class TestQuestion1C(unittest.TestCase): # def test_one(self): # class_received = q1c() # assert '__init__' in dir(class_received), 'Class has no constructor' # assert 'self' in inspect.getfullargspec(class_received.__init__).args, 'self should be an argument for the constructor' # assert 'side_length' in inspect.getfullargspec(class_received.__init__).args, 'side_length should be an argument for the constructor' # assert 'number_of_sides' in inspect.getfullargspec(class_received.__init__).args, 'number_of_sides should be an argument for the constructor' # assert 'apothem' in inspect.getfullargspec(class_received.__init__).args, 'apothem should be an argument for the constructor' # poly_obj = class_received(2, 5, 8) # assert 'side_length' in dir(poly_obj), 'side_length is not a property of the object' # assert 'number_of_sides' in dir(poly_obj), 'number_of_sides is not a property of the object' # assert 'apothem' in dir(poly_obj), 'apothem is not a property of the object' # self.assertEqual(poly_obj.perimeter(), 10) # self.assertEqual(poly_obj.area(), 40) # def test_two(self): # class_received = q1c() # assert '__init__' in dir(class_received), 'Class has no constructor' # assert 'self' in inspect.getfullargspec(class_received.__init__).args, 'self should be an argument for the constructor' # assert 'side_length' in inspect.getfullargspec(class_received.__init__).args, 'side_length should be an argument for the constructor' # assert 'number_of_sides' in inspect.getfullargspec(class_received.__init__).args, 'number_of_sides should be an argument for the constructor' # assert 'apothem' in inspect.getfullargspec(class_received.__init__).args, 'apothem should be an argument for the constructor' # poly_obj = class_received(1, 6, 8) # assert 'side_length' in dir(poly_obj), 'side_length is not a property of the object' # assert 'number_of_sides' in dir(poly_obj), 'number_of_sides is not a property of the object' # assert 'apothem' in dir(poly_obj), 'apothem is not a property of the object' # self.assertEqual(poly_obj.perimeter(), 6) # self.assertEqual(poly_obj.area(), 24) # def test_three(self): # class_received = q1c() #