Rectangle Java Program

Hey guys!! Welcome to flower brackets blog. Today we are going to learn rectangle java program.

rectangle java program

In this program user will be prompted to enter the length and width of a rectangle.

Also Read – Area of Circle Java Program

We will be using three classes to find area of rectangle namely BufferedReader, IOException and InputStreamReader of java.io package.

So let’s see how to write a java program to find area of rectangle,

Example: rectangle java program

/* Area of rectangle is, Area = length * width. */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class RectangleArea
{
   public static void main(String[] args)
   {
      int width = 0;
      int length = 0;

      try
      {
         // reading the length from console
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         System.out.println("Enter length of a rectangle: ");
         length = Integer.parseInt(br.readLine());

         // reading the width from console
         System.out.println("Enter width of a rectangle: ");
         width = Integer.parseInt(br.readLine());
      }

      // to check if invalid value is entered
      catch(NumberFormatException nfe)
      {
         System.out.println("Invalid value " + nfe);
         System.exit(0);
      }

      catch(IOException ioe)
      {
         System.out.println("IOError: " +ioe);
         System.exit(0);
      }
      int area = length * width;
      System.out.println("Area of rectangle is " + area);
   }
}

Output:

Enter length of a rectangle: 5
Enter width of a rectangle: 4
Area of rectangle is 20


Finding perimeter of rectangle

Here we are going find the perimeter of rectangle using the formula as below

// Perimeter of rectangle is, Perimeter = 2 * ( length + width.) 

import java.util.Scanner;

public class RectanglePerimeterDemo
{
   public static void main(String[] args)
   {
      int length, width, perimeter;
      Scanner sc = new Scanner(System.in);

      System.out.println("Enter length of a rectangle: ");
      length = sc.nextInt();

      System.out.println("Enter length of a rectangle: ");
      width = sc.nextInt(); 

      perimeter = 2 * ( length + width ); 
      System.out.println("The perimeter of Rectangle is : " + perimeter);

      sc.close();
   }
}

Output:

Enter length of a rectangle: 5
Enter width of a rectangle: 10
The perimeter of Rectangle is : 30


Rectangle java program using function

In this program I am using a function which computes the rectangle area java using two parameters length and width.

Then the value “area” is returned to the main function from where it is called.

Let’s see the code execution,

import java.util.*;
 
public class AreaRectangle
{ 
   public static void main(String []args)
   {
      double length, width, area;
 
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter length of a rectangle: ");
      length = sc.nextDouble();
      System.out.print("Enter width of a rectangle: ");
      width = sc.nextDouble();
 
      area = AreaOfRectangle(length, width);

      System.out.println("Area of Rectangle: " + area);
      sc.close();
   }
 
   public static double AreaOfRectangle(double length, double width)
   {
      double area;
      area = length * width;
      return area;
   }
}

Output:

Enter length of a rectangle: 60
Enter width of a rectangle: 40
Area of Rectangle: 2400.0


Without using scanner class and functions

We can also write java program to calculate area of rectangle without using scanner class and functions.

This is how we can accomplish,

public class AreaRectangle
{ 
   public static void main(String []args)
   {
      double length = 60.0;
      double width = 40.0;
      double area, perimeter;
 
      area = length * width;
      System.out.print("The area of rectangle is : " + area + "\n");
 
      perimeter = 2 * ( length + width );
      System.out.print("The perimeter of rectangle is : " + perimeter);
   } 
}

Output:

The area of rectangle is : 2400.0
The perimeter of rectangle is : 200.0


Using class and objects

Let’s see java program to find the area of rectangle, square and circle using class and object. Here’s the code,

public class RectangleAreaDemo
{
   public static void main(String[] args)
   {
      Area area = new Area();
      area.circleArea(5);
      area.squareArea(10);
      area.rectangleArea(10, 4);
   }

   public class Area
   {
      void squareArea(float a)
      {
         System.out.println("Area of the square is : " + Math.pow(a, 2) + " sq units");
      }
      void rectangleArea(float a, float b)
      {
         System.out.println("Area of the rectangle is : " + a * b + " sq units");
      }
      void circleArea(double a)
      {
         double c = 3.14 * a * a;
         System.out.println("Area of the circle is : " + c + " sq units");
      }
   }
}

Output:

Area of the circle is : 78.5 sq units
Area of the square is : 100.0 sq units
Area of the rectangle is : 40.0 sq units


Another way

Let’s learn another way to find area of rectangle using class and objects,

// this class has getter and setter methods to set and get values

public class RectangleShape 
{
   private double length;
   private double width;

   public void setLength(double len)
   {
      length = len;
   }

   public void setWidth(double wid)
   {
      width = wid;
   }

   public double getLength()
   {
      return length;
   }

   public double getWidth()
   {
      return width;
   }

   public double getArea()
   {
      return length * width;
   }
}

// main method

import java.util.Scanner;

public class FindArea 
{
   public static void main(String[] args) 
   {
      RectangleShape rs = new RectangleShape();
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter the length : ");
      double a = sc.nextDouble();
      rs.setLength(a);
      System.out.println("Please enter the width : ");
      double b = sc.nextDouble(); 
      rs.setWidth(b);

      System.out.println("Length of rectangle is : " + rs.getLength());
      System.out.println("Width of rectangle is : " + rs.getWidth());
      System.out.println("Area of rectangle is : " + rs.getArea());
      sc.close();
   }
}

Output:

Please enter the length : 30
Please enter the width : 50
Length of rectangle is : 30.0
Width of rectangle is : 50.0
Area of rectangle is : 1500.0


Java program to find area of rectangle using constructor

Here, we are going to learn java program to find area of rectangle using constructor. Let’s see the code implementation,

public class JavaArea 
{
   public static void main(String[] args) 
   {
      RectangleJava rj = new RectangleJava(); 
      RectangleJava rj1 = new RectangleJava(50); 
      RectangleJava rj2 = new RectangleJava(20,70); 
      System.out.println("Area of first rectangle in java : " + rj.getData()); 
      System.out.println("Area of second rectangle in java : " + rj1.getData()); 
      System.out.println("Area of third rectangle in java : " + rj2.getData());
   }
}

class RectangleJava
{ 
   int length, breadth; 
   RectangleJava(int a, int b) 
   { 
      length = a; 
      breadth = b; 
   } 
   RectangleJava(int a) 
   { 
      length = a; 
      breadth = 10; 
   } 
   RectangleJava() 
   { 
      length = 30; 
      breadth = 20; 
   } 
   float getData() 
   { 
      return(length * breadth); 
   } 
}

Output:

Area of first rectangle in java : 600.0
Area of second rectangle in java : 500.0
Area of third rectangle in java : 1400.0


Now let’s learn two more methods to find area of rectangle in java with parameters and without parameters using an example,

// java program to determine area of rectangle without parameters

class Rectangle
{
   double length, breadth;
   double area()
   {
      return length * breadth;
   }
}

public class AreaJava 
{
   public static void main(String[] args) 
   {
      double area;
      Rectangle rec = new Rectangle();
      rec.length = 20;
      rec.breadth = 70;
      area = rec.area();
      System.out.println("Area of rectangle in java : " + area);
   }
}

Output:

Area of rectangle in java : 1400.0


// java program to determine area of rectangle with parameters

class JavaRectangle
{
   double length, breadth;
   double findArea()
   {
      return length * breadth;
   }
   void calcValue(double l, double b)
   {
      length = l;
      breadth = b;
   }
}

public class RectangleAreaJava 
{
   public static void main(String[] args) 
   {
      double area;
      JavaRectangle rect = new JavaRectangle();
      rect.calcValue(30,50);
      area = rect.findArea();
      System.out.println("Area of rectangle in java : " + area);
   }
}

Output:

Area of rectangle in java : 1500.0


conclusion

That’s it guys. So this is how to find area of a rectangle program in java. You also learnt java program to find area of rectangle using constructor.

I hope you guys have understood the concept. You can subscribe to my blog flower brackets if you haven’t already.

If you like this post share it across social networks.

Related Posts