creation: 09/30/99; revised 03/07/00


Chapter 35 Fill in the Blanks

This exercise will give you practice in composing objects out of other objects. First you will define the Jam class. Objects of this class represent jars of fruit preserves. Then several these jars will be placed in a Pantry.

1.       Design of the Jam Class.     Objects of this class will represent a jar of fruit preserves such as your grandmother made every Fall. Think of three values you would like to know about a jar of jam.

Now think of some methods that apply to a Jam object. It is almost always a good idea to have a way to print the data of an object:

This is a somewhat silly example, and it would not be unusual if you thought of different ways to fill these blanks.


2.      Document the class.     Now fill in the blanks for the documentation of the class:

class 

A class that models a jar of jam.

Constructors

Jam ( String ,      // think of a good parameter name
      String ,      // for each of the three values
      int   )
Methods

// check if the jar is empty
public boolean  

// remove some jam --- spread an amount of it on toast
public void  spread 

// print info about the jar
public void print()


3.      Checking the Design.    To check the design, write a small program that uses the class. Of course, the program can't be compiled and run until the class is written, but you can get a feel for if the class design is sensible by doing this.

class JamTester
{

  public static void main ( String[] args )
  {
       jar = new   ( "crab apple", "9/30/99", 12 );

     jar.print();

     if ( jar. )
       System.out.println("Can't spread any jam: jar is empty.");
     else
     {
       jar. )
       System.out.println("Spreading 1 oz. of jam.");
       jar.print();
     }

  }
}

You may have some doubts that this is a sensible program. Perhaps it would be a good idea to have the spread() method print out a message each time it is used.



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

class Jam
{
  // Instance Variables
    contents;
   date ;
   capacity;

  // Constructors

  // Methods

}


5.      Complete the Constructor.    The constructor will initialize the instance variables of the object being constructed.

class Jam
{
  // Instance Variables
  String contents ;   // type of fruit in the jar
  String date  ;      // date of canning
  int capacity ;      // amount of jam in the jar

  // Constructors
  Jam( String contents, String date, int size )
  {
     . contents = contents  ;
     . date = date ;
     = size;
  }



  // Methods

}


6.      Complete the Methods.     If the user wants to spread more jam than is available, spread only the amount that is left in the jar.

class Jam
{
  // Instance Variables
  String contents ;   // type of fruit in the jar
  String date  ;      // date of canning
  int capacity ;      // amount of jam in the jar


  // Constructors
  Jam( String contents, String date, int size )
  {
    this . contents = contents  ;
    this . date = date ;
    capacity = size;
  }

  // Methods
  public boolean empty ()
  {
     return (   ==  ) ;
  }

  public void print ()
  {
     System.out.println (   + "   " +
         + "   " +  + " fl. oz." ) ;
  }

  public void spread ( int fluidOz)
  {
     if ( !empty() )
     {
         if ( fluidOz <= capacity )
         {
             System.out.println("Spreading " +  + " fluid ounces of "
                 + contents );

             capacity = capacity -  ;

         }
         else
         {
             System.out.println("Spreading " +  + " fluid ounces of "
                 + contents );

             capacity =  ;
         }
     }
     else
         System.out.println("No jam in the Jar!");
  }

}


7.      Design of the Pantry class.     Let us say that a pantry consists of three jars of jam of any type and size. The methods of a pantry will be to:

  1. List the contents of the pantry.
  2. To select a jar by number
  3. To spread jam from the selected jar.

To use a jar of jam, one must first go to the pantry and select one.

Here is the documentation:

class 

A class that models pantry full of jam.

Constructors

Pantry (   jar1,
           jar2,
           jar3 )
Note: this constructor will take three object references as parameters. The three objects will be the three jars to be kept in the pantry. The objects must already exist before using this constructor.
Methods

// print the contents of the pantry
  void print()

// select a jar by number
public void  select 

// spread an amount of jam from the selected jar
public void  spread 



8.      Instance Variables and constructor.     Decide on the instance variables. There will be a variable that refers to the currently selected jar of jam. When no jar is selected, it should refer to no object. Outsiders should not be able to directly change the variables.

class Pantry
{
  // Instance Variables
  private  jar1 ;
  private  jar2 ;
  private  jar3 ;
  private  selected ;

  // Constructors
  Pantry( Jam jar1, Jam jar2, Jam jar3 )
  {
     . jar1 = jar1 ;
     . jar2 = jar2 ;
     . jar3 = jar3 ;
    selected =   ;
  }

  // Methods

}


9.      Implement print method.     The print method will print each of the three jars in the pantry.

class Pantry
{
  // Instance Variables
  private Jam  jar1 ;
  private Jam  jar2 ;
  private Jam  jar3 ;

  // Constructors
  Pantry( Jam jar1, Jam jar2, Jam jar3 )
  {
    this . jar1 = jar1 ;
    this . jar2 = jar2 ;
    this . jar3 = jar3 ;
    selected = null ;
  }

  // Methods
  public void print()
  {
    System.out.print("1: ");   . print() ;
    System.out.print("2: ");   . print() ;
    System.out.print("3: ");   . print() ;
  }
}

Be sure that you understand



10.      Implement the select() and spread() methods.

class Pantry
{
  // Instance Variables
  private Jam  jar1 ;
  private Jam  jar2 ;
  private Jam  jar3 ;
  private Jam  selected ;

  // Constructors
  Pantry( Jam jar1, Jam jar2, Jam jar3 )
  {
    this . jar1 = jar1 ;
    this . jar2 = jar2 ;
    this . jar3 = jar3 ;
    selected = null ;
  }

  // Methods
  public void print()
  {
    System.out.print("1: "); jar1 . print() ;
    System.out.print("2: "); jar2 . print() ;
    System.out.print("3: "); jar3 . print() ;
  }

  // assume that the user entered a correct selection, 1, 2, or 3
  public void select( int jarNumber )
  {
    if ( jarNumber   1 )

      selected =   ;

    else if ( jarNumber   2 )

      selected =   ;

    else

      selected =   ;
  }

  // spread the selected jam
  public void spread( int oz )
  {
     . spread( oz ) ;
  }
}


Entire Program, with testing class:     You might wish to copy this program to NotePad, save it to a file, and to play with it.

class Jam
{
  // Instance Variables
  String contents ;   // type of fruit in the jar
  String date  ;      // date of canning
  int capacity ;      // amount of jam in the jar


  // Constructors
  Jam( String contents, String date, int size )
  {
    this . contents = contents  ;
    this . date = date ;
    capacity = size;
  }

  // Methods
  public boolean empty ()
  {
    return ( capacity== 0 ) ;
  }

  public void print ()
  {
    System.out.println ( contents + "   " +  date + "   " +  capacity + " fl. oz." ) ;
  }

  public void spread ( int fluidOz)
  {
    if ( !empty() )
    {
      if ( fluidOz <= capacity )
      {
        System.out.println("Spreading " + fluidOz + " fluid ounces of "
            + contents );
        capacity = capacity - fluidOz ;
      }
      else
      {
        System.out.println("Spreading " + capacity + " fluid ounces of "
            + contents );
        capacity =  0 ;
      }
     }
     else
       System.out.println("No jam in the Jar!");
  }

}

class Pantry
{
  // Instance Variables
  private Jam  jar1 ;
  private Jam  jar2 ;
  private Jam  jar3 ;
  private Jam  selected ;

  // Constructors
  Pantry( Jam jar1, Jam jar2, Jam jar3 )
  {
    this . jar1 = jar1 ;
    this . jar2 = jar2 ;
    this . jar3 = jar3 ;
    selected = null ;
  }

  // Methods
  public void print()
  {
    System.out.print("1: "); jar1 . print() ;
    System.out.print("2: "); jar2 . print() ;
    System.out.print("3: "); jar3 . print() ;
  }

  // assume that the user entered a correct selection, 1, 2, or 3
  public void select( int jarNumber )
  {
    if ( jarNumber == 1 )
      selected =  jar1 ;

    else if ( jarNumber == 2 )
      selected = jar2 ;

    else
      selected = jar3 ;
  }

  // spread the selected jam
  public void spread( int oz )
  {
    selected . spread( oz ) ;
  }
}

class PantryTester
{
  public static void main ( String[] args )
  {
    Jam goose = new Jam( "Gooseberry", "7/4/86", 12 );
    Jam apple = new Jam( "Crab Apple", "9/30/99", 8 );
    Jam rhub  = new Jam( "Rhubarb", "10/31/99", 3 );

    Pantry hubbard = new Pantry( goose, apple, rhub );
    hubbard.print();

    hubbard.select(1);
    hubbard.spread(2);
    hubbard.print();

    hubbard.select(3);
    hubbard.spread(4);
    hubbard.print();
  }
}

When you run the program you will be rewarded with the output:

1: Gooseberry   7/4/86   12 fl. oz.
2: Crab Apple   9/30/99   8 fl. oz.
3: Rhubarb   10/31/99   3 fl. oz.
Spreading 2 fluid ounces of Gooseberry
1: Gooseberry   7/4/86   10 fl. oz.
2: Crab Apple   9/30/99   8 fl. oz.
3: Rhubarb   10/31/99   3 fl. oz.
Spreading 3 fluid ounces of Rhubarb
1: Gooseberry   7/4/86   10 fl. oz.
2: Crab Apple   9/30/99   8 fl. oz.
3: Rhubarb   10/31/99   0 fl. oz.

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.