0% found this document useful (0 votes)
8 views30 pages

HYPERLINK

The document provides a detailed guide on how to calculate the area of a rectangle using classes in Python, covering three methods: static input, user input separated by spaces, and user input separated by newline. It includes examples, explanations, and code implementations for each method. The document emphasizes object-oriented programming principles and the use of classes to encapsulate data and behavior.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views30 pages

HYPERLINK

The document provides a detailed guide on how to calculate the area of a rectangle using classes in Python, covering three methods: static input, user input separated by spaces, and user input separated by newline. It includes examples, explanations, and code implementations for each method. The document emphasizes object-oriented programming principles and the use of classes to encapsulate data and behavior.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Skip to content

Python Programs

 Python Built in Functions

 About Us

 Privacy Policy

 Disclaimer

 Contact Us
Python Program to Find the
Area of a Rectangle Using
Classes

/ Python / By Vikram Chiluka


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.

 Using Classes(Static Input)


 Using Classes(User Input separated by spaces)
 Using Classes(User Input separated by newline)

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).

def __init__(self, rectbreadth, rectlength):

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

# 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.

rectObj = rectangle(rectlength, rectbreadth)


print("The area of the rectangle with the given sides",

rectlength, ',', rectbreadth, '=', rectObj.areaofRect())

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).

def __init__(self, rectbreadth, rectlength):

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
# 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.

rectObj=rectangle(rectlength, rectbreadth)

print("The area of the rectangle with the given sides",

rectlength, ',', rectbreadth, '=', rectObj.areaofRect())

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).

def __init__(self, rectbreadth, rectlength):

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

# 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.

rectbreadth = int(input('Enter some random breadth of the


rectangle = '))
# 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.

rectObj = rectangle(rectlength, rectbreadth)

print("The area of the rectangle with the given sides",

rectlength, ',', rectbreadth, '=', rectObj.areaofRect())

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:

 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

Skip to content
Python Programs

 Python Built in Functions

 About Us
 Privacy Policy

 Disclaimer

 Contact Us

Python Program to Find the


Area of a Rectangle Using
Classes

/ Python / By Vikram Chiluka


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.

 Using Classes(Static Input)


 Using Classes(User Input separated by spaces)
 Using Classes(User Input separated by newline)

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).

def __init__(self, rectbreadth, rectlength):

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

# 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.

rectObj = rectangle(rectlength, rectbreadth)

print("The area of the rectangle with the given sides",

rectlength, ',', rectbreadth, '=', rectObj.areaofRect())

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).

def __init__(self, rectbreadth, rectlength):


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

# 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.

rectObj=rectangle(rectlength, rectbreadth)

print("The area of the rectangle with the given sides",

rectlength, ',', rectbreadth, '=', rectObj.areaofRect())

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).

def __init__(self, rectbreadth, rectlength):

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
# 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.

rectbreadth = int(input('Enter some random breadth of the


rectangle = '))

# 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.

rectObj = rectangle(rectlength, rectbreadth)

print("The area of the rectangle with the given sides",


rectlength, ',', rectbreadth, '=', rectObj.areaofRect())

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:
 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 →

Copyright © 2024 Python Programs

Next Post →

Copyright © 2024 Python Programs

You might also like