Introduction
If you’re just starting with Java, one of the first things you’ll learn is how to take input from users and perform simple calculations. In this blog, we’ll walk you through a basic Java program that calculates the area of a square, a rectangle, and a circle. This is a great way to get comfortable with using variables, taking input, and performing basic math in Java.
What Will This Program Do?
The program will:
- Ask you to enter the side of a square.
- Ask you to enter the length and breadth of a rectangle.
- Ask you to enter the radius of a circle.
- Calculate the area of each shape.
- Display the results.
The Code
Here’s the complete Java program:
import java.util.Scanner;
public class AreaCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Taking inputs for the side of the square
System.out.print("Enter the side of the square: ");
double side = scanner.nextDouble();
// Taking inputs for the length and breadth of the rectangle
System.out.print("Enter the length of the rectangle: ");
double length = scanner.nextDouble();
System.out.print("Enter the breadth of the rectangle: ");
double breadth = scanner.nextDouble();
// Taking input for the radius of the circle
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
// Calculating the area of the square
double areaSquare = side * side;
// Calculating the area of the rectangle
double areaRectangle = length * breadth;
// Calculating the area of the circle
double areaCircle = Math.PI * radius * radius;
// Displaying the results
System.out.println("Area of the square: " + areaSquare);
System.out.println("Area of the rectangle: " + areaRectangle);
System.out.println("Area of the circle: " + areaCircle);
// Closing the scanner
scanner.close();
}
}
How Does It Work?
Let’s break it down step by step:
- Setting Up the Scanner:
- The program begins by creating a
Scanner
object. This object allows the program to take input from the user.
- Taking Inputs:
- The program then asks the user to input the side of a square, the length and breadth of a rectangle, and the radius of a circle. Each input is stored in a variable using
scanner.nextDouble()
, which captures the number the user types in.
- Calculating Areas:
- Square: The area of a square is calculated by multiplying the side by itself (
side * side
). - Rectangle: The area of a rectangle is calculated by multiplying the length by the breadth (
length * breadth
). - Circle: The area of a circle is calculated using the formula
π * radius * radius
. In Java,Math.PI
provides the value of π (pi).
- Displaying Results:
- The program then prints out the area of each shape using
System.out.println()
.
- Closing the Scanner:
- Finally, the
Scanner
is closed usingscanner.close()
. This is a good habit to have as it helps free up resources.
Example Output
If you enter:
- Side of square: 4
- Length of rectangle: 5
- Breadth of rectangle: 3
- Radius of circle: 2
The output will be:
Area of the square: 16.0
Area of the rectangle: 15.0
Area of the circle: 12.566370614359172
Conclusion
This simple program is a great starting point for learning how to work with user input and perform calculations in Java. By practicing with this code, you’ll become more comfortable with basic programming concepts and be ready to tackle more complex challenges. Keep coding and exploring!