Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the Circle class implementation in class... import math class Circle: create a circle class that allows you to set the radius, get the

Using the Circle class implementation in class...

import math

class Circle: """ create a circle class that allows you to set the radius, get the radius, calculate the area and compare two different circles""" def __init__(self, radius = 0): """constructs a circle object""" #under your constructor you place any attributes self.radius = radius def setRadius(self, radius): """ set the radius post: will update the radius with a new value""" self.radius = radius def getRadius(self): """ post: returns the radius to point of call""" return self.radius def area(self): """ post: returns the area of the cirle object""" return ((self.radius**2) * math.pi) def circumference(self): """ post: returns the circumference of the object""" return (2 * math.pi * self.radius) def show(self): """ post: diplays our circle stuff""" print(" ", "radius: ", self.radius) print(" area: ", self.area()) print(" circumference: ", self.circumference(), " ") # operator overloading def __add__(self, other): #other is another circle object """pre: two circle objects with a plus sign between post: a new circle object with the radius = c1+c2""" return Circle(other.radius + self.radius) def __str__(self): """pre: a circle object post: return the values associated with the object as a string""" return " Circle with radius " + str(self.radius) def __lt__(self, other): """pre: two circle objects post: returns a boolean to say if the statement (c1c2) #will return a bool to check if the circle is greater than def __gt__(self, other): return (self.radius > other.radius) #def __eq__ equal (c1==c2) def __equ__(self, other): return self.radius == other.radius class Cylinder(Circle): """this is a subclass of circle that defines a cylinder object""" def __init__(self, radius, height): super(). __init__(radius) #this super class will go into the child and get the radius #create private variables self.cyl__rad = radius self.cyl__height = height #create a variable to calculate the area of cylinder def cyl_area(self): """returns the area of a cyl""" return ((2 + math.pi * self.cyl__rad**2) + (2 *math.pi * self.cyl__rad * self.cyl__height)) def cyl_volume(self): return (math.pi * (self.cyl__rad**2) * self.cyl__height) def __str__(self): return (" Cylinder with radius" + str(self.cyl__rad)+ ", area " + str(self.cyl_area()) + ", and volume" + str(self.cyl_volume()))

define a second subclass called Cone with:

Attributes for radius and height

Methods for calculating volume, surface area and appropriate getters and setters (for height attribute).

Overload the __str__ operator to allow display of the cone data

Demonstrate the amazing new class implementation in your driver by displaying the radius (using the getRadius() function), and volume and surface area (using the __str__ overload in a print function).

import circle

def main(): c1 = circle.Circle(4) print("The radius of c1 : ", c1.getRadius()) c2 = circle.Circle() print("The radius of c2: ", c2.getRadius()) c1.setRadius(6) print("The new radius of c1 : ", c1.getRadius()) c3 = circle.Circle(12) c3.show() c4 = c1 + c2 #creates a new circle object with the sum of two objects radius print(c4) print(c4 > c2) print(c1 < c2)

print (c3 == c2) cyl = circle.Cylinder(4,2) print(cyl.getRadius()) main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions