Answered step by step
Verified Expert Solution
Question
1 Approved Answer
PYTHON 3 CODE PLEASE: How to create a class? Create a file hw2.py and define three classes, Polygon, Rectangle and Triangle (see next page for
PYTHON 3 CODE PLEASE:
How to create a class? 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. For now, just add pass in the body of these two methods. 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, and as before, for now just add pass in the body of these two methods. The pass statement is used as a placeholder for future code. When the pass statement is executed, nothing happens, but you avoid getting an error when empty code is not allowed. 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. 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. 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)) 4. Implement area and perimeter for the Triangle class using the formulas given below. perimeter = a + b + c area == s(s - a)(s -b)( sc) where s a+b+c 2 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. 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__(). Every time a new object is created, _init__0) sets the initial state of the object by assigning the 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started