Due: Friday October 12th by 9 am -- no late assignments will be accepted.
This assignment is larger than the others, and a bit harder (it is worth 5%, after all). It is also the first big program that you'll write from scratch, with interacting classes. It will help if you focus on one piece at a time, rather than trying to understand it all at once.
It is meant to be challenging, not frustrating. Don't spend three hours being confused and frustrated: Instead visit your professor during office hours and ask us questions. Email us. Read the newsgroup and announcements page. Visit the TAs in the lab. Use all the resources available to you.
Some newspapers print a puzzle composed of a 5- or 6-letter word such as grints. You are then expected to stare at it and guess that it is a jumbled (or permuted) version of the English word string -- the same characters in a different order.
We'll deal with 4-letter words in this assignment, to make the problem a bit shorter.
You will write a program that has (at least) three public classes:
WordJumble
. Its
main
method will pick a word, jumble it, print
the jumbled version for the user and read three guesses from
the keyboard. For each guess it will
confirm whether the guess was correct. (It
does some of these things using the next two classes.)
You may not use any loops or if
statements on this
assignment.
main
methodString
. For example,
"gonewithrealogrefits" is a list of the words "gone", "with", "real", "ogre" and
"fits". You can pick a particular word out using method
substring
. In this example the words begin at indices 0, 4, 8, 12 and 16
--multiples of 4. To pick one randomly, use the Random
class described in the next section.
How the program initially gets the word list is up to you. You could prompt the user to enter it or you could put it explicitly in the your program. Either way your program must work for word lists of different lengths. In other words, your program must still work if you change the word list from "gonewithrealogrefits" to "fitsgone" but make no other changes to your program.
Random
Class Random
is in package java.util
, so
you'll need to import java.util.*
. Each
Random
object can generate a sequence of random
numbers. The following example code shows how Random
works:
import java.util.*; public class ThreeRandomIntegers { public static void main(String[] args) { Random r = new Random(); // Create a new Random object. // Pick 3 random integers from 0 to 4. int i1 = r.nextInt(5); int i2 = r.nextInt(5); int i3 = r.nextInt(5); // Print them all, separated by spaces. System.out.println(i1 + " " + i2 + " " + i3); } }
Now that you know how to generate a random number from {0, 1, 2, 3, 4}, can you modify it to choose one of {0, 4, 8, 12, 16}? You can use that technique to figure out the index of the beginning of a random word in the word list. Your approach should work for word lists containing any number of words.
Suppose you have a four-letter word, and an empty
String
. Randomly select a letter from the four-letter
word, and add it to the String
. Remove the letter you
picked from the four-letter word, turning it into a three-letter
word.
Note that if you do this three more times, you'll have used all the
letters in the original word, and your empty String
will
have turned into a four-letter jumble of the original word.
WordJumble.java
and the .java files containing the
other two classes that you wrote for this assignment.