List of JAVA Program List of JAVA Program
Program 5 public static void main(String args[])
// This program uses a parameterized method. {
class Box // declare, allocate, and initialize Box objects
{ Box mybox1 = new Box();
double width; Box mybox2 = new Box();
double height; double vol;
double depth; // get volume of first box
// compute and return volume vol = [Link]();
double volume() [Link]("Volume is " + vol);
{ // get volume of second box
return width * height * depth; vol = [Link]();
} [Link]("Volume is " + vol);
// sets dimensions of box }
void setDim(double w, double h, double d) }
{ When this program is run, it generates the following results:
width = w; Constructing Box
height = h; Constructing Box
depth = d; Volume is 1000.0
} Volume is 1000.0
}
class BoxDemo5 Program 7
{
public static void main(String args[]) /* Here, Box uses a parameterized constructor to initialize the dimensions of a box.*/
{ class Box
Box mybox1 = new Box(); {
Box mybox2 = new Box(); double width;
double vol; double height;
// initialize each box double depth;
[Link](10, 20, 15); // This is the constructor for Box.
[Link](3, 6, 9); Box(double w, double h, double d)
// get volume of first box {
vol = [Link](); width = w;
[Link]("Volume is " + vol); height = h;
// get volume of second box depth = d;
vol = [Link](); }
[Link]("Volume is " + vol); // compute and return volume
} double volume()
} {
return width * height * depth;
Program 6 }
}
/* Here, Box uses a constructor to initialize the dimensions of a box.*/ class BoxDemo7
class Box {
{ public static void main(String args[])
double width; {
double height; // declare, allocate, and initialize Box objects
double depth; Box mybox1 = new Box(10, 20, 15);
// This is the constructor for Box. Box mybox2 = new Box(3, 6, 9);
Box() double vol;
{ // get volume of first box
[Link]("Constructing Box"); vol = [Link]();
width = 10; [Link]("Volume is " + vol);
height = 10; // get volume of second box
depth = 10; vol = [Link]();
} [Link]("Volume is " + vol);
// compute and return volume }
double volume() }
{ The output from this program is shown here:
return width * height * depth; Volume is 3000.0
} Volume is 162.0
}
class BoxDemo6
{