creation: 8/13/99; edited: 05/05/03, 04/09/06

Fill in the Blanks

Instructions:   This is an ungraded fill-in-the-blank exercise. Each question consists of a sentence with one or two words left out. A button represents the missing word(s). For each question, think of the word or phrase that should fill each blank, then click on the buttons to see if you are correct. No grade is calculated for this exercise.


Often students get stuck in writing a program because there seem to be too many ways to do something and it is not clear which one is correct. This exercise shows several ways in which a program could be written to accomplish the same task. The programs are increasingly complicated, and probably increasingly bad program designs (since simpler solutions are usually best).

1.       Fill in the blanks so that the following program writes "Hello Vesta" six times to the monitor.

class VestaHello
{

  public static void main ( String[] args )
  {
    int j =  ;
  
    while ( j  6 )
    {
        System.out.println( "Hello Vesta" );
        j = j + 1;
    }

  }
}

This program shows little of the object oriented style of programming. For the task at hand it is probably the best of all ten programs since it accomplishes the task in a simple and clear manner. But let us look at some other programs.



2.      Now write the program so that it makes use of a Vesta object that writes "Hello Vesta" to the monitor.

class Vesta
{
  void speak()
  {
    System.out.println( " " );
  }
}

class VestaHello2
{

  public static void main ( String[] args )
  {
    int   j    = 0  ;
    Vesta vest = new   ;
  
    while ( j < 6 )
    {
        vest. ;
        j = j + 1;
    }

  }
}

In this program one Vesta object is created, whose speak() method is invoked six times.



3.      Here is a modification of the program. Now six Vesta objects are created, and each one uses its speak() method once.

class Vesta
{
  void speak()
  {
    System.out.( "Hello Vesta" );
  }
}

class VestaHello3
{

  public static void main ( String[] args )
  {
    int   j    = 0  ;
  
    while ( j < 6 )
    {
        Vesta vest = new   ;
        vest. ;
        j = j + 1;
    }

  }
}

This version of the program is less efficient than the previous program because this version creates six objects all of which do the same thing, and each of which immediately becomes garbage after it is used. This is a poor programming style.



4.      Here is a further modification of the program. Again six Vesta objects are created, but now each one is a temporary, unnamed object.

class Vesta
{
  void speak()
  {
    System.out.println( "Hello Vesta" );
  }
}

class VestaHello4
{

  public static void main ( String[] args )
  {
    int   j    = 0  ;
  
    while ( j < 6 )
    {
        new   .  ;
        j = j + 1;
    }

  }
}

The new operator is used to create each object and each newly created object's speak() method is invoked. In spite of its sophistication, this version of the program is about as impractical as the previous version.



5.      Now the Vesta class is modified so a Vesta object writes out its message six times:

class Vesta
{
  void speak()
  {
    int   j    = 0  ;
  
    while ( j < 6 )
    {
        System.out.println("  ");
        j = j + 1;
    }

  }
}

class VestaHello5
{

  public static void main ( String[] args )
  {
  
     vest =  Vesta() ;
 
    vest .  ; 
  }
}

This version has the advantage that only one object is created, and has the further advantage that main() is now a very simple program. If main() is expected to be become longer as more and more things are added to it, the Vesta object might be sensible. But for the simple task as stated, the first program is still the best.



6.      In this version the class definition includes a constructor that initializes the message to be printed:

class Vesta
{
   message ; // The string to be printed

  // Constructor
  Vesta( String mess )
  {
       = mess ;  // Initialize the message
  }

  void speak()
  {
    int   j    = 0  ;
  
    while ( j < 6 )
    {
        System.out.println(  );
        j = j + 1;
    }

  }
}

class VestaHello6
{

  public static void main ( String[] args )
  {
  
    Vesta vest = new Vesta ( "  " ) ;
 
    vest . speak() ; 
  }
}

Again, whether the Vesta class is sensible depends on the task. If the task is complicated, and the above program is just a small step toward a complete solution, then maybe the Vesta class is a good design.



7.      Further sophistication is to include the iteration limit in the constructor.

class Vesta
{
  String  message ; // The string to be printed
   limit ; // Number of times to print
  
  // Constructor
  Vesta( String mess, int lim )
  {
      message = mess ;  // Initialize the message
      limit =  ;  // Initialize the limit
  }

  void speak()
  {
    int   j    = 0  ;
  
    while ( j <  )
    {
        System.out.println( message );
        j = j + 1;
    }

  }
}

class VestaHello7
{

  public static void main ( String[] args )
  {
  
    Vesta vest = new Vesta ( "Hello Vesta" ,  ) ;
 
    vest . speak() ; 
  }
}

This version of the Vesta class is more flexible than the previous version. It is probably better since it can be used for more situations than the previous.



8.      On further thought (which should have been done before any coding was done) it seems more sensible that the number of times to write the message is better specified as a parameter to the speak() method. This further increases the flexibility of the class.

class Vesta
{
  String  message ; // The string to be printed
  
  // Constructor
  Vesta( String mess )
  {
      message = mess ;  // Initialize the message
  }

  void speak( int )
  {
    int   j    = 0  ;
  
    while ( j <  )
    {
        System.out.println( message );
        j = j + 1;
    }

  }
}

class VestaHello8
{

  public static void main ( String[] args )
  {
  
    Vesta vest = new Vesta ( "Hello Vesta" ) ;
 
    vest . speak( ) ; 
  }
}

This version seem more logical, as well as being more versatile. The number of times a message should be repeated should be a characteristic of the request, not an inherent characteristic of the object.



9.      Mostly a Vesta object says "Hello Vesta" each time it is used. If that is what we want, the main() method should not have to say so. Classes can have several constructors that each take different parameters (remember the Point class.) In the following version of the program there are two constructors, one automatically initializes the message to "Hello Vesta" and the other initializes the message to the requested message.

class Vesta
{
  String  message ; // The string to be printed
  
  // Parameter-less Constructor
  Vesta( )
  {
      message =  "" ;  // Initialize the message
  }

  
  // One Parameter  Constructor
  Vesta(  mess )
  {
      message =   ;  // Initialize the message
  }

  void speak( int limit )
  {
    int   j    = 0  ;
  
    while ( j < limit )
    {
        System.out.println( message );
        j = j + 1;
    }

  }
}

class VestaHello9
{

  public static void main ( String[] args )
  {
  
    Vesta vest = new Vesta () ;  // Use the default value "Hello Vesta"

    vest . speak( ) ; 
  }
}


10.      A further improvement to the Vesta class is to include a method that changes the message string. This is NOT a constructor---it is a method that is part of an existing object and changes that object's message.

class Vesta
{
  String  message ; // The string to be printed
  
  // Parameter-less Constructor
  Vesta( )
  {
      message =  "Hello Vesta" ;  // Initialize the message
  }
 
  // One Parameter  Constructor
  Vesta( String mess )
  {
      message =   mess  ;  // Initialize the message
  }

  // Method that changes this object's message
  void changeMessage(  mess )
  {
      message =   ;  // Initialize the message
  }

  void speak( int limit )
  {
    int   j    = 0  ;
  
    while ( j < limit )
    {
        System.out.println(  );
        j = j + 1;
    }

  }
}

class VestaHello10
{

  public static void main ( String[] args )
  {
  
    Vesta vest = new Vesta () ;  // Use the default value "Hello Vesta"
    vest . speak( 6 ) ; 

    // Change the message to "Hello Ceres"
    vest.changeMessage( " " );
    vest . speak( 6 ) ; 
  }
}

Notice carefully the difference between a constructor and a method: a constructor initializes data in a object when the object is being created. Every use of a constructor creates a new object. A method is part of an object. It changes the data of its object. Because the data in Vesta objects can be changed, they are NOT immutable.



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.