creation: 08/12/99; revised 05/21/03, 01/15/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.


This exercise involves a computer game:

The first player starts the program and enters a three letter word. The second player does not see what word is entered. Now the second player gets the computer. The second player has five tries to guess the word. If the second player does not guess the word, the first player wins.

(This is not a very good game; you may wish to work on it some more at the end of this exercise.)

1.   Sometimes when you start out a program it is nice to write comments that make it clear to yourself what you are about to do. In the following, decide on two major divisions for the program. Then click on the buttons to see if you agree with me:

class MatchingGame
{
  public static void main ( String[] args )  
  {

    

    Code for first section of the program goes here.

    

    Code for second section of the program goes here.

  }
}

You might argue that the program is much more complicated than this. And of course, it is. But at the outermost level the program consists of code to perform just these two tasks.


2.   Let us think about the first task, and think about the second task later. The first player enters a three letter word. The length of a String object (the number of characters it contains) is retrieved by using its length() method. Here is more of the program:

class MatchingGame
{
  public static void main ( String[] args ) 
  {
    Scanner scan = new Scanner( System.in );

     theWord;

    // Get the word from the first player.
    System.out.print("Enter the word to be guessed: ");
    theWord = scan. 

    while ( theWord.length()  )
    {
        System.out.print("The word must be three characters long. Please try again: ");
         
    }

    // Play the game with the second player.  
    
    . . . .
    
  }
}


3.   We are nearly done with the first part, but there is a problem: when the first player turns the keyboard over the second player, the word to be guessed is there in plain view! How do you propose to overcome this problem?

(There are more elegant solutions, but this one works well enough.)



4.   Here is the program with this idea partly implemented. Assume that 50 blank lines will clear the screen.

class MatchingGame
{
  public static void main ( String[] args )  
  {
    Scanner scan = new Scanner( System.in );
    String  theWord;

    // Get the word from the first player.
    System.out.print("Enter the word to be guessed: ");
    theWord = scan.next() ;

    while ( theWord.length() != 3 )
    {
       System.out.print("The word must be three characters long. Please try again: ");
       theWord = scan.next() ;    
    }

    // Clear the Screen
    int lineCounter =  
     ( lineCounter < 50 )
    {
        
        lineCounter = lineCounter + 1;
    }

    // Play the game with the second player.  
    
    . . . .
    
  }
}


5.   Now for the second half of the program. In this part of the program, the second player repeatedly tries to guess the word. How (in general) will this repeated guessing be done in the program?

With a



6.   Here is the program with some work done on the second half. First decide on the variables that are needed, then initialize them:

class MatchingGame
{
  public static void main ( String[] args ) 
  {
    Scanner scan = new Scanner( System.in );
    String  theWord;

    // Get the word from the first player.
    System.out.print("Enter the word to be guessed: ");
    theWord = scan.next() ;

    while ( theWord.length() != 3 )
    {
      System.out.print("The word must be three characters long. Please try again: ");
      theWord = scan.next() ;
    }

    // Clear the Screen
    int lineCounter = 0 ;
    while ( lineCounter < 50 )
    {
      System.out.println();
      lineCounter = lineCounter + 1;
    }

    // Play the game with the second player.  
    int count = 1;
     guess;

    System.out.print("Enter Your Guess: ");
     

    // while the second player still has not guessed the word and has some tries left
    while ( )
    {

    }

    // check if second player won
    if ()
    {

    }

    else
    {

    }

  }
}


7.   The while in the second half will be somewhat complicated. It must keep looping as long as two things are true:

  1. The user has tries left.
  2. The user has not yet guessed the word.

To test the first part, use a relational operator. To test the second part, use the equals() method of the String guess. Each of these is a true/false value which must be combined using a logical operator.

class MatchingGame
{
  public static void main ( String[] args ) 
  {
    Scanner scan = new Scanner( System.in );
    String  theWord;

    // Get the word from the first player.

    . . . . .
    
    // Play the game with the second player.  
    int count = 1;
    String guess;

    System.out.print("Enter Your Guess: ");
    guess = scan.next() ;

    // while the second player still has not guessed the word and has some tries left
    while ( count < 5 !guess. )
    {
      System.out.println("Enter Another Guess:");
      guess = scan.next() ;
      count   = 
    }

    // check if second player won
    if ()
    {

    }

    else
    {

    }

  }
}

The loop condition combines the ideas of a counting loop and a sentinel loop.



8.   When the loop ends one of two things will be true: the second player has guessed the number and won, or the second player has used up all the tries and lost. The final if statement must detect which is true in order to write out the correct concluding message.

Think of what the if statement should test:

class MatchingGame
{
  public static void main ( String[] args ) 
  {
    Scanner scan = new Scanner( System.in );
    String  theWord;

    // Get the word from the first player.
    . . . . .

    // Play the game with the second player.  
    int count = 1;
    String guess;

    System.out.print("Enter Your Guess: ");
    guess = scan.next() ;

    // while the second player still has not guessed the word and has some tries left
    while ( count < 5  &&  !guess.equals( theWord ) )
    {
      System.out.print("Enter Another Guess: ");
      guess = scan.next() ;
      count = count + 1;
    }

    // check if second player won
    if ()
      System.out.println("You Won!");
    else
      System.out.println("You Lost!");

  }
}


9.   Here is the complete program. You can copy it to an editor, paste it to a file, compile and run it.

import java.util.Scanner;
class MatchingGame
{
  public static void main ( String[] args ) 
  {
    Scanner scan = new Scanner(System.in);
    String  theWord;

    // Get the word from the first player.
    System.out.print("Enter the word to be guessed: ");
    theWord = scan.next() ;

    while ( theWord.length() != 3 )
    {
      System.out.print("The word must be three characters long. Please try again: ");
      theWord = scan.next() ;  
    }

    // Clear the Screen
    int lineCounter = 0 ;
    while ( lineCounter < 50 )
    {
      System.out.println();
      lineCounter = lineCounter + 1;
    }

    // Play the game with the second player.  
    int count = 1;
    String guess;
    System.out.print("Enter Your Guess: ");
    guess = scan.next() ;

    // while the second player still has not guessed the word and has some tries left
    while ( count < 5  &&  !guess.equals( theWord ) )
    {
      System.out.print("Enter Another Guess: ");
      guess = scan.next() ;
      count = count + 1;
    }

    // check if second player won
    if ( guess.equals( theWord ) )
      System.out.println("You Won!");
    else
      System.out.println("You Lost!");

  }
}

Are any of the if or while statements nested?

It will benefit you to study the organization of this program. Observe how it is put together with a sequence of while statements, and if statements.



10.   Say that you wanted to play the game with THREE hidden words instead of one. The first player enters a word, the second player has five changes to guess it, then the first player enters a second word, the second player has five changes to guess it, and so on. (This is still a stupid game.)

How would you implement this new game?    

An even better game would consist of three "rounds". Each round would consist of the first player entering a word and the second player guessing followed by the second player entering a word and the first player guessing.



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.