CIS 260 Guessing Game assignment
Overview
You have to create a class that can support the game of
having the user guess a number within a range and telling
the user whether their guess is high, low, or correct. Most of
the game logic is already given to you in a file named
PlayGuessingGame.java. Some utility routines for
input are provided in a file named Keyboard.java.
You have to create a class named HiLoGame that
provides the missing functionality. You have to determine the
requirements for HiLoGame by examining the code
in PlayGuessingGame.java.
Objectives
- analyze a problem to determine requirements
- write a Java class to meet specified requirements
- test and debug an application
- compile a multi-file Java application
- use object-oriented programming techniques when creating a class
- follow Java coding style guidelines
Resources
- PlayGuessingGame.java
- Keyboard.java
Driver program with line numbers
1 /*
2 PlayGuessingGame.java
3 Dave Klick
4 CIS 260
5 2015-01-13
6
7 This program uses the HiLoGame class to play a number
8 guessing game. It is provided as a driver to be used
9 once the HiLoGame class has been implemented.
10
11 Note: This class depends upon the presence of Keyboard.java
12 (or Keyboard.class) in the same directory. Keyboard.java is
13 provided on the course web site.
14 */
15
16 public class PlayGuessingGame {
17 public static void main(String[] args) {
18 int low = 100;
19 int high = 1;
20 int guess = 0;
21 char again = 'Y';
22 String result = "";
23 int status;
24 Keyboard kbd = new Keyboard();
25
26 while (again == 'Y') {
27 HiLoGame game = new HiLoGame(high, low);
28 status = HiLoGame.LOW;
29 System.out.printf("Guess the secret number between %d and %d\n", game.getLow(), game.getHigh());
30 while (status != HiLoGame.CORRECT) {
31 guess = kbd.getInt("Your guess: ", game.getLow(), game.getHigh());
32 status = game.checkGuess(guess);
33 switch (status) {
34 case HiLoGame.LOW: result = "low"; break;
35 case HiLoGame.HIGH: result = "high"; break;
36 case HiLoGame.CORRECT: result = "correct"; break;
37 default: result = "undefined"; break;
38 }
39 System.out.printf("Your guess #%d was %s\n", game.getGuesses(), result);
40 }
41 System.out.println("Congratulations!");
42 System.out.printf("You got the right number in %d guesses.\n\n", game.getGuesses());
43 again = Character.toUpperCase(kbd.getChar("Play again (Y/N)? ", "YyNn"));
44 }
45
46 System.out.printf("\n%d %s played.\n", HiLoGame.getGames(),
47 HiLoGame.getGames() == 1 ? "game was" : "games were");
48 }
49 }
Basic requirements analysis
- You should assume all fields in HiLoGame are private unless otherwise indicated.
- For safety purposes, all parameters to methods should be declared as final. All
fields (class-level variables) that make sense to be "final" should also be declared
that way.
- Lines 18-19 indicate the HiLoGame class must be alert for the low and high
values being out of order and swap them if needed
- Line 27 requires a constructor which accepts two integers (high and low).
Since no code after this point requests a new number and the guessing (using
checkGuess) starts in a few lines, we know that the constructor must be
choosing a random number within the given range and storing it for use in
checking guesses.
- Lines 28, 34-36 show that there must be constants in the class for LOW, HIGH,
and CORRECT. These must be integer constants (since they are being stored in
"status", which is an int. They must also be static fields since they are being
accessed using the class name and not an object reference. They should have
public access since the code is accessing them directly from another class.
- Line 29 requires accessor methods named getLow and getHigh which return the values
sent to the constructor (which were swapped if needed). This means that those values
should have been stored by the constructor.
- Line 32 requires a checkGuess method which returns HiLoGame.LOW, HiLoGame.HIGH,
or HiLoGame.CORRECT. Since the HiLoGame constructor presumably generated a random
number and saved it, and this method has been sent the user guess, it should be
fairly straight-forward to figure out the logic needed to return one of the constant
values.
- Lines 39, 42 require a getGuesses method which tells us how many times checkGuess
has been called. This means there must be a field in HiLoGame to keep track of the
number of guesses and that checkGuess must increment that field. Since guesses are
per game (which is equivalent to being per HiLoGame object in this case), this field
must be an instance variable.
- Lines 46, 47 require a getGames method which tells us how many HiLoGames have been
played. A look at the code reveals that a new HiLoGame object/instance is created for
each game. A variable/field tied to the class rather than to an instance/object must
keep track of the number of HiLoGame objects that have been created. Every time the
constructor is called it should increment this variable. The getGames method returns
this value. This method is also tied to the class rather than to an object since it
is called using the class name.
Sample output
Guess the secret number between 1 and 100
Your guess: 50
Your guess #1 was high
Your guess: 25
Your guess #2 was high
Your guess: 12
Your guess #3 was high
Your guess: 6
Your guess #4 was low
Your guess: 9
Your guess #5 was low
Your guess: 10
Your guess #6 was correct
Congratulations!
You got the right number in 6 guesses.
Play again (Y/N)? 5
Error: Character entered is not a valid response.
Play again (Y/N)? y
Guess the secret number between 1 and 100
Your guess: 900
Error: Above maximum value of 100
Your guess: -9
Error: Below minimum value of 1
Your guess: 50
Your guess #1 was high
Your guess: 25
Your guess #2 was low
Your guess: 37
Your guess #3 was high
Your guess: 31
Your guess #4 was high
Your guess: 28
Your guess #5 was low
Your guess: 29
Your guess #6 was correct
Congratulations!
You got the right number in 6 guesses.
Play again (Y/N)? n
2 games were played.
Rubric
- 5 points for following coding conventions and having documentation comments
- 8 points for having working accessor methods with the appropriate access modifiers
- 12 points for having the constructor working properly
- 10 points for having methods other than the constructor and accessor methods working
properly and having the appropriate access permissions
- 5 points for having the required working constants with appropriate modifiers
- 8 points for having fields as required with the appropriate modifiers
- 2 points for using the "final" modifier for all method parameters and where appropriate
for fields (class-level variables)