Let's play the word scramble game. Try to guess the word from the scrambled word. The scrambled word is hylys What is your guess? lyhsy Sorry, the correct word is shyly
After logging in to your account, create a new directory named lab3 in your cpsc115 directory. Dwnload the following file into your lab3 directory.
The KeyboardReader class contains methods that will allow us to perform the command-line I/O needed for this problem. Here is a UML diagram of the class. You will be using the constructor and the prompt(), display(), and getKeyboardInput methods for this program:
Here's an example of how to use a KeyboardReader object to prompt the user for an input. Note the use of the escape character "\n" to print a carriage return:
KeyboardReader reader = new KeyboardReader(); // Create a KeyboardReader object
reader.prompt("Type in your name "); // Prompt the user
String name = reader.getKeyboardInput(); // Store the user's input
reader.display("Hello " + name + "\n"); // Display a greeting
What objects and classes do we need to solve this problem? We will follow the design of the Riddle and RiddleUser classes from §2.4.
Thus, we will define two classes. The Scramble class will be similar to the Riddle class. It will be the computational object for this program. It will have instance variables to store the words used in the game. It will have four methods: a constructor method that will be used to make new Scramble objects, methods to get the word and the scrambled word, and a boolean method named isCorrectGuess() to test whether a guess is the correct word.
Draw a UML diagram of the Scramble class and have it checked by the TA or lab instructor.
The ScrambleUser class will contain a main() method, which is where you will place your game-playing algorithm.
Step 4. Implement the Scramble Class.
Use stepwise refinement to implement the Scramble class. For a first step, implement a stub class definition. Then add in definitions for the instance variables, then definitions for each of the four methods. After each step, compile your program to make sure it does not contain syntax errors. The Scramble class does not contain a main() method, so you won't be able to run it.
Step 5. Implement the isCorrectGuess() Method.
The most challenging part of definition of the Scramble class is the implementation of the isCorrectGuess() method. This method takes a String argument and returns true if the argument equals the Scramble word and false otherwise. So, this method takes a String parameter and returns a boolean result. The body of the method should use the String class's equals() method to compare the two strings. Here's an example of how to use it:
word.equals(guess)
In this case, assume that both word and guess are Java Strings. Then this expression returns true if they are equal and false otherwise.
Ask for help on this part if you have trouble.
Step 6. Implement the ScrambleUser Class.
A good model for this class is the RiddleUser.java class.
The main() method for this class should declare variables for two objects: a Scramble object and a KeyboardReader object.
Your algorithm should then make use of these objects to play the game with the user. Use stepwise refinement, testing after each statement to make sure your code is running correctly.
Your algorithm will need to use an if statement to determine whether the user guessed correctly. In this example, the Scramble object is named scramble and the KeyboardReader object is named reader:
if (scramble.isCorrectGuess(guess))
reader.display("Good guess.\n");
else
reader.display("Sorry, the correct word is " + scramble.getWord() + "\n");
You can compile and run all the Java files used in this program by the following commands:
javac ScrambleUser.java
java ScrambleUser
Before handing in your programs, be sure to document your source code.
Have your work checked by the laboratory instructor or TA. Print out and hand in copies of your Scramble.java and ScrambleUser.java files.