Write a program to find area of geometrical figures using method.

import java.util.*;
class geofig
{
    double area(double r)
    {
        return(3.14*r*r);
    }
    float area(float s)
    {
        return(s*s);
    }
    float area(float l,float b)
    {
        return(l*b);
    }
    double area(double b,double h)
    {
        return(0.5*b*h);
    }
}

class geo
{
    public static  void main(String arg[])
    {
        Scanner sc =new Scanner(System.in);
        geofig g = new geofig();
        System.out.println("enter double value for radius of circle");
        double r =sc.nextDouble();
        System.out.println("area of circle="+g.area(r));
        System.out.println("enter float value for side of a square");
        float s =sc.nextFloat();
        System.out.println("area of square="+g.area(s));
        System.out.println("enter float value for length and braedth of rectangle");
        float l =sc.nextFloat();
        float b =sc.nextFloat();
        System.out.println("area of rectangle="+g.area(l,b));
        System.out.println("enter double value for base & height of triangle");
        double b1 =sc.nextDouble();
        double h =sc.nextDouble();
        System.out.println("area of triangle="+g.area(b1,h));
    }
}

Output

C:\Users\Jaisha\Desktop\Java>javac geo.java

C:\Users\Jaisha\Desktop\Java>java geo
enter double value for radius of circle
3.5
area of circle=38.465
enter float value for side of a square
3
area of square=9.0
enter float value for length and braedth of rectangle
3 5
area of rectangle=15.0
enter double value for base & height of triangle
2 4
area of triangle=4.0

No comments:

Post a Comment