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. Show
To understand this program you should have the knowledge of following Core Java topic: Example: Program to find area of Square, Rectangle and Circle using Method Overloadingclass 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:
Let see python program to find an area of a rectangle.
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. 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 functionHere, we will see python program to calculate the area of a rectangle using function.
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. Python program to calculate the area of a rectangle using functionThe 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 rectangleNow, we will see python program to find area and perimeter of a rectangle.
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. Python program to find area and perimeter of a rectangleThe 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 functionLet’s see python program to find the area and perimeter of a rectangle using function.
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. Python program to find the area and perimeter of a rectangle using functionThe 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 classesLet see python program to find the area of a rectangle using classes.
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. Python program to find the area of a rectangle using classesThis 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 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. |