//java program of multi level hierarchy
class Box
{
double width;
double height;
double depth;
Box() //when no argument is passed
{
width=1.5;
height=1.2;
depth=1.1;
}
Box(Box ob) // when pass object to constructor
{
width=ob.width;
height=ob.height;
depth=ob.depth;
}
Box(double w, double h, double d)
{
width=w;
height=h;
depth=d;
}
Box(double len)
{
width=height=depth=1;
}
double Volume()
{
return width*height*depth;
}
}
class BoxWeight extends Box //inherit box class
{
double weight;
BoxWeight() //when no argument is passed
{
super(); //call superclass constructor
weight=1.1;
}
BoxWeight(BoxWeight ob) // when pass object to constructor
{
super(ob); //call superclass constructor
weight=ob.weight;
}
BoxWeight(double w, double h, double d,double wgt)
{
super(w,h,d); //call superclass constructor
weight=wgt;
}
BoxWeight(double len,double m)
{
super(len); //call superclass constructor
weight=m;
}
}
class Shipment extends BoxWeight //inherit boxweight class and method
{
double cost;
Shipment() //when no argument is passed
{
super(); //call superclass constructor
cost=2.1;
}
Shipment(Shipment ob) // when pass object to constructor
{
super(ob); //call superclass constructor
cost=ob.cost;
}
Shipment(double w, double h, double d,double wgt,double c)
{
super(w,h,d,wgt); //call superclass constructor
cost=c;
}
Shipment(double len,double m,double c)
{
super(len,m); //call superclass constructor
cost=c;
}
}
class MainBox
{
public static void main(String ar[])
{
Shipment s1=new Shipment(10,20,15,10,3.41); // call constructor with five parameter
Shipment s2=new Shipment(20,5,10); // call constructor with three argument
Shipment s3=new Shipment(s1); //call constructor with object reference
Shipment s4=new Shipment(); //call constructor with no parameter
double vol;
vol=s1.Volume();
System.out.println("Volume of shipment1 is "+vol);
System.out.println("Weight of shipment1 is "+s1.weight);
System.out.println("Shipping cost is"+s1.cost);
System.out.println();
vol=s2.Volume();
System.out.println("Volume of shipment2 is "+vol);
System.out.println("Weight of shipment2 is "+s2.weight);
System.out.println("Shipping cost is"+s2.cost);
System.out.println();
vol=s3.Volume();
System.out.println("Volume of shipment1 is "+vol);
System.out.println("Weight of shipment1 is "+s3.weight);
System.out.println("Shipping cost is"+s3.cost);
System.out.println();
vol=s4.Volume();
System.out.println("Volume of shipment1 is "+vol);
System.out.println("Weight of shipment1 is "+s4.weight);
System.out.println("Shipping cost is"+s4.cost);
System.out.println();
}
}
class Box
{
double width;
double height;
double depth;
Box() //when no argument is passed
{
width=1.5;
height=1.2;
depth=1.1;
}
Box(Box ob) // when pass object to constructor
{
width=ob.width;
height=ob.height;
depth=ob.depth;
}
Box(double w, double h, double d)
{
width=w;
height=h;
depth=d;
}
Box(double len)
{
width=height=depth=1;
}
double Volume()
{
return width*height*depth;
}
}
class BoxWeight extends Box //inherit box class
{
double weight;
BoxWeight() //when no argument is passed
{
super(); //call superclass constructor
weight=1.1;
}
BoxWeight(BoxWeight ob) // when pass object to constructor
{
super(ob); //call superclass constructor
weight=ob.weight;
}
BoxWeight(double w, double h, double d,double wgt)
{
super(w,h,d); //call superclass constructor
weight=wgt;
}
BoxWeight(double len,double m)
{
super(len); //call superclass constructor
weight=m;
}
}
class Shipment extends BoxWeight //inherit boxweight class and method
{
double cost;
Shipment() //when no argument is passed
{
super(); //call superclass constructor
cost=2.1;
}
Shipment(Shipment ob) // when pass object to constructor
{
super(ob); //call superclass constructor
cost=ob.cost;
}
Shipment(double w, double h, double d,double wgt,double c)
{
super(w,h,d,wgt); //call superclass constructor
cost=c;
}
Shipment(double len,double m,double c)
{
super(len,m); //call superclass constructor
cost=c;
}
}
class MainBox
{
public static void main(String ar[])
{
Shipment s1=new Shipment(10,20,15,10,3.41); // call constructor with five parameter
Shipment s2=new Shipment(20,5,10); // call constructor with three argument
Shipment s3=new Shipment(s1); //call constructor with object reference
Shipment s4=new Shipment(); //call constructor with no parameter
double vol;
vol=s1.Volume();
System.out.println("Volume of shipment1 is "+vol);
System.out.println("Weight of shipment1 is "+s1.weight);
System.out.println("Shipping cost is"+s1.cost);
System.out.println();
vol=s2.Volume();
System.out.println("Volume of shipment2 is "+vol);
System.out.println("Weight of shipment2 is "+s2.weight);
System.out.println("Shipping cost is"+s2.cost);
System.out.println();
vol=s3.Volume();
System.out.println("Volume of shipment1 is "+vol);
System.out.println("Weight of shipment1 is "+s3.weight);
System.out.println("Shipping cost is"+s3.cost);
System.out.println();
vol=s4.Volume();
System.out.println("Volume of shipment1 is "+vol);
System.out.println("Weight of shipment1 is "+s4.weight);
System.out.println("Shipping cost is"+s4.cost);
System.out.println();
}
}







Comments
Post a Comment