PPT Slide
Using Objects: Creating a Box
class BoxesExample {
public static void main (String[ ] argv) {
Box shoeBox;
shoeBox = new Box( );
shoeBox.setLength(35);
shoeBox.setWidth(19);
shoeBox.setHeight(13);
Box cdBox = new Box( );
cdBox.setLength(14);
cdBox.setWidth(9);
cdBox.setHeight(1);
int iTotalVolumeOfBoxes;
iTotalVolumeOfBoxes =
shoeBox.getVolume()
+ cdBox.getVolume();
System.out.println
(“The combined volume of the boxes”);
System.out.println
(“is: “, iTotalVolumeOfBoxes);
} // of main
} // of class BoxesExampleProgram
First, we declare
a variable
called ‘shoeBox.’
At this point, shoeBox
is merely a null
reference.
Second, we
instantiate shoeBox.
Values are assigned.
Previous slide
Next slide
Back to first slide
View graphic version