0% found this document useful (0 votes)
19 views

List of JAVA Program List of JAVA Program

This document contains summaries of 7 Java programs that demonstrate using constructors and methods in classes. Program 5 uses a parameterized method in the Box class to calculate volume. Program 6 initializes Box dimensions using a constructor. Program 7 initializes Box using a parameterized constructor. Both programs output the volumes of example Box objects.

Uploaded by

aman dhyani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

List of JAVA Program List of JAVA Program

This document contains summaries of 7 Java programs that demonstrate using constructors and methods in classes. Program 5 uses a parameterized method in the Box class to calculate volume. Program 6 initializes Box dimensions using a constructor. Program 7 initializes Box using a parameterized constructor. Both programs output the volumes of example Box objects.

Uploaded by

aman dhyani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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 = mybox1.volume();
double volume() System.out.println("Volume is " + vol);
{ // get volume of second box
return width * height * depth; vol = mybox2.volume();
} System.out.println("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;
mybox1.setDim(10, 20, 15); // This is the constructor for Box.
mybox2.setDim(3, 6, 9); Box(double w, double h, double d)
// get volume of first box {
vol = mybox1.volume(); width = w;
System.out.println("Volume is " + vol); height = h;
// get volume of second box depth = d;
vol = mybox2.volume(); }
System.out.println("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
System.out.println("Constructing Box"); vol = mybox1.volume();
width = 10; System.out.println("Volume is " + vol);
height = 10; // get volume of second box
depth = 10; vol = mybox2.volume();
} System.out.println("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
{

You might also like