Write a Python program to find the area of circle and rectangle using Method Overloading

This program finds the area of square, rectangle and circle using method overloading. In this program we have three methods with same name area(), which means we are overloading area() method. By having three different implementation of area method, we are calculating the area of square, rectangle and circle.

To understand this program you should have the knowledge of following Core Java topic:
Method Overloading in Java

Example: Program to find area of Square, Rectangle and Circle using Method Overloading

class JavaExample { void calculateArea(float x) { System.out.println("Area of the square: "+x*x+" sq units"); } void calculateArea(float x, float y) { System.out.println("Area of the rectangle: "+x*y+" sq units"); } void calculateArea(double r) { double area = 3.14*r*r; System.out.println("Area of the circle: "+area+" sq units"); } public static void main(String args[]){ JavaExample obj = new JavaExample(); /* This statement will call the first area() method * because we are passing only one argument with * the "f" suffix. f is used to denote the float numbers * */ obj.calculateArea(6.1f); /* This will call the second method because we are passing * two arguments and only second method has two arguments */ obj.calculateArea(10,22); /* This will call the second method because we have not suffixed * the value with "f" when we do not suffix a float value with f * then it is considered as type double. */ obj.calculateArea(6.1); } }

Output:

Area of the square: 37.21 sq units Area of the rectangle: 220.0 sq units Area of the circle: 116.8394 sq units

In this python tutorial, you will learn about the Python program to find an area of a rectangle and, also we will check:

  • How to find an area of a rectangle in Python
  • Python program to calculate the area of a rectangle using function
  • How to find area and perimeter of a rectangle in Python
  • Python program to find the area and perimeter of a rectangle using function
  • How to find the area of a rectangle using classes in Python

Let see python program to find an area of a rectangle.

  • Firstly, we will take input from the user for length and breadth using the input() function.
  • Now, we will calculate the area of a rectangle by using the formula Area = l * b.
  • At last, print the area of a rectangle to see the output.

Example:

l = float(input('Enter the length of a Rectangle: ')) b = float(input('Enter the breadth of a Rectangle: ')) Area = l * b print("Area of a Rectangle is: %.2f" %Area)

You can refer to the below screenshot to see the output for python program to find an area of a rectangle.

Write a Python program to find the area of circle and rectangle using Method Overloading
Python program to find an area of a rectangle

The code we can use to find an area of a rectangle in Python.

Also, check, How to calculate simple interest in Python and How to find area of a triangle in Python?

Python program to calculate area of a rectangle using function

Here, we will see python program to calculate the area of a rectangle using function.

  • Firstly, we will define a function with two arguments using def keywords.
  • Now, we will calculate the area of a rectangle inside the function by using the formula Area = width * height.
  • The user will enter the width and height of a rectangle and we will pass those values to the function arguments.
  • At last, print the area of a rectangle to see the output.

Example:

def AreaofRectangle(width, height): Area = width * height print("Area of a Rectangle is: %.2f" %Area) AreaofRectangle(8, 6)

You can refer to the below screenshot to see the output for the python program to calculate the area of a rectangle using function.

Write a Python program to find the area of circle and rectangle using Method Overloading
Python program to calculate the area of a rectangle using function

The above code, we can use to calculate area of a rectangle using function in Python.

Check out, Python program to print pattern.

Python program to find area and perimeter of a rectangle

Now, we will see python program to find area and perimeter of a rectangle.

  • Firstly, we will take input from the user for length and breadth using the input() function.
  • Now, we will calculate the area of a rectangle by using the formula Area = w * h.
  • Next, we are calculating the perimeter of a rectangle Perimeter = 2 * (w + h)
  • At last, print the area and perimeter of a rectangle to see the output.

Example:

w = float(input('Enter the Width of a Rectangle: ')) h = float(input('Enter the Height of a Rectangle: ')) Area = w * h Perimeter = 2 * (w + h) print("Area of a Rectangle is: %.2f" %Area) print("Perimeter of Rectangle is: %.2f" %Perimeter)

You can refer to the below screenshot to see the output for the python program to find area and perimeter of a rectangle.

Write a Python program to find the area of circle and rectangle using Method Overloading
Python program to find area and perimeter of a rectangle

The above code, we can use to find area and perimeter of a rectangle in Python.

You may also like, Python program to find the area of square.

Python program to find the area and perimeter of a rectangle using function

Let’s see python program to find the area and perimeter of a rectangle using function.

  • Firstly, we will define a function with two arguments using def keywords.
  • Now, we will calculate the area of a rectangle inside the function by using the formula Area = width * height.
  • Next, we are calculating the perimeter of a rectangle inside the function using the formula Perimeter = 2 * (width + height).
  • Allow the user to enter the width and height of a rectangle by using the input function and we will pass those values to the function arguments.
  • At last, print the area and perimeter of a rectangle to see the output.

Example:

def Areaofrectangle(width, height): Area = width * height Perimeter = 2 * (width + height) print("Area of a rectangle is: %.2f" %Area) print("Perimeter of rectangle is: %.2f" %Perimeter) width = float(input('Enter the width of a rectangle: ')) height = float(input('Enter the height of a rectangle: ')) Areaofrectangle(width, height)

You can refer to the below screenshot to see the output for python program to find the area and perimeter of a rectangle using function.

Write a Python program to find the area of circle and rectangle using Method Overloading
Python program to find the area and perimeter of a rectangle using function

The above Python code we can use to find the area and perimeter of a rectangle using function.

Read, How to print factorial of a number in Python.

Python program to find area of a rectangle using classes

Let see python program to find the area of a rectangle using classes.

  • Firstly, we will create a class called rectangle and the __init__() method is used to initialize values of that class.
  • A method called area(self) return self.length*self.breadth which is the area of the class.
  • The user will enter the value of length and breadth by using the input function.
  • An object for the class is created as obj=rectangle(l,b)
  • By using the object, the method area() is called with the perimeters as length and breadth taken from the user.
  • At last, print the area of a rectangle to see the output.

Example:

class rectangle(): def __init__(self,length,breadth): self.length=length self.breadth=breadth def area(self): return self.length*self.breadth l=int(input("Enter length of rectangle: ")) b=int(input("Enter breadth of rectangle: ")) obj=rectangle(l,b) print("Area of rectangle:",obj.area())

You can refer to the below screenshot to see the output for python program to find the area of a rectangle using classes.

Write a Python program to find the area of circle and rectangle using Method Overloading
Python program to find the area of a rectangle using classes

This is the Python program to find area of a rectangle using classes.

You may like the following Python tutorials:

In this Python tutorial, we have learned about the Python program to find an area of a rectangle. Also, we covered these below topics:

  • Python program to find an area of a rectangle
  • Python program to calculate the area of a rectangle using function
  • Python program to find area and perimeter of a rectangle
  • Python program to find the area and perimeter of a rectangle using function
  • Python program to find the area of a rectangle using classes

Write a Python program to find the area of circle and rectangle using Method Overloading

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.