Question: a Create a file hw2.py and define three classes, Polygon, Rectangle and Triangle (see next page for a visual representation). 1. Polygon will be the

 a Create a file hw2.py and define three classes, Polygon, Rectangleand Triangle (see next page for a visual representation). 1. Polygon willbe the super class of Triangle and Rectangle. 2. Create a class,

a Create a file hw2.py and define three classes, Polygon, Rectangle and Triangle (see next page for a visual representation). 1. Polygon will be the super class of Triangle and Rectangle. 2. Create a class, Polygon. Its __init__() method should take nbr_sides (number of sides) as argument. Make sure to set that instant variable appropriately in the body of the __init__()method. 3. The Polygon should have methods whoamI and howmanysides. 4. Complete the implementation of the Rectangle class which takes two arguments breadth and length to create a Rectangle object. The Rectangle class is a subclass of Polygon class. 5. Complete the implementation of the Triangle class which takes three arguments (a, b, and c) to create a Triangle object. The Triangle class is a subclass of Polygon class. 6. Add area and perimeter methods to the Rectangle class and Triangle class, Tha ----Selonnanicenogen for future code ICH W V-a. cuuenot allowed dass name Polygon init() whoami bosnysides crea( methods 1. Write the tests for the test area and test_perimeter methods. 2. What is the output after running TestPolygon.py file? The Python standard library includes the unittest module to help you write and run tests for your Python code. nbraides object variables Tests written using the unittest module can help you find bugs in your programs and prevent regressions from occurring as you change your code over time. Tests give you confidence when you begin to refactor code, as you are more likely to catch bugs due to the instant feedback when tests are executed. Rectangle Triangle init area 1) perinatar area pavimesex() b Figure 1: Rectangle and Triangle are subclasses of Polygon. They overload some of the parent class methods, leave some as is, and the Triangle class adds some object variables (in the init function) Test Driven Development To begin writing tests in Python we will use the unittest module that comes with Python. To do that; create a new file TestPolygon.py, which will contain all our test cases. Finish implementing the classes 1. In Polygon class, method whoami should return the string 'Triangle if the current object is Triangle or 'Rectangle' if the current object is Rectangle. 2. In Polygon class, method howmanysides should return the number of sides as an int. 3. Implement area and perimeter methods for the Rectangle class using the formulas given below. area = length x breadth perimeter = (2 x (length + breadth)) 2 TestPolygon.py import unittest from hw2 import Polygon, Triangle, Rectangle class TestPolygons(unittest.Testcase): def test_init(self): a1 - Polygon(); self.assertEqual(ai.whoanI(), "Triangle") self.assertEqual(a1.howanysides(), 3) a2 - Polygon(); self.assertEqual(a2.whoanO), "Rectangle") self assertEqual(a2. howmanysides(), 4) 4. Implement area and perimeter for the Triangle class using the formulas given below. perimeter = a +b+c + c area == s(s-a)(s -b)( sc) (- wheres a+b+c def test_area(self): #Test the area methods 5. In hw2.py, use an if name --'__main__': block to create sample " : Triangle and Rectangle objects and print out all their properties and associated values (e.g. "Shape: Triangle, Area: ..."). You can be creative here - the goal is for a new user to easily be able to read the print-out. def test_perimeter(self): #Test the perimeter methods if (_name__-'__main__'): ) unittest.main() 6. Now, what is the output after running 'TestPolygon.py' file? The properties that all objects must have are defined in a method called . _init__(). _init__() sets the initial state of the object by assigning the Every time a new object is created, values of the object's properties. That is, __init__() initializes each new instance of the class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!