Question
Part 1: Housekeeping Read this lab all the way through BEFORE completing it. This lab is broken into multiple parts. Your goal is to complete
Part 1: Housekeeping
Read this lab all the way through BEFORE completing it.
This lab is broken into multiple parts. Your goal is to complete as many parts as you can by the end of the lab. If you do not complete all of them, you can still get full points; show that you have worked hard and put in effort and complete the minimum amount of problems (either part 2 or 3 and part 4. You can remove code that you do not need in the main function.) and this will be accomplishable.
This lab will be a continuation of our lectures on classes in Python. We will now delve into the OOP principle of inheritance, which allows for a subclass to inherit data members and methods of a superclass. You will need the Class geometricObject for this lab; study it before attempting to continue.
Part 2: The Circle Class (30 minutes)
Create a new Python file that contains the class called Circle. It should extend the geometricObject class. It should contain:
- An instance variable radius that represents the radius of the Circle
- A constructor that creates a Circle with specified radius, color, and filled. Use default values 1.0 for radius, white for color, and True for filled. Notice that the instance variables for color and filled are inherited from the superclass. Also remember that, in Python, every class is responsible for initializing the instance variables defined in it.
- A method named getArea() that returns the area of the Circle.
- A method named getPerimeter() that returns the perimeter of the Circle.
- An __str__ method to return a string representation of the form Circle: radius = 3 color: red and filled: True (Assume that 3, red, and True are the values for the corresponding instance variables of the Circle object on whose behalf the method was called). This __str__ method should append the string representation returned from geometricObject. Execute __str__(self) to get the part of the message that says color: red and filled: True.
Part 3: The Triangle Class (30 minutes)
Now, add to your program the class Triangle, which should also extend the geometricObject class. It should contain:
- Instance variables side1, side2, and side3 that denote the three sides of the Triangle.
- A constructor that creates a Triangle with the specified side1, side2, side3, color, and filled. Use default values 1.0 for each of the sides, white for color, and True for filled. Notice that the instance variables for color and filled are inherited from the superclass.
- A method named getArea() that returns the area of the Triangle. The formula for computing the area of a triangle is.
s = (side1 + side2 + side3) / 2 area = sqrt(s(s-side1)(s-side2)(s-side3)) |
- A method named getPerimeter() that returns the perimeter of the Triangle.
- An __str__ method to return a string representation of the form Triangle: side1 = 3, side2 = 4, side3 = 6 color: blue and filled: True. (Assume that 3, 4, 5, blue, and True are the values for the corresponding instance variables of the Triangle object on whose behalf the method was called).
Part 4: Testing Your Code (20 minutes)
This is the last problem required for full credit.
Add a test program to test your Circle and Triangle classes and some of the methods inherited from the class geometricObject. A sample program is as follows:
def main(): #Testing Circle class c = Circle(5, "blue", False) print(c) print()
print("Entering input values for a circle") r = float(input('Enter value for radius: '))
c1 = Circle(r)
print(c1) print("%.2f" % c1.getArea()) print("%.2f" % c1.getPerimeter()) print(c1.getColor()) print(c1.isFilled())
#Testing Triangle class print(" Entering input values for a triangle") s1 = float(input('Enter value for side1: ')) s2 = float(input('Enter value for side2: ')) s3 = float(input('Enter value for side3: ')) color = input('Enter color of the triangle: ') filled = input('Is the triangle filled (1/0)? ') filled = (filled == "1") t1 = Triangle(s1, s2, s3, color, filled)
print(t1) print("%.2f" % t1.getArea()) print("%.2f" % t1.getPerimeter()) print(t1.getColor()) print(t1.isFilled())
|
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