CS 5JA, Winter 2009
Assignment 5 - A guessing game, and some array practice
Due: Wednesday, March 11, 9:00 pm
Worth: 100 homework points
- Write the following two classes to play a number guessing game with the user:
- RandomPick.java - to represent a secret, random number pick
(a single integer selected randomly), with methods available to check the accuracy of guesses.
Include the following features:
- An
int
instance variable to store the random pick.
- A constructor that takes two
int
arguments: the minimum and maximum values that
define the allowable range from which to pick the random number. This constructor should use
a java.util.Random
object to select the pick (insuring it is greater than or equal
to the minimum, and less than or equal to the maximum).
- Three predicate methods:
public boolean goodGuess(int guess)
- returns
true
if the guess equals the random pick (or false
otherwise)
public boolean highGuess(int guess)
- returns true if the guess is greater
than the pick
public boolean lowGuess(int guess)
- returns true if
the guess is less than the pick.
Download, compile and run RandomPickTest.java to verify
your class's public interface is correct, and to test your methods. Verify results
match these sample RandomPickTest results from our solution.
- GuessNumber.java - to let a user play the guessing game.
- Match these sample GuessNumber runs
- including the menu, prompt and result strings, as well as the error-handling techniques
and messages.
- Use the minima and maxima shown in the sample runs: 1-5 if the user chooses the easy option
(i.e., the user enters
1
in response to the menu), 1-20 if the user chooses the
medium option, and 1-1000 if the user chooses the difficult option.
- Note the sample runs at the bottom include error situations. If the user enters improper
data, the program does not quit. Instead it gives the user additional chances to get it right.
- The program should let the user continue to guess until the correct answer is given. Then the
program lets the user know the answer is correct, and terminates.
- It should go without saying, but we expect you to use a RandomPick object in your solution.
- Complete CharCounter.java as a class that provides
various counts based on a
char
array that is passed to its constructor.
- Begin with this CharCounter skeleton, and
follow the instructions in that file.
- Basically the problem is to implement this public
interface (but the javadoc comments are not required) - a constructor and six methods:
- Notice the method named
count
is overloaded. The version that takes no arguments
should return the total number of characters in the array (i.e., the array's length). The
version that takes a char
argument should return the number of occurrences of
that character in the array.
countEach
- takes an array argument and returns an array of corresponding counts.
The order of the counts (in the int
array that is returned) match the order of
the characters in the parameter - first element equals the number of occurrences of the first
character, and so on.
countLetters
- returns the number of occurrences of characters in the range
'A'
-'Z'
and 'a'
-'z'
.
countDigits
- returns the number of occurrences of characters in the range
'0'
-'9'
.
countOther
- returns the number of occurrences of characters outside the ranges
of the letters and digits.
- Use CharCounterTest.java to test your class. This
application reads text until
EOF
is encountered (end-of-file, or when the user
types ctrl-Z in Windows or ctrl-D in a different operating system). Insure that results are
correct. For example, here are the correct results based on redirecting the testing program
itself as its own input: counts for CharCounterTest.java.
- Of course you should probably begin your testing on smaller inputs, but the program's range
should only be limited by available memory. A very large example shows
counts for War and Peace by Leo Tolstoy
(actually an English translation).
- Turn in RandomPick.java, GuessNumber.java and CharCounter.java
here
before the deadline.
Updated 2/25/09, by C. Michael Costanzo