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.
- Set up a new eclipse project for each lab (detailed instructions)
- Copy and paste file WordMorph.java into your project folder.
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.
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.
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.
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.
Now grab those 2 characters from the change string and put
them into character variables. Use .charAt()
// 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.
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.
// 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.
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.
// 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:
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.
Save your
solution document and your program. Show your game to a
friend and see if they like it.