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)

Second version of Rectangle Class Program

class Rectangle():
    ''' Rectangle class Program'''

    # constructor 
    def __init__(self,breadth,length):
        self.breadth=breadth
        self.length=length

     # calculate_area( ) method   
    def calculate_area(self):
        return self.breadth*self.length

# get input from the user for length and breadth
length=int(input("Enter length of rectangle: "))
breadth=int(input("Enter breadth of rectangle: "))
# create new Rectangle object with input values
rec1=Rectangle(length,breadth)
# call calculate_area() method and print result
print("Area of rectangle:",rec1.calculate_area())
OUTPUT:
Enter length of rectangle: 5
Enter breadth of rectangle: 10
Area of rectangle: 50

Another Sample Run of the Program

Enter length of rectangle: 7
Enter breadth of rectangle: 11
Area of rectangle: 77

Third Version of Rectangle Class Program

class Rectangle:
    # constructor for Initialization
    def __init__(self):
        # The only members are length and width
        self.length = 1
        self.width = 1

    # Write Setters methods for width and length
    def set_width(self, width):
        self.width = width

    def set_length(self, length):
        self.length = length

    # Write Getters methods for the both instance variables
    def get_width(self):
        return self.width

    def get_length(self):
        return self.length
    # find area method
    def find_area(self):
        return self.length * self.width

    # show String representation of object
    def __str__(self):
        return 'length = {}, width = {}'.format(self.length, self.width)

rectangle1 = Rectangle()
# set width and length of rectangle1 object
rectangle1.set_width(7)
rectangle1.set_length(4)
# call find_area( ) method 
ar = rectangle1.find_area();
# display values of instance variables of rectangle1 object
print(rectangle1)
# show area of rectangle1 object
print("Area = ",ar)
OUTPUT:
length = 4, width = 7
Area =  28
You will also like:

Loading

Leave a Reply

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