Calculating Perimeter and Area in Java.

Introduction

Are you just getting started with Java programming? Here’s a simple and easy program that will help you understand how to calculate the perimeter and area of a rectangle. This example is perfect for beginners and will teach you how to take user input, perform basic calculations, and display the results.

What Does This Program Do?

This Java program does two things:

  1. It calculates the perimeter of a rectangle, which tells us the length of rope needed to go around the rectangle.
  2. It calculates the area of a rectangle, which tells us the amount of carpet needed to cover the rectangle.

The Code

Here’s the full Java program:

import java.util.Scanner;
public class RopeAndCarpet {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        // Taking inputs for length and breadth of the rectangle
        double length = scan.nextDouble();
        double breadth = scan.nextDouble();

        // Calculating the perimeter
        double perimeter =  2 * (length + breadth);

        // Calculating the area
        double area = length * breadth;

        // Displaying the results
        System.out.println("Required length is " + perimeter + " m");
        System.out.println("Required quantity of carpet is " + area + " sqm");
    }
}

Step-by-Step Explanation

Let’s break down what each part of the program does:

  1. Importing the Scanner Class:
  • The program starts by importing java.util.Scanner, which allows us to take input from the user.
  1. Creating the Scanner Object:
  • We create a Scanner object named scan to read the input from the user. This is done using Scanner scan = new Scanner(System.in);.
  1. Taking User Input:
  • The program then asks the user to enter the length and breadth of the rectangle. These values are captured using scan.nextDouble() and stored in the length and breadth variables.
  1. Calculating the Perimeter:
  • The perimeter of a rectangle is calculated using the formula 2 * (length + breadth). This tells us how much rope is needed to go around the rectangle.
  1. Calculating the Area:
  • The area of a rectangle is calculated using the formula length * breadth. This tells us how much carpet is needed to cover the entire rectangle.
  1. Displaying the Results:
  • The program then prints out the perimeter and area using System.out.println().
  • The perimeter is displayed in meters (m), and the area is displayed in square meters (sqm).

Example Output

If you enter:

  • Length: 5 meters
  • Breadth: 3 meters

The program will output:

Required length is 16.0 m
Required quantity of carpet is 15.0 sqm

Conclusion

This simple Java program is a great way to understand the basics of taking user input and performing calculations. By working through this example, you’ll become more familiar with fundamental programming concepts like variables, arithmetic operations, and output. Keep practicing, and you’ll be well on your way to mastering Java!

Leave a Comment

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

Scroll to Top