Java Program to find area of different shapes using inheritance
Program
import java.util.*;
interface AreaPerimeter
{
float calculateArea(float x, float y);
float calculatePerimeter(float a, float b);
}
class Rectangle implements AreaPerimeter
{
public float calculateArea(float x, float y)
{
float area = x * y;
return area;
}
public float calculatePerimeter(float a, float b)
{
float perimeter = (2 * a) + (2 * b);
return perimeter;
}
}
class Triangle implements AreaPerimeter
{
public float calculateArea(float x, float y)
{
float area = (float) 0.5 * x * y;
return area;
}
public float calculatePerimeter(float a, float b)
{
float perimeter = 2 * (float) Math.PI * a;
return perimeter;
}
}
public class FindAreaInheritance
{
public static void main(String[] args)
{
float area, perimeter;
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of a:");
float a = sc.nextFloat();
System.out.println("Enter value of b:");
float b = sc.nextFloat();
Rectangle rect = new Rectangle();
area = rect.calculateArea(a, b);
perimeter = rect.calculatePerimeter(a, b);
System.out.println("Rectangle");
System.out.println("Area:\t" + area);
System.out.println("Perimeter:\t" + perimeter);
Triangle triangle = new Triangle();
area = triangle.calculateArea(a, b);
perimeter = triangle.calculatePerimeter(a, b);
System.out.println("Triangle");
System.out.println("Area:\t" + area);
System.out.println("Perimeter:\t" + perimeter);
sc.close();
}
}
This program demonstrates the use of interfaces and inheritance to calculate the area and perimeter of different shapes (Rectangle and Triangle).
-
Interface
AreaPerimeter:- Defines two methods:
calculateArea(float x, float y)to calculate the area of a shape.calculatePerimeter(float a, float b)to calculate the perimeter of a shape.
- Both methods are abstract (by default in an interface) and must be implemented by any class that implements this interface.
- Defines two methods:
-
Class
Rectangle:- Implements the
AreaPerimeterinterface. - Provides the implementation for:
calculateArea(float x, float y): Computes the area of a rectangle asx * y.calculatePerimeter(float a, float b): Computes the perimeter of a rectangle as2a + 2b.
- Implements the
-
Class
Triangle:- Also implements the
AreaPerimeterinterface. - Provides the implementation for:
calculateArea(float x, float y): Computes the area of a triangle as(0.5 * x * y)(base * height / 2).calculatePerimeter(float a, float b): This implementation appears incorrect. It's written as2 * π * a, which seems like a formula for the circumference of a circle. A proper triangle perimeter implementation would involve adding all three sides.
- Also implements the
-
Class
FindAreaInheritance:- Contains the
main()method, which is the entry point of the program. - Steps in
main():- Prompts the user to input two float values,
aandb. - Creates an instance of
Rectangleand uses it to calculate and print the area and perimeter of a rectangle. - Creates an instance of
Triangleand uses it to calculate and print the area and perimeter of a triangle.
- Prompts the user to input two float values,
- Contains the
-
User Input:
- The program reads two float values (
aandb) as inputs using theScannerclass.
- The program reads two float values (
Key Concepts:
-
Interface Implementation:
- The
AreaPerimeterinterface acts as a contract that bothRectangleandTrianglemust adhere to. - Interfaces ensure consistent method signatures across implementing classes.
- The
-
Polymorphism:
- Both
RectangleandTriangleimplement the same interface, but each provides its unique implementation of the methods (calculateAreaandcalculatePerimeter).
- Both
-
Encapsulation and Code Reusability:
- Common operations (area and perimeter calculations) are encapsulated within their respective classes, making the code modular and reusable.
-
Input Handling:
- The program takes user input to dynamically compute the values for area and perimeter.
Output
Enter value of a:
5
Enter value of b:
8
Rectangle
Area: 40.0
Perimeter: 26.0
Triangle
Area: 20.0
Perimeter: 31.415928