Site icon TestingDocs.com

Write a Java program to calculate Area of Rectangle

Problem Statement:

Write a Java program to calculate Area of Rectangle. On this page, we will write a java program to calculate the area of the rectangle. Before writing the program we will analyze the

IPO chart

Input

Length, Width

Process

Area of Rectangle = Length x Width

Output 

Area 

Formula

Area = length*width

Tools Used

We will use the below tools to develop the program. 

Eclipse IDE

Some steps and instructions to create a Java application on Eclipse IDE:

https://www.testingdocs.com/create-a-new-java-project-in-eclipse/

https://www.testingdocs.com/create-java-package-in-eclipse-ide/

https://www.testingdocs.com/create-a-new-java-class-in-a-project/

https://www.testingdocs.com/run-java-project-in-eclipse/


Java Program

The program prompts the user to enter the length and the width. The program uses the Scanner class to read the values from the keyboard. The variables are declared as double data types. 

The program calculates the area as per the mathematical formula and displays the result to the screen. 

Launch IDE and create a Java class.

Enter the program code. 

/**************************************************
 * AreaOfRectangle.java
 * @program : Area of Rectangle
 * @web    :  www.TestingDocs.com
 * @author    :  
 * @version   : 1.0
 **************************************************/

import java.util.*;

public class AreaOfRectangle 
{
  public static void main(String arg[ ]) 
  {
    Scanner keyboard = new Scanner(System.in);
    double length, width, areaRectangle;
    System.out.print("Enter Length := ");
    length = keyboard.nextDouble();
    System.out.print("Enter Width := ");
    width = keyboard.nextDouble();
    areaRectangle = length * width;
    System.out.println("Area of Rectangle = "+ areaRectangle);
  }
}

 

Screenshot

Screenshot of the code in the IDE window. 

Sample Output

Save and Run the program from the IDE. Execute some test cases to verify the output of the program. 

Right Click and choose Run as >> Java Application

Output

Enter Length := 5
Enter Width := 3
Area of Rectangle = 15

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/


For more information on Java, visit the official website :

https://www.oracle.com/in/java/

Exit mobile version