Guessing Game Assignment

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

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.

Resources

Driver program with line numbers

Basic requirements analysis

  • You should assume all fields in HiLoGame are private unless otherwise indicated.
  • All fields (class-level variables) that make sense to be constant ("final") should 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

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)