0% found this document useful (0 votes)
17 views3 pages

MODULE - 5 Java

This document provides a guide on how to write a Java program to calculate the area of a rectangle using user inputs for length and width. It details the steps for creating the program, including defining a class, importing the Scanner class, and calculating the area using a method. Additionally, it includes example code and output for both a direct calculation and a method-based approach to finding the area.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

MODULE - 5 Java

This document provides a guide on how to write a Java program to calculate the area of a rectangle using user inputs for length and width. It details the steps for creating the program, including defining a class, importing the Scanner class, and calculating the area using a method. Additionally, it includes example code and output for both a direct calculation and a method-based approach to finding the area.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MODULE – 5

JAVA PROGRAMING
• The area of the Rectangle is given by length * width. Here we will write a
Java program to find the area of the rectangle. We will take length and
width variables to store the value of length and width respectively.
Area of Rectangle = length * width
❖ Procedures to write a Java program to calculate the area of the rectangle:-
1) Define class and the main method
2) Declare variables for taking inputs:- width and height
3) Import Scanner class of util package to read inputs
4) Read inputs from end-user and store them in the declared variables
5) Calculate area using the formula, and store it in a variable
6) Display the result
7) Close the Scanner class object
• The value of width and height may be an integer or floating-point value.
So, it is a better idea to take width and length variable as double data
type, because it can store both types of values. The final result will be a
floating-point number hence, declare the area as the double data type.
1. Write a Java Program to Find Area of Rectangle?

import java.util.Scanner;

public class RectangleArea {

public static void main(String[] args) {

// declare variables
double length = 0.0;
double width = 0.0;
double area = 0.0;

// create Scanner class object to read input


Scanner scan = new Scanner(System.in);

// read input
System.out.print("Enter length of the rectangle:: ");
length = scan.nextDouble();
System.out.print("Enter width of the rectangle:: ");
width = scan.nextDouble();

// calculate area
area = length * width;

// display result
System.out.println("Area of Rectangle = "+ area);

// close Scanner class object


scan.close();

}
}

Output:-
Enter length of the rectangle:: 14.5
Enter width of the rectangle:: 10
Area of Rectangle = 145.0
2. Write a Program to find the Area of the rectangle in Java using
method?

• Previously In the Java program to find the area of Rectangle, we


have written logic to find the area of the rectangle inside the main
method. it was the wrong way. We should define a method to
calculate the area of the rectangle and call it from the main method.

import java.util.Scanner;

public class RectangleArea {

public static double findRectangleArea(double len, double wid) {


return len * wid;
}

public static void main(String[] args) {

// declare variables
double length = 0.0;
double width = 0.0;
double area = 0.0;

// create Scanner class object to read input


Scanner scan = new Scanner(System.in);

// read input
System.out.print("Enter length of the rectangle:: ");
length = scan.nextDouble();
System.out.print("Enter width of the rectangle:: ");
width = scan.nextDouble();

// calculate area by calling findRectangleArea()


area = findRectangleArea(length, width);

// display result
System.out.printf("Area of Rectangle = %.2f", area);

// close Scanner class object


scan.close();

}
}
Output:-
Enter length of the rectangle:: 15.5
Enter width of the rectangle:: 12.8
Area of Rectangle = 198.40

TASKS TO BE COMPLETED:

1. Write a Program to find the area of the right-angle triangle in Java?

You might also like