# Python Program to Find the Area of a Rectangle Using Classes

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Object-Oriented Programming(OOPS):

Object-oriented programming (OOP) is a form of program structure that involves grouping related characteristics and activities into separate objects.

Objects are conceptually similar to system components. Consider a program to be a sort of factory assembly line. A system component processes some material at each step of the assembly line, eventually changing raw material into a finished product.

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.

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:

given length of rectangle = 15
given breadth of rectangle = 11

Output:

The area of the rectangle with the given sides 15 , 11 = 165

Example2:

Input:

given length of rectangle = 31
given breadth of rectangle = 19

Output:

The area of the rectangle with the given sides 31 , 19 = 589

## Program to Find the Area of a Rectangle Using Classes in Python

Below is the full approach to calculate the area of the rectangle with the given length and breadth using classes in Python.

### 1)Using Classes(Static Input)

Approach:

• Give the length and breadth as static input and store it in two variables.
• Create a class and use a parameterized constructor to initialize its values (length and breadth of the rectangle).
• Create a method called areaofRect that returns the area with the given length and breadth of the rectangle.
• Create an object to represent the class.
• Call the method areaofRect() on the object with the length and breadth as the parameters taken from the user as static input.
• Print the area of the rectangle.
• The exit of the program.

Below is the implementation:

# creating a class
class rectangle():
# parameterized constructor with breadth and length as arguments
# parameterized constructor is used to initialize its values (length and breadth of the rectangle).
self.rectlength = rectlength
# Creating a method called areaofRect that returns the area with the given length and breadth of the rectangle

def areaofRect(self):

# Give the length and breadth as static input and store it in two variables.
rectlength = 31
# Creating an object to represent the class.
# Call the method areaofRect() on the object with the length and breadth as the parameters taken from the user as static input.
print("The area of the rectangle with the given sides",


Output:

The area of the rectangle with the given sides 31 , 19 = 589

### 2)Using Classes(User Input separated by spaces)

Approach:

• Scan the given length and breadth as user input using a map, int, and split() functions and store them in two variables.
• Create a class and use a parameterized constructor to initialize its values (length and breadth of the rectangle).
• Create a method called areaofRect that returns the area with the given length and breadth of the rectangle.
• Create an object to represent the class.
• Call the method areaofRect() on the object with the length and breadth as the parameters taken from the user as static input.
• Print the area of the rectangle.
• The exit of the program.

Below is the implementation:

# creating a class
class rectangle():
# parameterized constructor with breadth and length as arguments
# parameterized constructor is used to initialize its values (length and breadth of the rectangle).
self.rectlength = rectlength
# Creating a method called areaofRect that returns the area with the given length and breadth of the rectangle

def areaofRect(self):

# Scan the given length and breadth as user input using a map, int,
#and split() functions and store them in two variables.
rectlength, rectbreadth = map(int, input('Enter the length and breadth of the rectangle separated by spaces = ').split())
# Creating an object to represent the class.
# Call the method areaofRect() on the object with the length and breadth as the parameters taken from the user as static input.
print("The area of the rectangle with the given sides",


Output:

Enter the length and breadth of the rectangle separated by spaces = 7 9
The area of the rectangle with the given sides 7 , 9 = 63



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.

### 3)Using Classes(User Input separated by newline)

Approach:

• Scan the rectangle’s length as user input using the int(input()) function and store it in a  variable.
• Scan the rectangle’s breadth as user input using the int(input()) function and store it in another variable.
• Here int() is used to convert the given number to integer datatype.
• Create a class and use a parameterized constructor to initialize its values (length and breadth of the rectangle).
• Create a method called areaofRect that returns the area with the given length and breadth of the rectangle.
• Create an object to represent the class.
• Call the method areaofRect() on the object with the length and breadth as the parameters taken from the user as static input.
• Print the area of the rectangle.
• The exit of the program.

Below is the implementation:

# creating a class
class rectangle():
# parameterized constructor with breadth and length as arguments
# parameterized constructor is used to initialize its values (length and breadth of the rectangle).
self.rectlength = rectlength
# Creating a method called areaofRect that returns the area with the given length and breadth of the rectangle

def areaofRect(self):

# Scan the rectangle's length as user input using the int(input()) function and store it in a  variable.
rectlength = int(input('Enter some random length of the rectangle = '))
# Scan the rectangle's breadth as user input using the int(input()) function and store it in another variable.
# Here int() is used to convert the given number to integer datatype.
# Creating an object to represent the class.
# Call the method areaofRect() on the object with the length and breadth as the parameters taken from the user as static input.
print("The area of the rectangle with the given sides",


Output:

Enter some random length of the rectangle = 15
Enter some random breadth of the rectangle = 11
The area of the rectangle with the given sides 15 , 11 = 165

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: