Blog

object oriented programs to calculate the area in java.

object oriented program to find area

5) Write object oriented programs to calculate the area of a
a) Circle
b) Square
c) Rectangle
d) Sphere

  • 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.
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assignments;

/**
 *
 * @author 5442
 */
public class Qs5ObjectOriented {
    private double l;
    private double b;
    
    public Qs5ObjectOriented(double l, double b){
        this.l = l;
        this.b = b;
    }
    
    public static void main(String[] args) {
        Qs5ObjectOriented x = new Qs5ObjectOriented(5,6);
           System.out.println("the area of circle is "+x.Circlearea()+"\n The area of Square is "+x.Squarearea() +
                "\n the area of Rectangle is : "+x.Rectanglearea()+ "\n The area of Sphere is: "+x.Spherearea());
    }
    
    public double Circlearea(){
        return 2*Math.PI*l*l;
    }
    public double Squarearea(){
        return l*l;
    }
    public double Rectanglearea(){
    return l*b;
}
    public double Spherearea(){
        return 4*Math.PI*l*l;
    }
}

Output of this program :

run:
the area of circle is 157.07963267948966
The area of Square is 25.0
the area of Rectangle is : 30.0
The area of Sphere is: 25.0
BUILD SUCCESSFUL (total time: 0 seconds)