creation: 09/23/99


Fill in the Blanks

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:

class

A class that models a cardboard box.

Constructors

Box ( double ,
      double ,
      double   )

The constructor should not include volume and area as parameters because these values can be calculated from the width, height, and length of the box. As a convenience, include a constructor to use when all sides of the box are the same size (when it is a cube):

Box ( double  )

Methods

// calculate the volume of the box
double  

// calculate the total surface area of the box
double  


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.


class BoxTester
{

  public static void main ( String[] args )
  {
     Box box = new   ;

     System.out.println( "Area: "    + box. +
                         "  volume: " + box. );

  }
}


3.      Skeleton of the Class.    Fill in the blanks that give the over-all design of the class.

class Box
{
  //  

  //  

  //  

}


4.      Fill in Instance Variables.    Fill in the data type of each instance variable.

class Box
{
  // Instance Variables
   length ;
   width ;
   height ;

  // Constructors

  // Methods

}


5.      Complete the Constructors.    The constructors will initialize the instance variables of the object being constructed.

class Box
{
  // Instance Variables
  double length ;
  double width  ;
  double height ;

  // Constructors
  ( double , double , double   )
  {
    this.width   =   ;
    this.height  =  ;
    this.length  =  ;
  }

  // initialize all sides to the same value
  ( double  )
  {
    width   =  ;
    height  =  ;
    length  =  ;
  }


  // Methods

}

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

class Box
{
  // Instance Variables
  double length ;
  double width  ;
  double height ;


  // Constructors
  Box ( double width, double height, double length )
  {
    this.width  = width ;
    this.height = height ;
    this.length = length ;
  }

  Box ( double side )
  {
    width  = side ;
    height = side ;
    length = side ;
  }

  // Methods
  double volume()
  {
    return  *  * ;
  }

}

(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: 2 * (sum of area of each unique side)

class Box
{
  // Instance Variables
  double length ;
  double width  ;
  double height ;


  // Constructors
  Box ( double width, double height, double length )
  {
    this.width  = width ;
    this.height = height ;
    this.length = length ;
  }

  Box ( double side )
  {
    width  = side ;
    height = side ;
    length = side ;
  }

  // Methods
  double volume()
  {
    return width * height * length ;
  }

  double area()
  {
    return 2 * (  *  +  
                  *  +  
                  *  ) ;
  }

}


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:

import  ;

class BoxTester
{

  public static void main ( String[] args )
  {

     // create a box with sides= 2.5, 3.0, and 5.0

     Box box1 = new  Box( 2.5, 3.0, 5.0 );

     System.out.println( "Box1 Area: "    + box1.area() + 
                         "  Volume: " + box1.volume()  );

     // create a box with all sides = 3.0

     Box box2 =   Box (  );

     System.out.println( "Box2 Area: "    + box2.area() + 
                         "  Volume: " + box2.volume()  );


  }
}


9.      Compile each File.     Each of the two files must be compiled:

C:\MyFiles>javac   

C:\MyFiles>javac   


10.      Run the Program.     The java bytecode interpreter must be started with the *.class file that contains the main() method:

C:\MyFiles>java   


End of the Exercise. If you want to do it again, click on "Refresh" in your browser window.

Click here to go back to the main menu.