created: 09/23/99; revised 07/04/2011
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.
The variables should be floating point because integer dimensions would be too much of a restriction.
Nearly always, if you use floating point you should use double
.
5. Complete the Constructors. Constructors initialize the instance variables of the objects 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 specification 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 specification 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:
2 * (sum of area of each unique side)
8. Testing 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
.
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.