HYPERLINK
HYPERLINK
Python Programs
About Us
Privacy Policy
Disclaimer
Contact Us
Python Program to Find the
Area of a Rectangle Using
Classes
Object-Oriented Programming(OOPS):
Given length and breadth, the task is to calculate the area of the
rectangle with the given length and breadth using classes.
Examples:
Example1:
Input:
Output:
Example2:
Input:
Approach:
# creating a class
class rectangle():
self.rectbreadth = rectbreadth
self.rectlength = rectlength
# Creating a method called areaofRect that returns the area with
the given length and breadth of the rectangle
def areaofRect(self):
return self.rectbreadth*self.rectlength
# Give the length and breadth as static input and store it in two
variables.
rectlength = 31
rectbreadth = 19
# Call the method areaofRect() on the object with the length and
breadth as the parameters taken from the user as static input.
Output:
Approach:
class rectangle():
self.rectbreadth = rectbreadth
self.rectlength = rectlength
def areaofRect(self):
return self.rectbreadth*self.rectlength
# Scan the given length and breadth as user input using a map,
int,
# Call the method areaofRect() on the object with the length and
breadth as the parameters taken from the user as static input.
rectObj=rectangle(rectlength, rectbreadth)
Output:
Enter the length and breadth of the rectangle separated by spaces
= 7 9
Explanation:
Approach:
# creating a class
class rectangle():
self.rectbreadth = rectbreadth
self.rectlength = rectlength
def areaofRect(self):
return self.rectbreadth*self.rectlength
# Call the method areaofRect() on the object with the length and
breadth as the parameters taken from the user as static input.
Output:
Explanation:
The user must provide the length and breadth values.
A rectangle class is created, and the __init__() method is
used to initialize the class’s values.
The area method returns self. length*self. breadth, which is
the class’s area.
The class’s object is created.
Using the object, the method area() is invoked with the
length and breadth as the parameters given by the user.
The area has been printed.
Related Programs:
Post navigation
← Previous Post
Skip to content
Python Programs
About Us
Privacy Policy
Disclaimer
Contact Us
Object-Oriented Programming(OOPS):
Object-oriented programming (OOP) is a form of program
structure that involves grouping related characteristics and
activities into separate objects.
Given length and breadth, the task is to calculate the area of the
rectangle with the given length and breadth using classes.
Examples:
Example1:
Input:
Output:
Example2:
Input:
Output:
Approach:
# creating a class
class rectangle():
# parameterized constructor with breadth and length as arguments
self.rectbreadth = rectbreadth
self.rectlength = rectlength
def areaofRect(self):
return self.rectbreadth*self.rectlength
# Give the length and breadth as static input and store it in two
variables.
rectlength = 31
rectbreadth = 19
# Call the method areaofRect() on the object with the length and
breadth as the parameters taken from the user as static input.
Output:
# creating a class
class rectangle():
self.rectlength = rectlength
def areaofRect(self):
return self.rectbreadth*self.rectlength
# Scan the given length and breadth as user input using a map,
int,
rectObj=rectangle(rectlength, rectbreadth)
Output:
Explanation:
Approach:
# creating a class
class rectangle():
self.rectbreadth = rectbreadth
self.rectlength = rectlength
def areaofRect(self):
return self.rectbreadth*self.rectlength
# Scan the rectangle's length as user input using the
int(input()) function and store it in a variable.
# Call the method areaofRect() on the object with the length and
breadth as the parameters taken from the user as static input.
Output:
Explanation:
Related Programs:
Python Program to Create a Class and Get All Possible
Subsets from a List
Python Program to Create a Class and Compute the Area
and the Perimeter of the Circle
Python Program to Create a Class in which One Method
Accepts a String from the User and Another Prints it
Python Program to Create a Class which Performs Basic
Calculator Operations
Post navigation
← Previous Post
Next Post →
Next Post →