Python Create New Class Rectangle

By | May 12, 2020

Topic: Python Create New Class Rectangle

Write a Python Program to perform the above mentioned OOP concepts. The Rectangle class will have:

  • Two instance variables: length and width
  • Two instance methods:
  • compute_area() and compute_perimeter()
  • The formula of area of rectangle is to multiply length of rectangle by the width of the rectangle.
  • Similarly, the formula of perimeter of the rectangle is adding all four sides of the rectangle. Therefore perimeter=L+L+W+W
  • That is perimeter = 2L + 2W
  • And so, perimeter = 2(L+W)
  • Moreover the Rectanggle class will have a parameterized constructor to initialize the instance variables of the object.

 

Python Create New Class Rectangle

What is the Formula to Calculate Area of Rectangle?

area = length  x  width

What is the formula to Compute Perimeter of Rectangle?

perimeter = 2 (length+width)

Python Source Code Python Create New Class Rectangle

#  Write a Python Program using OOP - class and objects
#  create a class Rectangle to provide the functionality
# of  finding area and perimeter of rectangle objects with
#  instance vaiables: length and width
#  constructor
#  computer_area() method
# computer_perimeter() method
#  www.easycodebook.com

# define a class Rectangle

class Rectangle:
              
    # define a constructor

    def __init__(self, length, width):
         self.length = length
         self.width = width
           
    # define an instance  method to calculate area of rectangle
    def compute_area(self):
        return self.length * self.width
    
    # define an instance  method to calculate perimeter of rectangle
    def compute_perimeter(self):
        return 2 * (self.length+self.width)

# end of the class

# get input from the user

l= float(input('Please Enter the Length of the Rectangle: '))
w= float(input('Please Enter the Width of the Rectangle: '))
    
# create first object

object1 = Rectangle(l,w)
# call methods of rectangle

area = object1.compute_area()
perimeter = object1.compute_perimeter()

# print area and perimeter of the rectangle object
print("Area of Rectangle object = %.2f" %area)
print("Perimeter of Rectangle object= %.2f" %perimeter)

How to Define a Rectangle Class in Python

First of all use class keyword to define a Rectangle class:

class Rectangle:

 

Define a constructor in Rectangle Class

def __init__(self, length, width):
    self.length = length
    self.width = width

Define compute_area() method

def computer_area(self):
    return self.length * self.width 

Define compute_perimeter() method

def computer_perimeter(self):
    return 2*(self.length + self.width) 

Get Input from the User at Runtime

l= float(input('Please Enter the Length of the Rectangle: '))
w= float(input('Please Enter the Width of the Rectangle: '))

Create an Object of Rectangle Class

obj = Rectangle(l,w)

Call the compute_area() method for Object of Rectangle Class

USing dot notation with object.method() syntax to call the method
compute_area() method to calculate area of the rectangle object.
And the returned value will be stored in area variable

area = obj.compute_area()

Call the compute_perimeter() method for Object of Rectangle Class

USe dot notation with object.method() syntax to call the method
compute_perimeter()
And the returned value will be stored in perimeter variable
perimeter = obj.compute_perimeter()

Show Results area and perimeter for Object of Rectangle Class

Here we will use print() function to print the details
like area and perimeter of the rectangle object on the screen.
We will use %.2f to round the value upto 2 decimal places.
print("Area of Rectangle object = %.2f" %area)
print("Perimeter of Rectangle object= %.2f" %perimeter)

You will also like:
Python Class and Objects With Code Examples

Leave a Reply

Your email address will not be published. Required fields are marked *