Java Code
Lab 4 –
Part A: Create a program that demonstrates multi- class calls that results in a stack where there are at least 3 methods on the stack other than main. Your program should have at least 2 heap objects.
Method3()
Object1
Method2()
Object2
Main()
Method1()
Stack Heap
Part B: Create a program or extend part so that you create a constructor for one of the object class that is different from the default. Also, the constructor must be overloaded so the there are two different ways that the object can be initialized.
The below code is an EXAMPLE of how to set up constructor overloading for the box class.
You should replace box with your own class.
Do not submit this example as your lab
// Java program to illustrate
// Constructor Overloading
class Box
{
double width, height, depth;
// constructor used when all dimensions
// specified
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions
// specified
Box()
{
width = height = depth = 0;
}
// constructor used when cube is created
Box(double len)
{
width = height = depth = len;
}
// compute and return volume
double volume()
{
return width * height * depth;
}
}
public class Test
{
public static void main(String args[])
{
// create boxes using the various
// constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println(" Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println(" Volume of mybox2 is " + vol);
// get volume of cube
vol = mycube.volume();
System.out.println(" Volume of mycube is " + vol);
}
}