JAVA-Object creation of class Box

Q. Create a class called Box having following properties: 
a.Three instance variables for three dimensions 
b.Three constructors – one with no argument, one with one argument and another with three arguments 
c.A method to calculate volume of the Box Now create three objects of class Box using three different constructors and test it.

class box
{
private double l,b,h;
box()
{
l=20.5;
b=10.5;
h=3.0;
}
box(double a)
{
l=a;
b=a;
h=a;
}
box(double x,double y,double z)
{
l=x;
b=y;
h=z;
}
public void volume()
{
double v=l*b*h;
System.out.println("volume= "+v);
}
}
class prog
{
public static  void main(String args[])
{
box ob1=new box();
box ob2=new box(10.85);
box ob3=new box(20.5,60.3,85.2);
System.out.println("volume of 1st object");
ob1.volume();
System.out.println("volume of 2nd object");
ob2.volume();
System.out.println("volume of 3rd object");
ob3.volume();
}
}

Output:
volume of 1st object
volume= 645.75
volume of 2nd object
volume= 1277.289125
volume of 3rd object
volume= 105319.98

//if you have any problem, comment below

Comments