Sunday, 13 October 2013

Program to demonstrate Hierarchical Level Inheritence

//Program to demonstrate Hierarchical Level Inheritence
//Program by Ahlam Ansari

class Shape
{       
    public String display(int choice)
    {
        switch(choice)
        {
            case 1: return("Calculating the area of CIRCLE....");
               
            case 2: return("Calculating the area of RECTANGLE....");
               
            case 3: return("Calculating the area of TRIANGLE....");
               
            default:return("ERROR:PLEASE ENTER CORRECT CHOICE....");

        }
    }
}

class Circle extends Shape
{
    final static float pi=3.14F;
    float radius;
    Circle(float x)
    {
        radius = x;
    }
    public float compute()
    {
        return (pi*radius*radius);
    }
}

class Rectangle extends Shape
{
    float length, breadth;
    Rectangle(float x, float y)
    {
        length = x;
        breadth = y;
    }
    public float compute()
    {
        return (length*breadth);
    }
}

class Triangle extends Shape
{
    final static float half=0.5F;
    float base, height;
    Triangle(float x, float y)
    {
        base = x;
        height = y;
    }
    public float compute()
    {
        return (half*base*height);
    }
}

class HierarchicalLevel
{
    public static void main(String ahlam[])
    {

        Circle c=new Circle(10.0f);
        System.out.println(c.display(1) + "\n " + c.compute()+" square.cm");
        Rectangle r=new Rectangle (10.0f, 10.0f);
        System.out.println(r.display(2) + "\n " + r.compute()+" square.cm");
        Triangle t=new Triangle(10.0f,10.0f);
        System.out.println(t.display(3) + "\n " + t.compute()+" square.cm");
    }
}


/*
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac HierarchicalLevel.java

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java HierarchicalLevel
Calculating the area of CIRCLE....
 314.0 square.cm
Calculating the area of RECTANGLE....
 100.0 square.cm
Calculating the area of TRIANGLE....
 50.0 square.cm

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
 */

No comments:

Post a Comment