Our first implementation of ScrambledWordLibrary
will be the
SortedScrambledWordLibrary
. Alt+Enter on the class name and select "Implement
Abstract Class". It sounds contradictory to be "sorted" and "scrambled", but
it isn't. The letters will be scrambled in a particular way compared to the
correct word.
Inside our scramble()
method we call Arrays.sort(letters)
.
Arrays
is a helper class inside the
java.util
package. This is not imported by default, which is
why the Arrays
class is underlined in red. ALT+Enter suggests the fix "Add
import for java.util.Arrays".
Let's go back to our WordLibraries
facade and in the
createDefaultWordLibrary()
we now wrap the ShuffledWordLibrary
with our new
SortedScrambledWordLibrary
.
We run our game, which compiles and tests that we have not introduced bugs into the code. The tests pass, which either means that our code is correct or that our test did not find the new bugs. By now we should have a healthy level of scepticism about our ability to find all the bugs in our code with our tests. We get the first word, which is "aeeilnqtuv". Looks Estonian or Finnish. No idea what that is, oh wait, perhaps it is equivalent? Next us is "ceeffo". That's easy - coffee of course. And then "aaeilmnptu" - hmmm these sorted words seem harder to guess than the previously scrambled ones! "abimpt"? What is that???
This is too hard. Wouldn't it be nice to have a "Hint" button that shows us the first letters of the word?
The words are shuffled and the individual letters sorted, which seems to make them harder to guess, at least for me. I think it will get easier once I've guessed each word correctly, but for now it is challenging. A "Hint" button would be welcome.
We hope you enjoyed this tutorial. If you did, you will also enjoy our courses. We suggest you start with Extreme Java - Advanced Java, followed by Extreme Java - Concurrency Performance for Java 8.
We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.
We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...