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

  1. 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.
  2. Class Rectangle:

    • Implements the AreaPerimeter interface.
    • Provides the implementation for:
      • calculateArea(float x, float y): Computes the area of a rectangle as x * y.
      • calculatePerimeter(float a, float b): Computes the perimeter of a rectangle as 2a + 2b.
  3. Class Triangle:

    • Also implements the AreaPerimeter interface.
    • 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 as 2 * π * a, which seems like a formula for the circumference of a circle. A proper triangle perimeter implementation would involve adding all three sides.
  4. 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, a and b.
      • Creates an instance of Rectangle and uses it to calculate and print the area and perimeter of a rectangle.
      • Creates an instance of Triangle and uses it to calculate and print the area and perimeter of a triangle.
  5. User Input:

    • The program reads two float values (a and b) as inputs using the Scanner class.

Key Concepts:

  1. Interface Implementation:

    • The AreaPerimeter interface acts as a contract that both Rectangle and Triangle must adhere to.
    • Interfaces ensure consistent method signatures across implementing classes.
  2. Polymorphism:

    • Both Rectangle and Triangle implement the same interface, but each provides its unique implementation of the methods (calculateArea and calculatePerimeter).
  3. Encapsulation and Code Reusability:

    • Common operations (area and perimeter calculations) are encapsulated within their respective classes, making the code modular and reusable.
  4. 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