207k views
1 vote

\underline{ \large \red{ \frak{Question: - }}}

Define a class circle having data members pi nd radius. Initialise nd display values of data members also calculate area of circle nd display it.

\\ \\ \\


Thank You!​

User Cwishva
by
7.1k points

2 Answers

0 votes

Here's an example of how you can define a Circle class in Python, with data members for pi and radius. It also includes methods to initialize and display the values of the data members, as well as calculate and display the area of the circle:


\red{\rule{200pt}{2pt}}

class Circle:

def __init__(self, radius):

self.pi = 3.14159 # Assuming pi value for calculations

self.radius = radius

def display(self):

print("Radius:", self.radius)

print("Pi:", self.pi)

def calculate_area(self):

area = self.pi * (self.radius ** 2)

return area

def display_area(self):

area = self.calculate_area()

print("Area of the circle:", area)


\red{\rule{200pt}{2pt}}

You can create an instance of the Circle class and use its methods to initialize, display, and calculate the area of the circle:

# Create an instance of the Circle class

my_circle = Circle(5)

# Display the values of the data members

my_circle.display()

# Calculate and display the area of the circle

my_circle.display_area()


\red{\rule{200pt}{2pt}}

Output:

Radius: 5

Pi: 3.14159

Area of the circle: 78.53975


\red{\rule{200pt}{2pt}}

In this example, the radius is set to 5. The display method displays the radius and the pi value, while the calculate_area method calculates the area using the formula A = π * r^2. The display_area method calls calculate_area and displays the calculated area.

4 votes

Answer:

Implementation of a Circle class in Python:

class Circle:

def __init__(self, radius):

self.pi = 3.14159 # defining the value of pi as a data member

self.radius = radius

def display(self):

print("Radius: ", self.radius)

print("Pi: ", self.pi)

def area(self):

return self.pi * self.radius**2

circle = Circle(5) # creating an instance of the Circle class with radius 5

circle.display() # displaying the values of the data members

print("Area: ", circle.area()) # calculating and displaying the area of the circle

In this example, we define a Circle class with two data members: pi and radius. We initialize these data members in the constructor __init__() with the value of pi set to 3.14159, and the radius passed as a parameter.

We then define two methods display() and area(). The display() method prints out the values of the data members, while the area() method calculates the area of the circle using the formula pi x radius^2.

Finally, we create an instance of the Circle class with a radius of 5, and we call the display() method to display the values of the data members. We then calculate and display the area of the circle using the area() method. The output of this code will be:

Radius: 5

Pi: 3.14159

Area: 78.53975

User Aucuparia
by
7.7k points