MODULE - 5 Java
MODULE - 5 Java
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;
// declare variables
double length = 0.0;
double width = 0.0;
double area = 0.0;
// 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);
}
}
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?
import java.util.Scanner;
// declare variables
double length = 0.0;
double width = 0.0;
double area = 0.0;
// read input
System.out.print("Enter length of the rectangle:: ");
length = scan.nextDouble();
System.out.print("Enter width of the rectangle:: ");
width = scan.nextDouble();
// display result
System.out.printf("Area of Rectangle = %.2f", area);
}
}
Output:-
Enter length of the rectangle:: 15.5
Enter width of the rectangle:: 12.8
Area of Rectangle = 198.40
TASKS TO BE COMPLETED: