Running on Java 17+35-2724 (Preview)
Home of The JavaSpecialists' Newsletter

3.4 SortedScrambledWordLibrary

Juppies 2 by Dr Heinz M. Kabutz

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.

Comments

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Table of Contents

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.



About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack 21

Superpack 21 Our entire Java Specialists Training in one huge bundle more...

Free Java Course

Free Juppies 2 Course
Juppies 2 - a course for complete beginners more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...

Java Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.