Program to Create a Class and Compute the Area and the Perimeter of the Circle

Python Program to Create a Class and Compute the Area and the Perimeter of the Circle

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Object-Oriented Programming(OOPS):

Object-Oriented Programming (OOP) is a programming paradigm based on the concepts of classes and objects. It is used to organize a software program into simple, reusable code blueprints (typically referred to as classes), which are then used to build individual instances of objects. Object-oriented programming languages include JavaScript, C++, Java, and Python, among others.

A class is a generic blueprint that can be used to generate more specific, concrete things. Classes are frequently used to represent broad groups, such as Cars or Dogs, that share property. These classes indicate what properties, such as color, an instance of this type will have, but not the value of those attributes for a specific object.

An object comprises data, such as the raw or preprocessed materials at each step of an assembly line, as well as behavior, such as the action performed by each assembly line component.

Python, like other general-purpose programming languages, has always been an object-oriented language. It enables us to create a program with an Object-Oriented approach. Classes and objects are simple to build and utilize in Python.

Given the radius of the circle, the task is to write the python program using classes that calculate the area and perimeter of the circle.

Examples:

Example1:

Input:

given radius = 13.5

Output:

The Perimeter of circle with given radius 13.5 = 84.823
The Area of circle with given radius 13.5 = 572.5553

Example2:

Input:

given radius = 20.98

Output:

The Perimeter of circle with given radius 20.98 = 131.8212
The Area of circle with given radius 20.98 = 1382.8047

Program to Create a Class and Compute the Area and the Perimeter of the Circle

Below is the full approach for writing a Python program that creates a class with two methods that calculate the area and perimeter of the circle.

1)Using Classes(Static Input)

Approach:

  • Give the radius of the circle as static input and store it in a variable.
  • Create a class and use a parameterized constructor to initialize its value (radius of the circle).
  • Create two methods inside the class.
  • printPerimeter: which calculates the perimeter of the circle and returns it.
  • printArea: which calculates the area of the circle and returns it.
  • Create an object to represent the class.
  • Call both methods with the object created above.
  • Hence the area and perimeter of the circle are calculated and printed.
  • The Exit of the Program.

Below is the implementation:

# importing math module
import math
# creating a class


class circleClass():
  # use a parameterized constructor to initialize its value (radius of the circle).
    def __init__(self, circleradius):
        self.circleradius = circleradius
    # Create two methods inside the class.
    # printArea: which calculates the area of the circle and returns it.

    def printArea(self):
        return math.pi*(self.circleradius**2)
     # printPerimeter: which calculates the perimeter of the circle and returns it.

    def printPerimeter(self):
        return 2*math.pi*self.circleradius


# Give the radius of the circle as static input and store it in a variable.
circleradius = 13.5
# Create an object to represent the class.
circleobj = circleClass(circleradius)
# Call both methods with the object created above.
# Hence the area and perimeter of the circle are calculated and printed.
print("The Perimeter of circle with given radius",
      circleradius, '=', round(circleobj.printPerimeter(), 4))
print("The Area of circle with given radius", circleradius,
      '=', round(circleobj.printArea(), 4))

Output:

The Perimeter of circle with given radius 13.5 = 84.823
The Area of circle with given radius 13.5 = 572.5553

Explanation:

  • The radius value must be given as static input from the user.
  • A circle class is created, and the __init__() method is used to initialize the class’s values (radius of the circle.
  • The area method returns math. pi*(self. radius**2), which is the class’s area.
  • Another method, perimeter, returns 2*math. pi*self. radius, which represents the class’s perimeter.
  • The class’s object is created.
  • The methods printArea() and printPerimeter() are called using the object.
  • The area and perimeter of the circle are printed.

2)Using Classes(User Input)

Approach:

  • Give the radius of the circle as user input using float(input()) and store it in a variable.
  • Create a class and use a parameterized constructor to initialize its value (radius of the circle).
  • Create two methods inside the class.
  • printPerimeter: which calculates the perimeter of the circle and returns it.
  • printArea: which calculates the area of the circle and returns it.
  • Create an object to represent the class.
  • Call both methods with the object created above.
  • Hence the area and perimeter of the circle are calculated and printed.
  • The Exit of the Program.

Below is the implementation:

# importing math module
import math
# creating a class


class circleClass():
  # use a parameterized constructor to initialize its value (radius of the circle).
    def __init__(self, circleradius):
        self.circleradius = circleradius
    # Create two methods inside the class.
    # printArea: which calculates the area of the circle and returns it.

    def printArea(self):
        return math.pi*(self.circleradius**2)
     # printPerimeter: which calculates the perimeter of the circle and returns it.

    def printPerimeter(self):
        return 2*math.pi*self.circleradius


# Give the radius of the circle as user input using float(input()) and store it in a variable.
circleradius = float(input('Enter some random radius of the circle = '))
# Create an object to represent the class.
circleobj = circleClass(circleradius)
# Call both methods with the object created above.
# Hence the area and perimeter of the circle are calculated and printed.
print("The Perimeter of circle with given radius",
      circleradius, '=', round(circleobj.printPerimeter(), 4))
print("The Area of circle with given radius", circleradius,
      '=', round(circleobj.printArea(), 4))

Output:

Enter some random radius of the circle = 20.98
The Perimeter of circle with given radius 20.98 = 131.8212
The Area of circle with given radius 20.98 = 1382.8047

Related Programs: