Java Program to Calculate Area and Perimeter of Different Shapes

This page contains a Java program that calculates the area and perimeter of different shapes (circle, rectangle, triangle). The program uses basic geometric formulas to achieve this task.

Program Explanation

The Java program to calculate the area and perimeter of different shapes follows these steps:

  1. Define the main class: Create a public class named ShapeCalculator.
  2. Methods for Calculations: Define methods to calculate the area and perimeter for each shape.
  3. Main Method: The main method serves as the entry point of the program where we test the calculation methods.

Java Program Code

/**
 * Java program to calculate the area and perimeter of different shapes.
 */
public class ShapeCalculator {

    /**
     * Method to calculate the area of a circle.
     * @param radius the radius of the circle
     * @return the area of the circle
     */
    public static double areaOfCircle(double radius) {
        return Math.PI * radius * radius;
    }

    /**
     * Method to calculate the perimeter of a circle.
     * @param radius the radius of the circle
     * @return the perimeter of the circle
     */
    public static double perimeterOfCircle(double radius) {
        return 2 * Math.PI * radius;
    }

    /**
     * Method to calculate the area of a rectangle.
     * @param length the length of the rectangle
     * @param width the width of the rectangle
     * @return the area of the rectangle
     */
    public static double areaOfRectangle(double length, double width) {
        return length * width;
    }

    /**
     * Method to calculate the perimeter of a rectangle.
     * @param length the length of the rectangle
     * @param width the width of the rectangle
     * @return the perimeter of the rectangle
     */
    public static double perimeterOfRectangle(double length, double width) {
        return 2 * (length + width);
    }

    /**
     * Method to calculate the area of a triangle.
     * @param base the base of the triangle
     * @param height the height of the triangle
     * @return the area of the triangle
     */
    public static double areaOfTriangle(double base, double height) {
        return 0.5 * base * height;
    }

    /**
     * Method to calculate the perimeter of a triangle.
     * @param side1 the length of the first side of the triangle
     * @param side2 the length of the second side of the triangle
     * @param side3 the length of the third side of the triangle
     * @return the perimeter of the triangle
     */
    public static double perimeterOfTriangle(double side1, double side2, double side3) {
        return side1 + side2 + side3;
    }

    /**
     * Main method which is the entry point of the program.
     * @param args command-line arguments
     */
    public static void main(String[] args) {
        // Test calculations for a circle
        double radius = 5.0;
        System.out.println("Circle with radius " + radius);
        System.out.println("Area: " + areaOfCircle(radius));
        System.out.println("Perimeter: " + perimeterOfCircle(radius));

        // Test calculations for a rectangle
        double length = 4.0;
        double width = 6.0;
        System.out.println("\nRectangle with length " + length + " and width " + width);
        System.out.println("Area: " + areaOfRectangle(length, width));
        System.out.println("Perimeter: " + perimeterOfRectangle(length, width));

        // Test calculations for a triangle
        double base = 3.0;
        double height = 4.0;
        double side1 = 3.0;
        double side2 = 4.0;
        double side3 = 5.0;
        System.out.println("\nTriangle with base " + base + ", height " + height + " and sides " + side1 + ", " + side2 + ", " + side3);
        System.out.println("Area: " + areaOfTriangle(base, height));
        System.out.println("Perimeter: " + perimeterOfTriangle(side1, side2, side3));
    }
}

Program Details

The program contains the following methods:

  • areaOfCircle: Calculates the area of a circle given its radius.
  • perimeterOfCircle: Calculates the perimeter of a circle given its radius.
  • areaOfRectangle: Calculates the area of a rectangle given its length and width.
  • perimeterOfRectangle: Calculates the perimeter of a rectangle given its length and width.
  • areaOfTriangle: Calculates the area of a triangle given its base and height.
  • perimeterOfTriangle: Calculates the perimeter of a triangle given the lengths of its three sides.

Running the Program

To run the program:

  1. Save the code in a file named ShapeCalculator.java.
  2. Compile the program using the command: javac ShapeCalculator.java.
  3. Run the compiled program using the command: java ShapeCalculator.

 

Explanation of the Java Program:

  1. Class Definition: The class ShapeCalculator contains methods to calculate the area and perimeter of different shapes.
  2. Methods for Calculations:
    • areaOfCircle: Calculates the area of a circle using the formula π×radius2\pi \times \text{radius}^2.
    • perimeterOfCircle: Calculates the perimeter (circumference) of a circle using the formula 2×π×radius2 \times \pi \times \text{radius}.
    • areaOfRectangle: Calculates the area of a rectangle using the formula length×width\text{length} \times \text{width}.
    • perimeterOfRectangle: Calculates the perimeter of a rectangle using the formula 2×(length+width)2 \times (\text{length} + \text{width}).
    • areaOfTriangle: Calculates the area of a triangle using the formula 0.5×base×height0.5 \times \text{base} \times \text{height}.
    • perimeterOfTriangle: Calculates the perimeter of a triangle by summing the lengths of its three sides.
  3. Main Method:
    • Tests the methods by calculating the area and perimeter of a circle, rectangle, and triangle with predefined dimensions.
    • Prints the results to the console.

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)