Objectives

In this lab you will start with a skeleton of a simple game, and fill in the parts of the game using the new java constructs that you are learning: characters, strings, and string operations.

Preparation


Exercise 1: (2 points) - using string.length()

We are going to be building a simple but challenging word game. Run the WordMorph.java program. It doesn't do much yet except print instructions and ask the user to type in a word. You are going to fill out this skeleton program step by step.

In the code you will find comments where the answers for each exercise will go. So far the starting code just prints instructions and asks the user to type in a word. In Exercise 1 you will determine the length of the input word.

Copy and paste the code below into the matching place in the program and then fill in the blank with code to make it work.

        // Exercise 1: use .length() to find the length of the word string
        int len = _______________;

Test it out and make sure it works before continuing. Note: When you run the program, you need to click on the console window before typing your input.

Copy and paste just the Exercise 1 comment and the modified line into your solutions document.

Exercise 2: (2 points) - using string1.equals(string2)

The starting code now has a loop. (I promise we will learn about loops soon.)

Inside the loop the program asks the user to type 2 characters: an old character and a new replacement for that character. These are both scanned into the string "change". One possibility is that they typed "quit". In Exercise 2 you are going to check for this, and if they did type "quit", the code below will print a message and then end the program using System.exit(). Use the .equals() method to compare strings.

            // Exercise 2: use .equals to check if the change string says "quit"
            if (______________) {
                System.out.println("This is a hard game. Thanks for playing.");
                System.exit(1);
            }

Test it out and make sure typing "quit" works.

Copy and paste just the Exercise 2 comment and code block into your solutions document.

Exercise 3: (2 points) - using string.charAt(position)

Now grab those 2 characters from the change string and put them into character variables. Use .charAt()

            // Exercise 3: use .charAt(position) to get the two characters
            // from the change string
            // and put them into char variables oldChar and newChar
            char oldChar = ________________;
            char newChar = ________________;
            System.out.println("replacing " + oldChar + " with " + newChar);

That println() statement is just for testing. After you have confirmed that this step works, you should comment out or delete that statement.

The way we are writing this program is called incremental development. Testing and making sure that each little piece of code works as you develop your program in small steps.

Copy and paste just the Exercise 3 comment and code into your solutions document.

Exercise 4: (2 points) - using string.indexOf(char)

Check that the old character that they are replacing is actually in the word. To check that a string contains a character, you can use string.indexOf(char). It returns the index of the first match or -1 if the character is not found.

            // Exercise 4: use .indexOf(char) to check that the word string
            // contains oldChar
            int index = ________________;
            if (index == -1) {  // indexOf(c) returns -1 if c in not found
                System.out.println("Old character not found. Exiting.");
                System.exit(1);
            }

Test it out and make sure it is working before continuing.

Copy and paste just the Exercise 4 comment and code into your solutions document.

Exercise 5: (2 points) - using string.replace(oldChar, newChar);

Finally we are ready to replace that character! Remember that Strings are immutable. We don't really replace a character in a string, but instead create a new string, and then keep that new string instead of the original.

            // Exercise 5: use .replace(oldChar, newChar) to replace
            // oldChar with newChar in the word string
            word = ___________________;

Your game is finished, test it out and make sure it works.  Your programs output should look like:

     word  : dk
1    work  : wf
2    fork  : kt
3    fort  : fp
4    port  : ...

How many words can you get?

Copy and paste just the Exercise 5 comment and code into your solutions document.

YOU ARE DONE!

Save your solution document and your program. Show your game to a friend and see if they like it.