Prog 2 Precaution
Prof. Reed, CS 102, Fall 2011
Program is due at 11:59pm on the date indicated on the course schedule page.
In learning something new it is good to take every precaution. Write a word-guessing game where words must be formed using the letters from the word "precaution," where each letter is used at most once. Running your program should look like the following:
Author: Dale Reed Program: #1, Precaution TA: Richard Francis Burton, Wed 8am Play the game of Precaution. Try and create as many unique words as you can using only the letters in the word 'precaution' where each letter is used at most once. Words must be at least 3 characters long. An added wrinkle is that The last letter of each word guessed must be the first letter of the next word guessed. Type in the word 'exit' to exit the program. Guess a word using the letters in "precaution": trip 1 words found: trip Guess a word using the letters in "precaution": print 2 words found: trip print Guess a word using the letters in "precaution": to Sorry, words must be longer than 3 characters. Guess a word using the letters in "precaution": trecapn Sorry, that is not a valid dictionary word. Guess a word using the letters in "precaution": tear 3 words found: trip print tear Guess a word using the letters in "precaution": trap Sorry, the current word must start with 'r' Guess a word using the letters in "precaution": root Sorry, you used an invalid letter. Guess a word using the letters in "precaution": real Sorry, you used an invalid letter. Guess a word using the letters in "precaution": exit Thanks for playing. 3 words found: trip print tear Goodbye.
You need to know the following concepts in order to write this program:
Running a program in Eclipse (and installing Java and Eclipse if doing this on your own machine)
Basic input and output using Strings; the charAt() method to extract a single character from a String; Concatenating multiple words to store them into a String; doing a substring search
String and int variables, if statement, some sort of loop (while, do-while, or for)
It will likely be very helpful for you to have reviewed the break and continue statements to make it easier to jump around in loops.
Hints:
Consider writing the program a step at a time, using the following order. The number of points for each item are shown, out of the 55 total points for program execution.
(0 points) Display instructions, prompt for input, read in the input into a String and echo the input (to make sure you got it). As always, compile and execute before going to the next step described here. (no loops or anything yet here in your code). You can use the Precaution.java code as a starting point.
(0 points) Create a loop around the code that prompts for an input word.
(0 points) Check the input to see if the word 'exit' was entered. If so, exit the program.
(5 points) Check the input to ensure the length is >= 3. If not, give a warning message and use continue to go back up to the top of the loop.
(15 points) For valid words store them into a String that is used to accumulate all the words found so far. After adding the new word to this String, extract the words in the String and display them, 5 across in lined-up columns. For each new word ensure it has not already been previously guessed.
(20 points) Check the input to ensure letters in 'precaution' are the only ones used, and they are used at most once. You will need to use some additional variables somehow to do this check. Again if a letter is used more than once, give a warning message and go back up to the top of the loop.
(15 points) Look up each word in a dictionary of words, ensuring only valid dictionary words are used. The code in this program includes code for a dictionary lookup. You will also need this dictionary, which should be placed in the Eclipse project directory (but not in the bin or src subdirectories.)
Make sure you review the course syllabus regarding program documentation. The header in your program should clearly describe not only what the program is supposed to do, but which parts you have/haven't done.
Turn in your program into Blackboard, into the Program 2 assignment. You can turn it in multiple times, but only the most recent version turned in will be graded.
Looking for up to 5 points of extra credit? Do the extra work described below. Be sure that you have turned in a completed working version of the basic requirements before you try this extra credit.
Instead of simply using the word 'Precaution', choose randomly from among a set of possible words to use. (See this code for a sample of a random number generator.) Implement a timer (see ElapsedTime.java), giving users a maximum of 4 minutes. Once time is up, display all the possible words that could have been formed.