Tuesday, 20 December 2011

02 - Computing volume of Box

//Computing the volume of a Box.

/*
    In the previous program we have seen how to declare a class and object.
   
    Lets compute the voulme of the box. Volume = length*width*height.

    One way of computing the voulme is as shown below:
*/

class Box
{
    int length;
    int width;
    int height;
}

class BoxDemo
{
    public static void main(String args[])
    {
        Box b = new Box();
        b.length = 10;
        b.width = 20;
        b.height = 30;

        int volume = (b.length)*(b.width)*(b.height);
        System.out.println("Volume of the Box is: "+volume);
    }
}

/*
    In the above program "volume" is a local variable with respect to main function.
    So, "volume" can be accessed directly without creating any object.
*/

Download only the program: http://paste2.org/followup/1832510

No comments:

Post a Comment