List of Practice Program in JAVA
Program 1
class Box
{
double width;
double height;
double depth;
}
// This class declares an object of type Box.
class BoxDemo
{
public static void main(String args[])
{
Box mybox = new Box();
double vol;
// assign values to mybox's instance variables
[Link] = 10;
[Link] = 20;
[Link] = 15;
// compute volume of box
vol = [Link] * [Link] * [Link];
[Link]("Volume is " + vol);
}
}
Program 2
// This program declares two Box objects.
class Box
{
double width;
double height;
double depth;
}
class BoxDemo2
{
public static void main(String args[])
{
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
[Link] = 10;
[Link] = 20;
[Link] = 15;
/* assign different values to mybox2's instance variables */
[Link] = 3;
[Link] = 6;
[Link] = 9;
// compute volume of first box
vol = [Link] * [Link] * [Link];
[Link]("Volume is " + vol);
// compute volume of second box
vol = [Link] * [Link] * [Link];
[Link]("Volume is " + vol);
}
}
Program 3
// This program includes a method inside the box class.
class Box
{
double width;
double height;
double depth;
// display volume of a box
void volume()
{
[Link]("Volume is ");
[Link](width * height * depth);
}
}
class BoxDemo3
{
public static void main(String args[])
{
Box mybox1 = new Box();
Box mybox2 = new Box();
// assign values to mybox1's instance variables
[Link] = 10;
[Link] = 20;
[Link] = 15;
/* assign different values to mybox2's instance variables */
[Link] = 3;
[Link] = 6;
[Link] = 9;
// display volume of first box
[Link]();
// display volume of second box
[Link]();
}
}
Program 4
// Now, volume() returns the volume of a box.
class Box
{
double width;
double height;
double depth;
// compute and return volume
double volume()
{
return width * height * depth;
}
}
class BoxDemo4
{
public static void main(String args[])
{
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
[Link] = 10;
[Link] = 20;
[Link] = 15;
/* assign different values to mybox2's
instance variables */
[Link] = 3;
[Link] = 6;
[Link] = 9;
// get volume of first box
vol = [Link]();
[Link]("Volume is " + vol);
// get volume of second box
vol = [Link]();
[Link]("Volume is " + vol);
}
}