Java: How to Get the Area of a Rectangle

The area of a rectangle is a fundamental calculation often required in programming, especially in geometry-related applications. The formula to calculate the area is simple:

In this tutorial from The Coding College, we’ll guide you through creating a Java program to calculate the area of a rectangle.

Steps to Calculate the Area

  1. Take Input: The program should accept the length and width of the rectangle.
  2. Apply the Formula: Multiply the length and width to compute the area.
  3. Output the Result: Display the calculated area to the user.

Example Program

1. Basic Program

import java.util.Scanner;

public class RectangleArea {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Prompt user for length and width
        System.out.print("Enter the length of the rectangle: ");
        double length = scanner.nextDouble();

        System.out.print("Enter the width of the rectangle: ");
        double width = scanner.nextDouble();

        // Calculate the area
        double area = length * width;

        // Display the result
        System.out.println("The area of the rectangle is: " + area);
    }
}

Output Example:

Enter the length of the rectangle: 5  
Enter the width of the rectangle: 3  
The area of the rectangle is: 15.0  

2. Using a Method

To make the program more reusable, you can define a method for the area calculation.

public class RectangleArea {
    // Method to calculate area
    public static double calculateArea(double length, double width) {
        return length * width;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the length of the rectangle: ");
        double length = scanner.nextDouble();

        System.out.print("Enter the width of the rectangle: ");
        double width = scanner.nextDouble();

        // Call the method and display the area
        double area = calculateArea(length, width);
        System.out.println("The area of the rectangle is: " + area);
    }
}

3. Object-Oriented Approach

You can use a class to encapsulate the rectangle’s properties and methods.

class Rectangle {
    private double length;
    private double width;

    // Constructor
    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    // Method to calculate area
    public double getArea() {
        return length * width;
    }
}

public class RectangleArea {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the length of the rectangle: ");
        double length = scanner.nextDouble();

        System.out.print("Enter the width of the rectangle: ");
        double width = scanner.nextDouble();

        // Create a Rectangle object and calculate the area
        Rectangle rectangle = new Rectangle(length, width);
        System.out.println("The area of the rectangle is: " + rectangle.getArea());
    }
}

Real-Life Applications

  1. Flooring and Tiling: Calculate the area to estimate material requirements.
  2. UI Design: Determine the size of elements in graphical interfaces.
  3. Mapping Applications: Compute areas of land plots or spaces.

Best Practices

  • Validation: Ensure that input values for length and width are positive.
  • Reuse Code: Encapsulate the calculation logic in a method or class.
  • Scalability: Consider extending the class to include perimeter calculations or other geometric properties.

Conclusion

By following this tutorial, you can easily calculate the area of a rectangle in Java. This foundational concept can be extended for more complex geometric calculations.

For more hands-on tutorials and coding examples, visit The Coding College and enhance your programming skills!

Leave a Comment