Saturday, 7 September 2013

parameterized constructor in java

class Box{
double width;
double height;
double depth;
//this is the constructor for Box

        Box(double w, double h, double d)
        {
        width = w;
        height = h;
        depth = d;
        }
// compute and return value
        double volume()
        {
        return width*height*depth;
        }
}

    class BoxParConst{
    public static void main(String args[]){
// declare allocate and initialize Box objects

    Box mybox1 = new Box(10,20,15);
    Box mybox2 = new Box(3,6,9);
   
    double vol;
   
// get the volume of first box
   
    vol = mybox1.volume();
    System.out.println("Volume of the first Box is:"+vol);

// get the volume of second box

    vol = mybox2.volume();
    System.out.println("Volume of the second Box is:"+vol);
   
    }
}