Assignment 4: Word Jumbles

Due: Friday October 12th by 9 am -- no late assignments will be accepted.


Warning

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.


Overview

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:

You may not use any loops or if statements on this assignment.


The main method

The main method should do the following steps.
  1. Set up the word list.
  2. Get a word randomly from the word list.
  3. Create a jumble of that word.
  4. Display the jumbled version on the screen for the user.
  5. Ask the user to guess the original (un-jumbled) word.
  6. Tell the user whether or not the guess was correct.
  7. Repeat steps 5 and 6 two more times.

The word list

The word list is kept in a String. 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.


Class 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.


Generating a jumble

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.


Submission

Submit WordJumble.java and the .java files containing the other two classes that you wrote for this assignment.

Feeling overwhelmed?

Here are a few tips to help you approach this assignment.
Danny Heap
Last modified: Fri Sep 28 17:09:58 EDT 2001 f