Java program to calculate area of rectangle code example
Example 1: Java program to find area of rectangle using function
import java.util.Scanner;
public class RectangleAreaUsingFunction
{
public static void areaRectangle(double width, double height)
{
double area;
area = width * height;
System.out.println("Area of rectangle : " + area);
}
public static void main(String[] args)
{
double width, height;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter length of rectangle : ");
width = sc.nextDouble();
System.out.println("Please enter width of rectangle : ");
height = sc.nextDouble();
areaRectangle(width, height);
sc.close();
}
}
Example 2: Calculate area of rectangle using class in java
import java.io.*;
class RectangleShape
{
int length, breadth;
void setValue(int l, int b)
{
length = l;
breadth = b;
}
int findArea()
{
return (length * breadth);
}
}
public class RectangleAreaDemo
{
public static void main(String[] args)
{
RectangleJava obj = new RectangleJava();
obj.setValue(10, 5);
System.out.println("Area of rectangle: " + obj.findArea());
}
}
Example 3: Java program to calculate area of rectangle
import java.util.Scanner;
public class RectangleArea
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter length of rectangle: ");
double length = sc.nextDouble();
System.out.println("Please enter width of rectangle: ");
double width = sc.nextDouble();
double area = length * width;
System.out.println("Area of rectangle is: " + area);
sc.close();
}
}