Write a Java program to calculate Area of Rectangle [ 2024 ]
Write a Java program to calculate Area of Rectangle
Write a Java program to calculate the Area of the 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.Â
- JDK ( Java Development Kit)
- Eclipse IDE( Integrated Development Environment)Â
Eclipse IDE
Some steps and instructions to create a Java application on Eclipse IDE:
- Create Java Project in Eclipse
https://www.testingdocs.com/create-a-new-java-project-in-eclipse/
- Create Java Package in Eclipse
https://www.testingdocs.com/create-java-package-in-eclipse-ide/
- Create Java Class in Eclipse
https://www.testingdocs.com/create-a-new-java-class-in-a-project/
- Run Java Project in Eclipse
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 Tutorials
Java Tutorial on this website:
For more information on Java, visit the official website :