creation: 09/23/99
1. Design of the Box Class. Objects of this class will represent a box (such as a cardboard box.) The sides of a box are rectangles. A Box will have three essential characteristics: width, height, and length. From these characteristics other values may be calculated: volume and total surface area.
Fill in the blanks in the following design for the class:
2. Checking the Design. To check the design, write a small program that uses the class. Write a program that creates a Box object with sides of 2.5, 5.0, and 6.0 inches. Write out its surface area and volume.
3. Skeleton of the Class. Fill in the blanks that give the over-all design of the class.
4. Fill in Instance Variables. Fill in the data type of each instance variable.
5. Complete the Constructors. The constructors will initialize the instance variables of the object being constructed.
When an instance variable and a parameter use the same identifier, you specify the instance variable of the object by saying "this.identifier" as in the above.
6. Complete a Method. The documentation for the volume( ) method says it looks like this:
// calculate the volume of the box double volume()
The formula to use is: volume = product of all three sides
(Of course, the three instance variables could be used in any order in the arithmetic expression.)
7. Complete the other Method. The documentation for
the area()
method says it looks like this:
double area()
.
There are three pairs of sides to a box.
Opposite sides have the same area, so the calculation looks like this:
8. Import the Class. At this point all
the coding for the definition of Box
is done.
Say that the small test program (class BoxTester
) is in a
file called BoxTester.java
and that the complete definition of
the Box
class is in a file called Box.java
.
The code for BoxTester
has to tell the compiler that it is
using a class defined outside its file with an import
statement:
9. Compile each File. Each of the two files must be compiled:
10. Run the Program. The java bytecode interpreter must be started with the *.class file that contains the main() method:
End of the Exercise. If you want to do it again, click on "Refresh" in your browser window.