SwadhyAI Notes

All Java notes

How to Calculate Area of Rectangle in Java

The Challenge

Calculate the area of a rectangle using the formula: Area = length × width

Solution

java
public class Main {
    public static void main(String[] args) {
        double length = 10.5;
        double width = 5.2;
        
        double area = length * width;
        
        System.out.println("Length: " + length);
        System.out.println("Width: " + width);
        System.out.println("Area: " + area);
        // Output: Area: 54.6
    }
}

Using a Method

java
public static double calculateArea(double length, double width) {
    return length * width;
}

// Usage
double area = calculateArea(10.5, 5.2);
System.out.println("Area: " + area); // 54.6

Using a Class

java
class Rectangle {
    double length;
    double width;
    
    Rectangle(double l, double w) {
        length = l;
        width = w;
    }
    
    double getArea() {
        return length * width;
    }
}

// Usage
Rectangle rect = new Rectangle(10.5, 5.2);
System.out.println("Area: " + rect.getArea()); // 54.6

Did You Know?

A square is a special type of rectangle where length equals width! You can use the same formula: Area = side × side = side²

Pro Tip

Add perimeter calculation too! Perimeter = 2 × (length + width)

Real-World Use Cases

  • Construction: Calculate floor area for materials
  • Real estate: Compute property sizes
  • Graphics: Calculate canvas or screen areas
  • Farming: Measure field dimensions
›Short versionOpen if you only have a minute.
  • Area is length times width, in one unit, after rejecting a negative side.
  • int times int is an int product. 50_000 * 50_000 wraps.
  • Promote one side to long before multiplying when the product must survive.
  • A zero side is a zero area. It is not the same bug as a negative side.

Rectangle area

Multiply length by width after validating that dimensions make sense.

Working approach

  • Reject a negative length or width before multiplying.
  • Use long area = (long) length * width when the inputs are ints that may be large.
  • Keep both sides in the same unit. The formula does not convert centimeters to meters.

Walk the sample

  1. Length 8, width 5
  2. Product 8 * 5
  3. Area 40

The trap

The int product overflows even when you store it in a long, if both factors are still ints at the moment of multiplication.

java
static long area(int length, int width) {
    if (length < 0 || width < 0) {
        throw new IllegalArgumentException("negative dimension");
    }
    return (long) length * width;   // cast first; length * width alone wraps
}

What is 50_000 * 50_000 when both factors are ints?

The mathematical product is 2_500_000_000, past Integer.MAX_VALUE. The int multiply wraps to a negative value.

Assigning that wrapped int to a long keeps the negative value. The cast to long has to happen on one factor before the multiply.

Time O(1), space O(1).

Easy to get wrong

  • Multiplying two ints and only then widening the stored result.
  • Mixing units and still multiplying.