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 the design of 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 ) throws IOException
  {

    

    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 save the second task for later. The first player is supposed to enter a three letter word. The length of a String objects (the number of characters it contains) can be checked with its length() method. Here is more of the program:

class MatchingGame
{
  public static void main ( String[] args ) throws IOException
  {
    BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );

     theWord;

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

    while ( theWord.length()  )
    {
        System.out.println("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, now with this idea partly implemented. Assume that 50 blank lines will clear the screen.

class MatchingGame
{
  public static void main ( String[] args ) throws IOException
  {
    BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );

    String  theWord;

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

    while ( theWord.length() != 3 )
    {
      System.out.println("The word must be three characters long. Please try again.");
      theWord = stdin.readLine() ;    
    }

    // 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 ) throws IOException
  {
    BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );
    String  theWord;

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

    while ( theWord.length() != 3 )
    {
      System.out.println("The word must be three characters long. Please try again.");
      theWord = stdin.readLine() ;    
    }

    // 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.println("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 ) throws IOException
  {
    BufferedReader stdin = new BufferedReader( new InputStreamReader( 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.println("Enter Your Guess:");
    guess = stdin.readLine();

    // 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 = stdin.readLine();
      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 ) throws IOException
  {
    BufferedReader stdin = new BufferedReader( new InputStreamReader( 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.println("Enter Your Guess:");
    guess = stdin.readLine();

    // while the second player still has not guessed the word and has some tries left
    while ( count < 5  &&  !guess.equals( theWord ) )
    {
      System.out.println("Enter Another Guess:");
      guess = stdin.readLine();
      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 (repeated here so that you can copy it to an editor, paste it to a file, compile and run it.)

import java.io.* ;
class MatchingGame
{
  public static void main ( String[] args ) throws IOException
  {
    BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );
    String  theWord;

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

    while ( theWord.length() != 3 )
    {
      System.out.println("The word must be three characters long. Please try again.");
      theWord = stdin.readLine() ;    
    }

    // 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.println("Enter Your Guess:");
    guess = stdin.readLine();

    // while the second player still has not guessed the word and has some tries left
    while ( count < 5  &&  !guess.equals( theWord ) )
    {
      System.out.println("Enter Another Guess:");
      guess = stdin.readLine();
      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.