Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Calculating the area of a rectangle in Java can be done using the length and width of the rectangle. This calculation is useful in various mathematical and real-world applications where determining the space occupied by a rectangular shape is required.
In this article, we will learn different methods to achieve this calculation, along with examples to demonstrate their implementation.
Formula for Area of Rectangle and Perimeter of Rectangle
Before writing the Java program, let's understand the formulas used to calculate the area and perimeter of a rectangle:
Area of a Rectangle = length × width
Perimeter of a Rectangle = 2 × (length + width)
These formulas help in determining the space covered by the rectangle and its boundary length.
Algorithm
Before jumping into writing code, it’s important to understand the steps or algorithm required to calculate the area of a rectangle. An algorithm is like a recipe—it tells you what to do step by step. Let’s discuss this in detail:
1. Understand the Formula:
The area of a rectangle is calculated using the formula:
Area = Length × Width
Here, "Length" is one side of the rectangle, & "Width" is the side perpendicular to it.
2. Take Input:
To calculate the area, we need the values of length & width. These values can be provided by the user or hardcoded in the program.
3. Perform the Calculation:
Multiply the length by the width to get the area.
4. Display the Result:
Show the calculated area to the user.
For example:
import java.util.Scanner; // Import the Scanner class to take user input
public class RectangleArea {
public static void main(String[] args) {
// Step 1: Create a Scanner object to read input
Scanner scanner = new Scanner(System.in);
// Step 2: Ask the user for the length & width
System.out.print("Enter the length of the rectangle: ");
double length = scanner.nextDouble(); // Read the length
System.out.print("Enter the width of the rectangle: ");
double width = scanner.nextDouble(); // Read the width
// Step 3: Calculate the area
double area = length width;
// Step 4: Display the result
System.out.println("The area of the rectangle is: " + area);
// Close the scanner to free up resources
scanner.close();
}
}
You can also try this code with Online Java Compiler
It’s good practice to close the `Scanner` object to free up system resources.
Approach
We can follow multiple approaches to calculate the area of a rectangle in Java:
Using Static Values - Assigning fixed values to length and width.
Using User Input (Scanner Class) - Taking input from the user for length and width.
Using Methods - Creating a method that takes parameters and returns the area.
Using Object-Oriented Programming (OOP) - Using a class and objects to encapsulate the logic.
Let's look at each approach with proper Java programs.
1. Using Static Values
public class RectangleArea {
public static void main(String[] args) {
int length = 10;
int width = 5;
int area = length * width;
int perimeter = 2 * (length + width);
System.out.println("Area of the rectangle: " + area);
System.out.println("Perimeter of the rectangle: " + perimeter);
}
}
You can also try this code with Online Java Compiler
Area of the rectangle: 50
Perimeter of the rectangle: 30
2. Using User Input (Scanner Class)
import java.util.Scanner;
public class RectangleInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of the rectangle: ");
int length = sc.nextInt();
System.out.print("Enter the width of the rectangle: ");
int width = sc.nextInt();
int area = length * width;
int perimeter = 2 * (length + width);
System.out.println("Area of the rectangle: " + area);
System.out.println("Perimeter of the rectangle: " + perimeter);
sc.close();
}
}
You can also try this code with Online Java Compiler
Since all approaches involve basic arithmetic operations and a few variable assignments, the time complexity remains O(1) (constant time). The space complexity is also O(1) since only a few integer variables are used.
Frequently Asked Questions
What is the formula to calculate the area and perimeter of a rectangle?
The formula for the area is length × width, and for the perimeter, it is 2 × (length + width).
What is the difference between using static values and user input in Java?
Using static values means assigning fixed numbers in the program, while user input allows dynamic values to be entered when running the program.
Why is OOP preferred over other approaches in Java?
OOP provides better code organization, reusability, and encapsulation, making it useful for larger projects.
Conclusion
In this article, we discussed different ways to calculate the area and perimeter of a rectangle in Java. We used static values, user input, methods, and object-oriented programming to implement the logic. Additionally, we analyzed the time and space complexity of each approach. This knowledge will help you in understanding Java fundamentals, user input handling, and method usage effectively.