Blog

Write an object oriented program to find area and perimeter of rectangle.

object oriented program to find area and perimeter of rectangle.

object oriented program to find area and perimeter of rectangle in java / Write an object oriented program in java to find area and perimeter of rectangle.

Objectives of Object oriented Programming Language

  • In object oriented programming, program is divided into small parts called objects.
  • Object oriented programming follows bottom up approach.
  • Adding new data and function is easy.
  • Object oriented programming provides data hiding so it is more secure.
  • Object oriented programming is based on real world.

Answer :

package assignments;
/**
 *
 * @author achyut1324
 */
public class AreaAndPerimeter {
    private double l, b;
    public AreaAndPerimeter(double l, double b){
        this.l = l;
        this.b = b;
    }
    public double area(){
        return l*b;
    }
    public double perimeter(){
        return 2*(l+b);
    }
    
    public static void main(String[] args) {
     AreaAndPerimeter rectangle = new AreaAndPerimeter(2,4); 
        System.out.println("Area of rectangle is : "+ rectangle.area());
         System.out.println("Perimeter of rectangle is : "+ rectangle.perimeter());
    } 
}

Output of the Program :

run:
Area of rectangle is : 8.0
Perimeter of rectangle is : 12.0
BUILD SUCCESSFUL (total time: 0 seconds)